step_num2factor() 會將一個或多個數值向量轉換為因子(有序或無序)。當類別被編碼為整數時,這非常有用。
用法
step_num2factor(
recipe,
...,
role = NA,
transform = function(x) x,
trained = FALSE,
levels,
ordered = FALSE,
skip = FALSE,
id = rand_id("num2factor")
)
參數
- recipe
-
一個菜譜對象。該步驟將添加到此配方的操作序列中。
- ...
-
一個或多個選擇器函數用於為此步驟選擇變量。有關更多詳細信息,請參閱
selections()。 - role
-
由於沒有創建新變量,因此此步驟未使用。
- transform
-
采用單個參數
x的函數,可用於在確定級別之前修改數值(可能使用base::as.integer())。函數的輸出應該是一個整數,對應於應分配的levels的值。如果不是整數,該值將在bake()期間轉換為整數。 - trained
-
指示預處理數量是否已估計的邏輯。
- levels
-
將用作級別的值的字符向量。這些是轉換為字符並排序的數字數據。一旦執行
prep(),就會修改此值。 - ordered
-
單個邏輯值;應該對因子進行排序嗎?
- skip
-
一個合乎邏輯的。當
bake()烘焙食譜時是否應該跳過此步驟?雖然所有操作都是在prep()運行時烘焙的,但某些操作可能無法對新數據進行(例如處理結果變量)。使用skip = TRUE時應小心,因為它可能會影響後續操作的計算。 - id
-
該步驟特有的字符串,用於標識它。
整理
當您 tidy() 此步驟時,將返回包含列 terms(選定的選擇器或變量)和 ordered 的 tibble。
也可以看看
其他虛擬變量和編碼步驟:step_bin2factor() , step_count() , step_date() , step_dummy_extract() , step_dummy_multi_choice() , step_dummy() , step_factor2string() , step_holiday() , step_indicate_na() , step_integer() , step_novel() , step_ordinalscore() , step_other() , step_regex() , step_relevel() , step_string2factor() , step_time() , step_unknown() , step_unorder()
例子
library(dplyr)
data(attrition, package = "modeldata")
attrition %>%
group_by(StockOptionLevel) %>%
count()
#> # A tibble: 4 × 2
#> # Groups: StockOptionLevel [4]
#> StockOptionLevel n
#> <int> <int>
#> 1 0 631
#> 2 1 596
#> 3 2 158
#> 4 3 85
amnt <- c("nothin", "meh", "some", "copious")
rec <-
recipe(Attrition ~ StockOptionLevel, data = attrition) %>%
step_num2factor(
StockOptionLevel,
transform = function(x) x + 1,
levels = amnt
)
encoded <- rec %>%
prep() %>%
bake(new_data = NULL)
table(encoded$StockOptionLevel, attrition$StockOptionLevel)
#>
#> 0 1 2 3
#> nothin 631 0 0 0
#> meh 0 596 0 0
#> some 0 0 158 0
#> copious 0 0 0 85
# an example for binning
binner <- function(x) {
x <- cut(x, breaks = 1000 * c(0, 5, 10, 20), include.lowest = TRUE)
# now return the group number
as.numeric(x)
}
inc <- c("low", "med", "high")
rec <-
recipe(Attrition ~ MonthlyIncome, data = attrition) %>%
step_num2factor(
MonthlyIncome,
transform = binner,
levels = inc,
ordered = TRUE
) %>%
prep()
encoded <- bake(rec, new_data = NULL)
table(encoded$MonthlyIncome, binner(attrition$MonthlyIncome))
#>
#> 1 2 3
#> low 749 0 0
#> med 0 440 0
#> high 0 0 281
# What happens when a value is out of range?
ceo <- attrition %>%
slice(1) %>%
mutate(MonthlyIncome = 10^10)
bake(rec, ceo)
#> # A tibble: 1 × 2
#> MonthlyIncome Attrition
#> <ord> <fct>
#> 1 NA Yes
相關用法
- R recipes step_nzv 近零方差濾波器
- R recipes step_nnmf 非負矩陣分解信號提取
- R recipes step_normalize 中心和比例數值數據
- R recipes step_novel 新因子水平的簡單賦值
- R recipes step_ns 自然樣條基函數
- R recipes step_nnmf_sparse 帶套索懲罰的非負矩陣分解信號提取
- R recipes step_naomit 刪除缺失值的觀測值
- 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_depth 數據深度
- R recipes step_other 折疊一些分類級別
- R recipes step_harmonic 添加正弦和餘弦項以進行諧波分析
- R recipes step_corr 高相關濾波器
- R recipes step_select 使用 dplyr 選擇變量
- R recipes step_regex 檢測正則表達式
- R recipes step_spline_b 基礎樣條
- R recipes step_window 移動窗口函數
- R recipes step_ica ICA 信號提取
注:本文由純淨天空篩選整理自Max Kuhn等大神的英文原創作品 Convert Numbers to Factors。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
