剖析最佳的航班時長模型
你剛剛設定了 CrossValidator,要為預測航班時長的線性迴歸模型找出合適的參數。
這個模型的 pipeline 有多個階段(StringIndexer、OneHotEncoder、VectorAssembler 與 LinearRegression 類型的物件),會依序運作。這些階段可從 pipeline 物件的 stages 屬性取得。它們以清單呈現,並會按照清單中的出現順序執行。
現在你將更仔細地檢視這個 pipeline,拆解各個階段,並用它在測試資料上進行預測。
以下物件已經建立:
cv— 已訓練好的CrossValidatorModel物件,及evaluator—RegressionEvaluator物件。
航班資料已隨機切分為 flights_train 與 flights_test。
本練習屬於課程
使用 PySpark 的機器學習
練習說明
- 取回最佳模型。
- 檢視最佳模型中的各個階段。
- 抽出線性迴歸階段並擷取其參數。
- 使用最佳模型在測試資料上產生預測並計算 RMSE。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Get the best model from cross validation
best_model = cv.____
# Look at the stages in the best model
print(best_model.____)
# Get the parameters for the LinearRegression object in the best model
best_model.____.extractParamMap()
# Generate predictions on testing data using the best model then calculate RMSE
predictions = ____.____(____)
print("RMSE =", ____.____(____))