开始使用免费开始使用

用于蘑菇数据的 K 最近邻

作为初始模型,高斯朴素贝叶斯分类器表现非常不错。现在我们构建一个新模型,与朴素贝叶斯进行对比。

这里将使用 5-近邻分类器。由于哑变量会产生高维数据集,请使用 Ball Tree 算法来加速模型。让我们看看这个模型的效果如何!

本练习是课程的一部分

Python 中的集成方法

查看课程

练习说明

  • 构建一个具有 5 个邻居且 algorithm = 'ball_tree'KNeighborsClassifier(用于加速处理)。
  • 将模型拟合到训练数据。
  • 使用准确率在测试集上评估模型表现。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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)))
编辑并运行代码