用 vtreat 處理腳踏車租借資料
在這個練習中,你要為 7/8 月的腳踏車資料建立 one-hot 編碼的資料框,之後會搭配 xgboost 使用。
資料框 bikesJuly 和 bikesAugust 已經預先載入。
為了方便起見,我們已經替你定義了變數 vars,其中包含模型會用到的欄位名稱。
本練習屬於課程
R 中的監督式學習:回歸
練習說明
- 載入套件
vtreat。 - 使用
designTreatmentsZ(),以bikesJuly(訓練資料)與vars中的變數建立處理計畫treatplan。- 設定旗標
verbose=FALSE,避免函式印出過多訊息。
- 設定旗標
- 補上空格,建立向量
newvars,只包含經過clean與lev轉換的變數名稱。並將它印出。 - 使用
prepare()建立 one-hot 編碼的訓練資料框bikesJuly.treat。- 透過
varRestrictions引數,將使用的變數限制為newvars。
- 透過
- 以相同方式,使用
prepare()從bikesAugust建立 one-hot 編碼的測試資料框bikesAugust.treat。 - 對兩個處理後的資料框都呼叫
str(),檢視其結構。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# The outcome column
(outcome <- "cnt")
# The input columns
(vars <- c("hr", "holiday", "workingday", "weathersit", "temp", "atemp", "hum", "windspeed"))
# Load the package vtreat
___
# Create the treatment plan from bikesJuly (the training data)
treatplan <- ___(___, ___, verbose = FALSE)
# Get the "clean" and "lev" variables from the scoreFrame
(newvars <- treatplan %>%
use_series(scoreFrame) %>%
filter(code %in% ___) %>% # get the rows you care about
use_series(___)) # get the varName column
# Prepare the training data
bikesJuly.treat <- ___(___, ___, varRestriction = ___)
# Prepare the test data
bikesAugust.treat <- ___(___, ___, varRestriction = ___)
# Call str() on the treated data
___
___