我試圖了解如何申請step_pca預處理我的資料。假設我想為 iris 資料集構建一個 K-Nearest Neighbor 分類器。為了簡單起見,我不會將原始iris資料集拆分為訓練和測驗。我將假設iris是火車資料集,并且我有一些其他的觀察結果作為我的測驗資料集。
我想對我的火車資料集中的預測變數應用三個轉換:
- 居中所有預測變數
- 縮放所有預測變數
- PCA 轉換所有預測變數并保留其中一些至少可以解釋我的資料方差的 80%
所以這就是我所擁有的:
library(tidymodels)
iris_rec <-
recipe(Species ~ .,
data = iris) %>%
# center/scale
step_center(-Species) %>%
step_scale(-Species) %>%
# pca
step_pca(-Species, threshold = 0.8) %>%
# apply data transformation
prep()
iris_rec
#> Recipe
#>
#> Inputs:
#>
#> role #variables
#> outcome 1
#> predictor 4
#>
#> Training data contained 150 data points and no missing data.
#>
#> Operations:
#>
#> Centering for Sepal.Length, Sepal.Width, Petal.Length, Petal.... [trained]
#> Scaling for Sepal.Length, Sepal.Width, Petal.Length, Petal.... [trained]
#> PCA extraction with Sepal.Length, Sepal.Width, Petal.Length, Petal.W... [trained]
使用reprex v2.0.2創建于 2022-10-13
好的,到目前為止,一切都很好。所有轉換都應用于我的資料集。當我使用 準備我的火車資料集juice時,一切都按預期進行:
# transformed training set
iris_train_t <- juice(iris_rec)
iris_train_t
#> # A tibble: 150 × 3
#> Species PC1 PC2
#> <fct> <dbl> <dbl>
#> 1 setosa -2.26 -0.478
#> 2 setosa -2.07 0.672
#> 3 setosa -2.36 0.341
#> 4 setosa -2.29 0.595
#> 5 setosa -2.38 -0.645
#> 6 setosa -2.07 -1.48
#> 7 setosa -2.44 -0.0475
#> 8 setosa -2.23 -0.222
#> 9 setosa -2.33 1.11
#> 10 setosa -2.18 0.467
#> # … with 140 more rows
使用reprex v2.0.2創建于 2022-10-13
所以,我有兩個基于 PCA(PC1和PC2)的預測變數和我的回應變數。但是,當我繼續建模時,我收到一個錯誤:我測驗的所有模型都失敗了,如下所示:
# cross validation
set.seed(2022)
iris_train_cv <- vfold_cv(iris_train_t, v = 5)
# tuning
iris_knn_tune <-
nearest_neighbor(
neighbors = tune(),
weight_func = tune(),
dist_power = tune()
) %>%
set_engine("kknn") %>%
set_mode("classification")
# grid search
iris_knn_grid <-
grid_regular(neighbors(range = c(3, 9)),
weight_func(),
dist_power(),
levels = c(22, 2, 2))
# workflow creation
iris_wflow <-
workflow() %>%
add_recipe(iris_rec) %>%
add_model(iris_knn_tune)
# model assessment
iris_knn_fit_tune <-
iris_wflow %>%
tune_grid(
resamples = iris_train_cv,
grid = iris_knn_grid
)
#> x Fold1: preprocessor 1/1:
#> Error in `check_training_set()`:
#> ! Not all variables in the recipe are present in the supplied training...
#> x Fold2: preprocessor 1/1:
#> Error in `check_training_set()`:
#> ! Not all variables in the recipe are present in the supplied training...
#> x Fold3: preprocessor 1/1:
#> Error in `check_training_set()`:
#> ! Not all variables in the recipe are present in the supplied training...
#> x Fold4: preprocessor 1/1:
#> Error in `check_training_set()`:
#> ! Not all variables in the recipe are present in the supplied training...
#> x Fold5: preprocessor 1/1:
#> Error in `check_training_set()`:
#> ! Not all variables in the recipe are present in the supplied training...
#> Warning: All models failed. Run `show_notes(.Last.tune.result)` for more
#> information.
# cv results
collect_metrics(iris_knn_fit_tune)
#> Error in `estimate_tune_results()`:
#> ! All of the models failed. See the .notes column.
#> Backtrace:
#> ▆
#> 1. ├─tune::collect_metrics(iris_knn_fit_tune)
#> 2. └─tune:::collect_metrics.tune_results(iris_knn_fit_tune)
#> 3. └─tune::estimate_tune_results(x)
#> 4. └─rlang::abort("All of the models failed. See the .notes column.")
使用reprex v2.0.2創建于 2022-10-13
我懷疑我的問題出在我在iris_rec食譜上定義的公式上。那里的公式
Species ~ ., data = iris
意思是
Species ~ Sepal.Length Sepal.Width Petal.Length Petal.Width, data = iris
但是,當我運行我的模型時,預測變數是PC1和PC2,所以我猜公式應該是
Species ~ ., data = iris_train_t
或者
Species ~ PC1 PC2, data = iris_train_t
如何通知我的模型我的變數和資料集發生了變化?step_*我在我身上使用的所有其他人tidymodels都作業過,但我正在專門為step_pca.
uj5u.com熱心網友回復:
令人困惑的兩件事。
首先,在模型或作業流程中使用它之前,您不需要prep()或配方。juice()調整和重采樣功能將在每個 resample中執行此操作。
如果您希望處理訓練集以進行故障排除、可視化或以其他方式進行探索,prep()您可以。juice()但你不需要這樣做。
其次,配方基本上是配方的替代品。它知道預測變數和結果是什么,因此很少需要在此之上使用額外的公式。
(例外是需要特殊公式但不需要的模型)。
這是為您更新的代碼:
library(tidymodels)
iris_rec <-
recipe(Species ~ .,
data = iris) %>%
# center/scale
step_center(-Species) %>%
step_scale(-Species) %>%
# pca
step_pca(-Species, threshold = 0.8)
set.seed(2022)
iris_train_cv <- vfold_cv(iris, v = 5) #<- changes here
# tuning
iris_knn_tune <-
nearest_neighbor(
neighbors = tune(),
weight_func = tune(),
dist_power = tune()
) %>%
set_engine("kknn") %>%
set_mode("classification")
# grid search
iris_knn_grid <-
grid_regular(neighbors(range = c(3, 9)),
weight_func(),
dist_power(),
levels = c(22, 2, 2))
# workflow creation
iris_wflow <-
workflow() %>%
add_recipe(iris_rec) %>%
add_model(iris_knn_tune)
# model assessment
iris_knn_fit_tune <-
iris_wflow %>%
tune_grid(
resamples = iris_train_cv,
grid = iris_knn_grid
)
show_best(iris_knn_fit_tune, metric = "roc_auc")
#> # A tibble: 5 × 9
#> neighbors weight_func dist_power .metric .estima…1 mean n std_err .config
#> <int> <chr> <dbl> <chr> <chr> <dbl> <int> <dbl> <chr>
#> 1 9 rectangular 1 roc_auc hand_till 0.976 5 0.00580 Prepro…
#> 2 7 triangular 1 roc_auc hand_till 0.975 5 0.00688 Prepro…
#> 3 9 triangular 2 roc_auc hand_till 0.975 5 0.00571 Prepro…
#> 4 8 triangular 1 roc_auc hand_till 0.975 5 0.00655 Prepro…
#> 5 9 triangular 1 roc_auc hand_till 0.975 5 0.00655 Prepro…
#> # … with abbreviated variable name 1?.estimator
使用reprex v2.0.2創建于 2022-10-13
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/516944.html
標籤:r机器学习整洁的模型
上一篇:如何列出AzureML資料集中所有可用的資料集版本,并獲取資料集最新版本之前的版本
下一篇:如何用陣列索引串列
