開始使用免費開始

用 vtreat 處理腳踏車租借資料

在這個練習中,你要為 7/8 月的腳踏車資料建立 one-hot 編碼的資料框,之後會搭配 xgboost 使用。

資料框 bikesJulybikesAugust 已經預先載入。

為了方便起見,我們已經替你定義了變數 vars,其中包含模型會用到的欄位名稱。

本練習屬於課程

R 中的監督式學習:回歸

檢視課程

練習說明

  • 載入套件 vtreat
  • 使用 designTreatmentsZ(),以 bikesJuly(訓練資料)與 vars 中的變數建立處理計畫 treatplan
    • 設定旗標 verbose=FALSE,避免函式印出過多訊息。
  • 補上空格,建立向量 newvars,只包含經過 cleanlev 轉換的變數名稱。並將它印出。
  • 使用 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
___
___
編輯並執行程式碼