step_string2factor() 會將一個或多個字符向量轉換為因子(有序或無序)。
僅在特殊情況下使用此步驟(請參閱詳細信息),並在使用任何 tidymodels 函數之前將字符串轉換為因子。
用法
step_string2factor(
recipe,
...,
role = NA,
trained = FALSE,
levels = NULL,
ordered = FALSE,
skip = FALSE,
id = rand_id("string2factor")
)
參數
- recipe
-
一個菜譜對象。該步驟將添加到此配方的操作序列中。
- ...
-
一個或多個選擇器函數用於為此步驟選擇變量。有關更多詳細信息,請參閱
selections()。 - role
-
由於沒有創建新變量,因此此步驟未使用。
- trained
-
指示預處理數量是否已估計的邏輯。
- levels
-
用於新因子的級別的選項規範。如果保留
NULL,則將使用調用bake時出現的排序唯一值。 - ordered
-
單個邏輯值;應該對因子進行排序嗎?
- skip
-
一個合乎邏輯的。當
bake()烘焙食譜時是否應該跳過此步驟?雖然所有操作都是在prep()運行時烘焙的,但某些操作可能無法對新數據進行(例如處理結果變量)。使用skip = TRUE時應小心,因為它可能會影響後續操作的計算。 - id
-
該步驟特有的字符串,用於標識它。
細節
什麽時候應該使用此步驟?
在大多數情況下,如果您計劃使用 step_string2factor() 而不設置 levels ,則最好在使用配方之前將這些字符變量轉換為因子變量。
這可以使用以下方法完成dplyr用下麵的代碼
df <- mutate(df, across(where(is.character), as.factor))
在重采樣期間,完整的值集可能不在字符數據中。然後使用 step_string2factor() 將它們轉換為因子將導致級別配置錯誤。
如果使用 step_string2factor() 的 levels 參數,它將把受此步驟影響的所有變量轉換為具有相同的級別。因此,在定義配方時,您需要了解完整的級別集。
另請注意, prep() 有一個選項 strings_as_factors,默認為 TRUE 。應更改此設置,以便將原始字符數據應用於 step_string2factor() 。但是,此步驟也可以采用現有因子(但將保持原樣)。
整理
當您 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_num2factor() , step_ordinalscore() , step_other() , step_regex() , step_relevel() , step_time() , step_unknown() , step_unorder()
例子
data(Sacramento, package = "modeldata")
# convert factor to string to demonstrate
Sacramento$city <- as.character(Sacramento$city)
rec <- recipe(~ city + zip, data = Sacramento)
make_factor <- rec %>%
step_string2factor(city)
make_factor <- prep(make_factor,
training = Sacramento
)
make_factor
#>
#> ── Recipe ────────────────────────────────────────────────────────────────
#>
#> ── Inputs
#> Number of variables by role
#> predictor: 2
#>
#> ── Training information
#> Training data contained 932 data points and no incomplete rows.
#>
#> ── Operations
#> • Factor variables from: city | Trained
# note that `city` is a factor in recipe output
bake(make_factor, new_data = NULL) %>% head()
#> # A tibble: 6 × 2
#> city zip
#> <fct> <fct>
#> 1 SACRAMENTO z95838
#> 2 SACRAMENTO z95823
#> 3 SACRAMENTO z95815
#> 4 SACRAMENTO z95815
#> 5 SACRAMENTO z95824
#> 6 SACRAMENTO z95841
# ...but remains a string in the data
Sacramento %>% head()
#> # A tibble: 6 × 9
#> city zip beds baths sqft type price latitude longitude
#> <chr> <fct> <int> <dbl> <int> <fct> <int> <dbl> <dbl>
#> 1 SACRAMENTO z95838 2 1 836 Residential 59222 38.6 -121.
#> 2 SACRAMENTO z95823 3 1 1167 Residential 68212 38.5 -121.
#> 3 SACRAMENTO z95815 2 1 796 Residential 68880 38.6 -121.
#> 4 SACRAMENTO z95815 2 1 852 Residential 69307 38.6 -121.
#> 5 SACRAMENTO z95824 2 1 797 Residential 81900 38.5 -121.
#> 6 SACRAMENTO z95841 3 1 1122 Condo 89921 38.7 -121.
相關用法
- R recipes step_select 使用 dplyr 選擇變量
- R recipes step_spline_b 基礎樣條
- R recipes step_shuffle 隨機排列變量
- R recipes step_scale 縮放數值數據
- R recipes step_spline_nonnegative 非負樣條
- R recipes step_sample 使用 dplyr 的示例行
- R recipes step_spline_natural 自然樣條
- R recipes step_spline_convex 凸樣條
- R recipes step_slice 使用 dplyr 按位置過濾行
- R recipes step_sqrt 平方根變換
- R recipes step_spline_monotone 單調樣條
- R recipes step_spatialsign 空間符號預處理
- 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_depth 數據深度
注:本文由純淨天空篩選整理自Max Kuhn等大神的英文原創作品 Convert Strings to Factors。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
