You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用tidymodels调优样条degree遇类型不匹配错误,是否为bug?

问题解决:调优样条配方degree参数时的类型不匹配错误

错误原因

step_spline_b的degree参数要求整数类型(样条阶数通常为2、3这类整数),但默认的tune()调用会尝试使用连续型(double)参数范围,导致类型不匹配报错。

解决方案

需要明确指定调优参数为整数类型,以下两种方法均可解决:

方法1:在tune()中直接定义整数范围

使用带L的整数语法(如c(2L, 5L))指定范围,明确告知dials这是整数参数:

library(tidymodels)

rec <- recipe(mpg ~ disp, data = mtcars) |> 
  step_spline_b(disp, degree = tune("spline_degree", range = c(2L, 5L)))

rs <- vfold_cv(mtcars) 

workflow() |> 
  add_recipe(rec) |> 
  add_model(linear_reg()) |> 
  tune_grid(resamples = rs)

方法2:提前定义整数参数对象

用dials::integer()创建整数参数对象,再传入tune():

library(tidymodels)

# 定义整数参数范围
spline_degree_param <- integer(range = c(2, 5))

rec <- recipe(mpg ~ disp, data = mtcars) |> 
  step_spline_b(disp, degree = tune("spline_degree", spline_degree_param))

rs <- vfold_cv(mtcars) 

workflow() |> 
  add_recipe(rec) |> 
  add_model(linear_reg()) |> 
  tune_grid(resamples = rs)

是否为Bug?反馈渠道

这不属于Bug,是参数类型匹配的问题——需要明确告知调优工具参数的整数类型。如果认为参数类型推断逻辑可以优化,可向recipes或dials包提交反馈。

内容的提问来源于stack exchange,提问作者Andrew Barros

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 19:42:04