對縮放後資料套用 KNN
在未縮放的 wine 資料集上,分類正確率還不錯。不過,我們來看看使用標準化後,你能達到什麼表現。和先前一樣,knn 模型以及 X 和 y 的資料與標籤集合都已經為你建立好了。
本練習屬於課程
Python 的 Machine Learning 前處理
練習說明
- 建立
StandardScaler(),並將其指定給變數scaler。 - 縮放訓練與測試特徵,注意不要造成資料外洩。
- 使用縮放後的訓練資料來訓練(fit)
knn模型。 - 以測試集的正確率來評估模型表現。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=42)
# Instantiate a StandardScaler
scaler = ____
# Scale the training and test features
X_train_scaled = ____.____(____)
X_test_scaled = ____.____(____)
# Fit the k-nearest neighbors model to the training data
____.____(____, ____)
# Score the model on the test data
print(____.____(____, ____))