用於分類的中心化與縮放
現在你要把特徵縮放與模型建置結合成一個可用於交叉驗證的 pipeline。
你的任務是為 music_df 資料集建立一個 pipeline,先對特徵做縮放,然後使用邏輯斯迴歸模型,針對超參數 C 的不同數值進行格網搜尋交叉驗證。此處的目標變數是 "genre",其中搖滾標記為 1,其他任何曲風為 0。
StandardScaler、LogisticRegression 和 GridSearchCV 都已經為你匯入。
本練習屬於課程
使用 scikit-learn 進行監督式學習
練習說明
- 建立 pipeline 的步驟:一個命名為
"scaler"的StandardScaler()物件,以及一個命名為"logreg"的邏輯斯迴歸模型。 - 建立
parameters:在 pipeline 中,對邏輯斯迴歸模型的C超參數搜尋從0.001到1.0、等距分成 20 個的浮點數值。 - 具現化格網搜尋物件。
- 將格網搜尋物件擬合至訓練資料。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Build the steps
steps = [("____", ____()),
("____", ____())]
pipeline = Pipeline(steps)
# Create the parameter space
parameters = {"____": np.____(____, ____, 20)}
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=21)
# Instantiate the grid search object
cv = ____(____, param_grid=____)
# Fit to the training data
cv.____(____, ____)
print(cv.best_score_, "\n", cv.best_params_)