對航班時間模型的管線進行交叉驗證
你剛建立的交叉驗證模型很簡單,只用 km 來預測 duration。
另一個影響航班時間的重要預測因子是起飛機場。從繁忙機場起飛的航班通常需要更久才能升空。來看看把這個預測因子加進模型是否能提升表現!
在這個練習中,你會把 org 欄位加進模型。不過因為 org 是類別型變數,納入前還需要多做一些處理:必須先轉成索引,再做 one-hot 編碼,最後與 km 一起組裝,才能用來建立迴歸模型。我們會把這些步驟包成一個 pipeline。
以下物件已經建立好:
params—— 空的參數格網evaluator—— 迴歸評估器regression—— 一個LinearRegression物件,且labelCol='duration'。
StringIndexer、OneHotEncoder、VectorAssembler 與 CrossValidator 類別也都已經匯入。
本練習屬於課程
使用 PySpark 的機器學習
練習說明
- 建立字串索引器(string indexer)。將輸入與輸出欄位分別指定為
org與org_idx。 - 建立 one-hot 編碼器(one-hot encoder)。將輸出欄位命名為
org_dummy。 - 將
km與org_dummy這兩個欄位組裝成名為features的單一欄位。 - 使用以下步驟建立 pipeline:字串索引器、one-hot 編碼器、組裝器與線性迴歸。用這個 pipeline 來建立交叉驗證器。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create an indexer for the org field
indexer = ____(____, ____)
# Create an one-hot encoder for the indexed org field
onehot = ____(____, ____)
# Assemble the km and one-hot encoded fields
assembler = ____(____, ____)
# Create a pipeline and cross-validator.
pipeline = ____(stages=[____, ____, ____, ____])
cv = ____(estimator=____,
estimatorParamMaps=____,
evaluator=____)