为 lasso 回归缩放数据
为拟合 lasso 回归模型做准备时,重要的一步是对数据进行缩放,使所有特征在可比尺度上。完整的 King County, California 房屋销售数据在 house_sales_df 中可用。
在本练习中,您将先单独缩放目标变量 price,然后再将数据划分为训练集和测试集。原因与 tidymodels 的 recipe 工作方式有关。我们不会在 recipe 中包含对目标变量的变换。
已为您加载 tidyverse 和 tidymodels 包。
本练习是课程的一部分
R 中的降维
练习说明
- 使用
scale()对house_sales_df中的目标变量price进行缩放。 - 创建训练集和测试集,训练集占 80%。
- 使用训练数据创建 recipe,对所有数值型自变量进行缩放。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Scale the target variable
house_sales_df <- ___ %>%
mutate(price = as.vector(___(___)))
# Create the training and testing sets
split <- ___(___, prop = ___)
train <- ___ %>% ___()
test <- ___ %>% ___()
# Create recipe to scale the predictors
lasso_recipe <-
___(___ ~ ., data = ___) %>%
___(___())