用於歌曲曲風預測的 Pipeline:II
你已在前一個練習設定好 pipeline 的步驟,現在要在 music_df 資料集上使用它來分類歌曲的曲風。Pipeline 的強大之處在於它提供了非常簡潔的一致介面。
X_train、X_test、y_train 和 y_test 已為你預先載入,且已從 sklearn.metrics 匯入 confusion_matrix。
本練習屬於課程
使用 scikit-learn 進行監督式學習
練習說明
- 使用你先前定義的步驟建立一個 pipeline。
- 將 pipeline 擬合到訓練資料。
- 在測試集上進行預測。
- 計算並列印混淆矩陣。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
steps = [("imputer", imp_mean),
("knn", knn)]
# Create the pipeline
pipeline = ____(____)
# Fit the pipeline to the training data
____
# Make predictions on the test set
y_pred = ____
# Print the confusion matrix
print(____(____, ____))