使用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
相关产品推荐
相关产品推荐

