以 K-nearest neighbors 分類蘑菇
Gaussian Naive Bayes 分類器作為初始模型表現相當不錯。現在我們來建立一個新模型,和 Naive Bayes 做比較。
這次要使用的是 5-nearest neighbors 分類器。由於經過 dummy 特徵後資料集的維度很高,請使用 Ball Tree 演算法來加速模型。一起看看這個模型的表現如何!
本練習屬於課程
Python 的 Ensemble 方法
練習說明
- 建立一個
KNeighborsClassifier,設定5個鄰居,並使用algorithm = 'ball_tree'(用來加速處理)。 - 將模型擬合到訓練資料。
- 使用 accuracy 分數在測試集上評估效能。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Instantiate a 5-nearest neighbors classifier with 'ball_tree' algorithm
clf_knn = ____(____, ____)
# Fit the model to the training set
____
# Calculate the predictions on the test set
pred = ____
# Evaluate the performance using the accuracy score
print("Accuracy: {:0.4f}".format(accuracy_score(y_test, pred)))