step_impute_median()
創建配方步驟的規範,該步驟將用這些變量的訓練集中值替換數值變量的缺失值。
用法
step_impute_median(
recipe,
...,
role = NA,
trained = FALSE,
medians = NULL,
skip = FALSE,
id = rand_id("impute_median")
)
step_medianimpute(
recipe,
...,
role = NA,
trained = FALSE,
medians = NULL,
skip = FALSE,
id = rand_id("impute_median")
)
參數
- recipe
-
一個菜譜對象。該步驟將添加到此配方的操作序列中。
- ...
-
一個或多個選擇器函數用於為此步驟選擇變量。有關更多詳細信息,請參閱
selections()
。 - role
-
由於沒有創建新變量,因此此步驟未使用。
- trained
-
指示預處理數量是否已估計的邏輯。
- medians
-
一個命名的中位數數值向量。在由
prep()
計算之前,這是NULL
。請注意,如果原始數據是整數,則中位數將轉換為整數以保持相同的數據類型。 - skip
-
一個合乎邏輯的。當
bake()
烘焙食譜時是否應該跳過此步驟?雖然所有操作都是在prep()
運行時烘焙的,但某些操作可能無法對新數據進行(例如處理結果變量)。使用skip = TRUE
時應小心,因為它可能會影響後續操作的計算。 - id
-
該步驟特有的字符串,用於標識它。
細節
step_impute_median
根據 prep.recipe
的 training
參數中使用的數據估計變量中位數。 bake.recipe
然後使用這些中位數將新值應用於新數據集。
從 recipes
0.1.16 開始,該函數名稱從 step_medianimpute()
更改為 step_impute_median()
。
整理
當您 tidy()
此步驟時,將返回包含列 terms
(選定的選擇器或變量)和 model
(中值)的小標題。
箱重
此步驟執行可以利用案例權重的無監督操作。因此,個案權重僅與頻率權重一起使用。有關更多信息,請參閱 case_weights 中的文檔和 tidymodels.org
中的示例。
也可以看看
其他插補步驟: step_impute_bag()
、 step_impute_knn()
、 step_impute_linear()
、 step_impute_lower()
、 step_impute_mean()
、 step_impute_mode()
、 step_impute_roll()
例子
data("credit_data", package = "modeldata")
## missing data per column
vapply(credit_data, function(x) mean(is.na(x)), c(num = 0))
#> Status Seniority Home Time Age
#> 0.0000000000 0.0000000000 0.0013471037 0.0000000000 0.0000000000
#> Marital Records Job Expenses Income
#> 0.0002245173 0.0000000000 0.0004490346 0.0000000000 0.0855410867
#> Assets Debt Amount Price
#> 0.0105523125 0.0040413112 0.0000000000 0.0000000000
set.seed(342)
in_training <- sample(1:nrow(credit_data), 2000)
credit_tr <- credit_data[in_training, ]
credit_te <- credit_data[-in_training, ]
missing_examples <- c(14, 394, 565)
rec <- recipe(Price ~ ., data = credit_tr)
impute_rec <- rec %>%
step_impute_median(Income, Assets, Debt)
imp_models <- prep(impute_rec, training = credit_tr)
imputed_te <- bake(imp_models, new_data = credit_te, everything())
credit_te[missing_examples, ]
#> Status Seniority Home Time Age Marital Records Job Expenses
#> 28 good 15 owner 36 43 married no fixed 75
#> 688 good 2 rent 60 32 married no partime 87
#> 1002 good 21 rent 60 39 married no fixed 124
#> Income Assets Debt Amount Price
#> 28 251 4000 0 1800 2557
#> 688 115 2000 0 1250 1517
#> 1002 191 2000 0 2000 2536
imputed_te[missing_examples, names(credit_te)]
#> # A tibble: 3 × 14
#> Status Seniority Home Time Age Marital Records Job Expenses Income
#> <fct> <int> <fct> <int> <int> <fct> <fct> <fct> <int> <int>
#> 1 good 15 owner 36 43 married no fixed 75 251
#> 2 good 2 rent 60 32 married no part… 87 115
#> 3 good 21 rent 60 39 married no fixed 124 191
#> # ℹ 4 more variables: Assets <int>, Debt <int>, Amount <int>, Price <int>
tidy(impute_rec, number = 1)
#> # A tibble: 3 × 3
#> terms value id
#> <chr> <dbl> <chr>
#> 1 Income NA impute_median_Hlm2y
#> 2 Assets NA impute_median_Hlm2y
#> 3 Debt NA impute_median_Hlm2y
tidy(imp_models, number = 1)
#> # A tibble: 3 × 3
#> terms value id
#> <chr> <dbl> <chr>
#> 1 Income 125 impute_median_Hlm2y
#> 2 Assets 3000 impute_median_Hlm2y
#> 3 Debt 0 impute_median_Hlm2y
相關用法
- R recipes step_impute_mean 使用平均值估算數值數據
- R recipes step_impute_mode 使用最常見的值估算名義數據
- R recipes step_impute_knn 通過 k 最近鄰進行插補
- R recipes step_impute_roll 使用滾動窗口統計估算數值數據
- R recipes step_impute_lower 估算低於測量閾值的數值數據
- R recipes step_impute_bag 通過袋裝樹進行插補
- R recipes step_impute_linear 通過線性模型估算數值變量
- R recipes step_inverse 逆變換
- R recipes step_ica ICA 信號提取
- R recipes step_indicate_na 創建缺失數據列指示器
- R recipes step_integer 將值轉換為預定義的整數
- R recipes step_intercept 添加截距(或常數)列
- R recipes step_interact 創建交互變量
- R recipes step_invlogit 逆 Logit 變換
- R recipes step_isomap 等位圖嵌入
- R recipes step_unknown 將缺失的類別分配給“未知”
- R recipes step_relu 應用(平滑)修正線性變換
- R recipes step_poly_bernstein 廣義伯恩斯坦多項式基
- 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等大神的英文原創作品 Impute numeric data using the median。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。