- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这与 Predict with step_naomit and retain ID using tidymodels 是同一个问题,但即使有一个可接受的答案,OP 的最后评论指出了“id 变量”被用作预测变量的问题,正如在查看 model$fit$variable.importance
时所见.
我有一个包含“id 变量”的数据集,我想保留。我认为我可以通过 recipe() 规范实现这一点。
library(tidymodels)
# label is an identifier variable I want to keep even though it's not
# a predictor
df <- tibble(label = 1:50,
x = rnorm(50, 0, 5),
f = factor(sample(c('a', 'b', 'c'), 50, replace = TRUE)),
y = factor(sample(c('Y', 'N'), 50, replace = TRUE)) )
df_split <- initial_split(df, prop = 0.70)
# Make up any recipe: just note I specify 'label' as "id variable"
rec <- recipe(training(df_split)) %>%
update_role(label, new_role = "id variable") %>%
update_role(y, new_role = "outcome") %>%
update_role(x, new_role = "predictor") %>%
update_role(f, new_role = "predictor") %>%
step_corr(all_numeric(), -all_outcomes()) %>%
step_dummy(all_predictors(),-all_numeric()) %>%
step_meanimpute(all_numeric(), -all_outcomes())
train_juiced <- prep(rec, training(df_split)) %>% juice()
logit_fit <- logistic_reg(mode = "classification") %>%
set_engine(engine = "glm") %>%
fit(y ~ ., data = train_juiced)
# Why is label a variable in the model ?
logit_fit[['fit']][['coefficients']]
#> (Intercept) label x f_b f_c
#> 1.03664140 -0.01405316 0.22357266 -1.80701531 -1.66285399
由 reprex package 创建于 2020-01-27 (v0.3.0)
但即使我确实指定 label
是一个 id 变量,它仍被用作预测变量。所以也许我可以在公式中使用我想要的特定术语,并专门添加 label
作为 id 变量。
rec <- recipe(training(df_split), y ~ x + f) %>%
update_role(label, new_role = "id variable") %>%
step_corr(all_numeric(), -all_outcomes()) %>%
step_dummy(all_predictors(),-all_numeric()) %>%
step_meanimpute(all_numeric(), -all_outcomes())
#> Error in .f(.x[[i]], ...): object 'label' not found
由 reprex package 创建于 2020-01-27 (v0.3.0)
我可以尝试不提及 label
rec <- recipe(training(df_split), y ~ x + f) %>%
step_corr(all_numeric(), -all_outcomes()) %>%
step_dummy(all_predictors(),-all_numeric()) %>%
step_meanimpute(all_numeric(), -all_outcomes())
train_juiced <- prep(rec, training(df_split)) %>% juice()
logit_fit <- logistic_reg(mode = "classification") %>%
set_engine(engine = "glm") %>%
fit(y ~ ., data = train_juiced)
# Why is label a variable in the model ?
logit_fit[['fit']][['coefficients']]
#> (Intercept) x f_b f_c
#> -0.98950228 0.03734093 0.98945339 1.27014824
train_juiced
#> # A tibble: 35 x 4
#> x y f_b f_c
#> <dbl> <fct> <dbl> <dbl>
#> 1 -0.928 Y 1 0
#> 2 4.54 N 0 0
#> 3 -1.14 N 1 0
#> 4 -5.19 N 1 0
#> 5 -4.79 N 0 0
#> 6 -6.00 N 0 0
#> 7 3.83 N 0 1
#> 8 -8.66 Y 1 0
#> 9 -0.0849 Y 1 0
#> 10 -3.57 Y 0 1
#> # ... with 25 more rows
由 reprex package 创建于 2020-01-27 (v0.3.0)
好的,模型可以用了,但是我的标签
不见了。
我应该怎么做?
最佳答案
您遇到的主要问题/概念性问题是,一旦您juice()
配方,它就只是数据,即字面上只是一个数据框。当您使用它来拟合模型时,模型无法知道某些变量具有特殊作用。
library(tidymodels)
# label is an identifier variable to keep even though it's not a predictor
df <- tibble(label = 1:50,
x = rnorm(50, 0, 5),
f = factor(sample(c('a', 'b', 'c'), 50, replace = TRUE)),
y = factor(sample(c('Y', 'N'), 50, replace = TRUE)) )
df_split <- initial_split(df, prop = 0.70)
rec <- recipe(y ~ ., training(df_split)) %>%
update_role(label, new_role = "id variable") %>%
step_corr(all_numeric(), -all_outcomes()) %>%
step_dummy(all_predictors(),-all_numeric()) %>%
step_meanimpute(all_numeric(), -all_outcomes()) %>%
prep()
train_juiced <- juice(rec)
train_juiced
#> # A tibble: 35 x 5
#> label x y f_b f_c
#> <int> <dbl> <fct> <dbl> <dbl>
#> 1 1 1.80 N 1 0
#> 2 3 1.45 N 0 0
#> 3 5 -5.00 N 0 0
#> 4 6 -4.15 N 1 0
#> 5 7 1.37 Y 0 1
#> 6 8 1.62 Y 0 1
#> 7 10 -1.77 Y 1 0
#> 8 11 -3.15 N 0 1
#> 9 12 -2.02 Y 0 1
#> 10 13 2.65 Y 0 1
#> # … with 25 more rows
请注意,train_juiced
只是一个普通的小标题。如果您使用 fit()
在这个 tibble 上训练一个模型,它不会知道任何关于用于转换数据的方法。
tidymodels 框架确实有一种方法可以使用配方中的角色信息来训练模型。可能最简单的方法是使用 workflows .
logit_spec <- logistic_reg(mode = "classification") %>%
set_engine(engine = "glm")
wf <- workflow() %>%
add_model(logit_spec) %>%
add_recipe(rec)
logit_fit <- fit(wf, training(df_split))
# No more label in the model
logit_fit
#> ══ Workflow [trained] ══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
#> Preprocessor: Recipe
#> Model: logistic_reg()
#>
#> ── Preprocessor ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
#> 3 Recipe Steps
#>
#> ● step_corr()
#> ● step_dummy()
#> ● step_meanimpute()
#>
#> ── Model ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
#>
#> Call: stats::glm(formula = formula, family = stats::binomial, data = data)
#>
#> Coefficients:
#> (Intercept) x f_b f_c
#> 0.42331 -0.04234 -0.04991 0.64728
#>
#> Degrees of Freedom: 34 Total (i.e. Null); 31 Residual
#> Null Deviance: 45
#> Residual Deviance: 44.41 AIC: 52.41
由 reprex package 创建于 2020-02-15 (v0.3.0)
模型中没有更多标签!
关于r - 为什么 tidymodels/recipes 中的 "id variable"会起到预测作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59941616/
我有以下代码,我在其中对不同的 mtry 和 min_n 进行了一些网格搜索。我知道如何提取精度最高的参数(参见第二个代码框)。如何提取训练数据集中每个特征的重要性?我在网上找到的指南显示了如何使用“
我正在使用 tidymodels创建随机森林预测。我的测试数据包含训练数据中不存在的新因子水平,这会导致错误: 1: Novel levels found in column 'Siblings':
我最近一直在使用 tidymodels 来运行模型并选择最能满足某些目标函数的参数。例如对 mtcars 数据使用假设回归 ( using the regression examples from t
有没有办法在 tidy 模型中获取逻辑回归的标准误差和 p 值? 我可以通过下面的代码获得系数..但我想计算每个特征的优势比,我还需要标准误差.. glm.fit % set_engine(eng
数据集可以在这里找到: https://www.kaggle.com/mlg-ulb/creditcardfraud 我正在尝试使用 tidymodels 在此数据集上运行具有 5 折交叉验证的游侠。
通过 tidymodels 和 R 中的 vip 包,我计算了变量重要性。就代码而言,它看起来像这样: rf_vi_fit %>% pull_workflow_fit() %>% vip(geom =
我使用 tidymodels 在 R 中训练和测试了一个随机森林模型。现在我想使用相同的模型来预测一个全新的数据集(不是训练数据集)。 例如 Julia silge,在这篇博文中解释了训练、测试和评估
下面的代码工作正常,没有我所知道的错误,但我想添加更多内容。 我想补充的两件事是: 1 - 模型对训练数据对最终图的预测。我想在适合训练数据的模型上运行 collect_predictions()。
我尝试使用 tidymodels 来调整配方和模型参数的工作流程。调整单个工作流时没有问题。但是当使用多个工作流调整工作流集时,它总是失败。这是我的代码: # read the training da
我想使用 tidymodels 调整岭回归.我看过这个 nested sampling tutorial ,但不确定如何将调整从一个超参数增加到两个。请看下面的例子: 示例数据: library("m
我已经设法使用 tidymodels 构建了一个决策树模型。包,但我不确定如何提取结果并绘制树。我知道我可以使用 rpart和 rpart.plot包来实现同样的事情,但我宁愿使用 tidymodel
我想使用 recipes 创建一个食谱该软件包既可以估算缺失的数据,又可以添加指示哪些值缺失的指标列。如果有一个选项可以选择为原始数据框中的每一列包含一个指标列,或者只包含原始数据框中缺少数据的列的指
我真的很喜欢 tidymodels,但我不清楚我如何才能将该模型工作流适合嵌套分组依据之类的东西。例如,tidyr 在 mtcars 的圆柱体之类的东西上勾勒出一个简单的嵌套,然后为每个圆柱体拟合一个
我有以下代码用于使用 lightgbm 模型创建 tidymodels 工作流。但是,当我尝试保存到 .rds 对象并进行预测时出现了一些问题 library(AmesHousing) library
如何使用此 tidymodels 工作流程拟合模型? library(tidymodels) workflow() %>% add_model(linear_reg() %>% set_engin
我正在尝试对一个模型进行 k 折交叉验证,该模型根据卫星图像预测树种断面积比例的联合分布。这需要使用 DiricihletReg::DirichReg() 函数,这反过来又需要使用 Dirichlet
我可以在经典 Iris 数据集上应用 PCA 以获得每个维度的累积比例: library(tidyverse) x % as.matrix() pca % select(-Species) iris_
仍然习惯于 stackoverflow,所以如果发布不正确,我们深表歉意。 最近,我发现自己不得不运行许多预测变量略有不同的模型来衡量模型性能(我确信有一种更优雅的方法可以做到这一点),我正在考虑创建
我有一个二元分类问题,使用了随机森林和逻辑回归。根据 conf_mat、collect_metrics() 和 collect_predictions 的结果,我想更改我的模型,仅当模型“确定”时才分
我想对 LASSO 算法执行惩罚选择并使用 tidymodels 预测结果.我将使用波士顿住房数据集来说明这个问题。 library(tidymodels) library(tidyverse) li
我是一名优秀的程序员,十分优秀!