查看邻居如何投票
当多个最近邻居进行投票时,查看投票是否一致或分歧较大常常很有价值。
例如,了解投票者对分类的置信度,可以让自动驾驶车辆在前方"有任何可能"是停车标志的情况下更加谨慎。
在本练习中,您将学习如何从 knn() 函数获取投票结果。
class 包已在您的工作区中加载,同时还有数据集 signs、sign_types 和 signs_test。
本练习是课程的一部分
R 中的监督学习:分类
练习说明
- 构建一个带有
prob = TRUE参数的 kNN 模型来计算投票占比。将k = 7。 - 使用
attr()函数获取预测类别的投票占比。该信息存储在属性"prob"中。 - 使用
head()函数查看前几条投票结果及百分比,了解不同路标之间置信度如何变化。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Use the prob parameter to get the proportion of votes for the winning class
sign_pred <- ___
# Get the "prob" attribute from the predicted classes
sign_prob <- attr(___, "___")
# Examine the first several predictions
head(___)
# Examine the proportion of votes for the winning class
head(___)