step_dummy_multi_choice() 创建配方步骤的规范,该步骤将多个标称数据(例如字符或因子)转换为原始数据级别的一个或多个数字二进制模型项。
用法
step_dummy_multi_choice(
recipe,
...,
role = "predictor",
trained = FALSE,
threshold = 0,
levels = NULL,
input = NULL,
other = "other",
naming = dummy_names,
prefix = NULL,
keep_original_cols = FALSE,
skip = FALSE,
id = rand_id("dummy_multi_choice")
)
参数
- recipe
-
一个菜谱对象。该步骤将添加到此配方的操作序列中。
- ...
-
一个或多个选择器函数用于为此步骤选择变量。有关更多详细信息,请参阅
selections()。所选变量必须是因子。 - role
-
对于此步骤创建的模型项,应为其分配什么分析角色?默认情况下,此步骤根据原始变量创建的新列将用作模型中的预测变量。
- trained
-
指示预处理数量是否已估计的逻辑。
- threshold
-
0 到 1 之间的数值,或者大于或等于 1 的整数。如果小于 1,则训练集中出现率低于
threshold的因子级别将被汇集到other。如果大于或等于 1,则该值被视为频率,出现次数少于threshold的因子水平将被汇集到other。 - levels
-
包含为
terms中包含的每个变量创建虚拟变量所需的信息的列表。在prep()训练该步骤之前,这是NULL。 - input
-
包含所用列名称的字符向量。在
prep()训练该步骤之前,这是NULL。 - other
-
"other" 类别的单个字符值。
- naming
-
定义新虚拟列的命名约定的函数。请参阅下面的详细信息。
- prefix
-
生成的新变量的前缀字符串。请参阅下面的注释。
- keep_original_cols
-
将原始变量保留在输出中的逻辑。默认为
FALSE。 - skip
-
一个合乎逻辑的。当
bake()烘焙食谱时是否应该跳过此步骤?虽然所有操作都是在prep()运行时烘焙的,但某些操作可能无法对新数据进行(例如处理结果变量)。使用skip = TRUE时应小心,因为它可能会影响后续操作的计算。 - id
-
该步骤特有的字符串,用于标识它。
细节
计算类别的总体比例(或总计数)。 "other" 类别用于代替训练集中个体比例(或频率)小于 threshold 的任何分类级别。
此配方步骤允许对结果变量进行灵活命名。对于名为 x 且级别为 "a" 和 "b" 的无序因子,默认命名约定是创建一个名为 x_b 的新变量。可以使用naming参数更改命名格式;函数 dummy_names() 是默认函数。
也可以看看
其他虚拟变量和编码步骤:step_bin2factor() , step_count() , step_date() , step_dummy_extract() , step_dummy() , step_factor2string() , step_holiday() , step_indicate_na() , step_integer() , step_novel() , step_num2factor() , step_ordinalscore() , step_other() , step_regex() , step_relevel() , step_string2factor() , step_time() , step_unknown() , step_unorder()
例子
library(tibble)
languages <- tribble(
~lang_1, ~lang_2, ~lang_3,
"English", "Italian", NA,
"Spanish", NA, "French",
"Armenian", "English", "French",
NA, NA, NA
)
dummy_multi_choice_rec <- recipe(~., data = languages) %>%
step_dummy_multi_choice(starts_with("lang")) %>%
prep()
bake(dummy_multi_choice_rec, new_data = NULL)
#> # A tibble: 4 × 5
#> lang_1_Armenian lang_1_English lang_1_French lang_1_Italian
#> <int> <int> <int> <int>
#> 1 0 1 0 1
#> 2 0 0 1 0
#> 3 1 1 1 0
#> 4 0 0 0 0
#> # ℹ 1 more variable: lang_1_Spanish <int>
tidy(dummy_multi_choice_rec, number = 1)
#> # A tibble: 5 × 3
#> terms columns id
#> <chr> <chr> <chr>
#> 1 lang_1 Armenian dummy_multi_choice_kJsyR
#> 2 lang_1 English dummy_multi_choice_kJsyR
#> 3 lang_1 French dummy_multi_choice_kJsyR
#> 4 lang_1 Italian dummy_multi_choice_kJsyR
#> 5 lang_1 Spanish dummy_multi_choice_kJsyR
dummy_multi_choice_rec2 <- recipe(~., data = languages) %>%
step_dummy_multi_choice(starts_with("lang"),
prefix = "lang",
threshold = 0.2
) %>%
prep()
bake(dummy_multi_choice_rec2, new_data = NULL)
#> # A tibble: 4 × 2
#> lang_English lang_other
#> <int> <int>
#> 1 1 1
#> 2 0 1
#> 3 1 1
#> 4 0 0
tidy(dummy_multi_choice_rec2, number = 1)
#> # A tibble: 2 × 3
#> terms columns id
#> <chr> <chr> <chr>
#> 1 lang_1 English dummy_multi_choice_Cb4Qi
#> 2 lang_1 other dummy_multi_choice_Cb4Qi
相关用法
- R recipes step_dummy_extract 从名义数据中提取模式
- R recipes step_dummy 创建传统的虚拟变量
- R recipes step_depth 数据深度
- R recipes step_discretize 离散数值变量
- R recipes step_date 日期特征生成器
- R recipes step_unknown 将缺失的类别分配给“未知”
- R recipes step_relu 应用(平滑)修正线性变换
- R recipes step_poly_bernstein 广义伯恩斯坦多项式基
- R recipes step_impute_knn 通过 k 最近邻进行插补
- R recipes step_impute_mean 使用平均值估算数值数据
- R recipes step_inverse 逆变换
- R recipes step_pls 偏最小二乘特征提取
- R recipes step_ratio 比率变量创建
- R recipes step_geodist 两个地点之间的距离
- R recipes step_nzv 近零方差滤波器
- R recipes step_nnmf 非负矩阵分解信号提取
- R recipes step_normalize 中心和比例数值数据
- R recipes step_other 折叠一些分类级别
- R recipes step_harmonic 添加正弦和余弦项以进行谐波分析
- R recipes step_corr 高相关滤波器
- R recipes step_novel 新因子水平的简单赋值
- R recipes step_select 使用 dplyr 选择变量
- R recipes step_regex 检测正则表达式
- R recipes step_spline_b 基础样条
- R recipes step_window 移动窗口函数
注:本文由纯净天空筛选整理自Max Kuhn等大神的英文原创作品 Handle levels in multiple predictors together。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
