菜菜鸟的《机器学习实战》学习笔记——kNN算法代码01
网上的有关资料很多*3,作为一只菜菜,仅把我自己比较困惑的地方及解释逐条列明:
- for i in range(k):
# python中数字区间的取值方式都是:左闭 右开!,当k=3时,会取到0,1,2三个数,所以下一条命令的voteIlabel会取前三位 # - classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1
# get()给classCount[voteIlabel] 赋值了 # - sortedClassCount = sorted(classCount.items(), key=operator.itemgetter(1), reverse=True)中的
classCount.items() # 显示字典的所有键值对 # key=operator.itemgetter(1) # 以元组的第1位作为索引 # - sortedClassCount[0][0]
# 第1个[0] 第0位的元组,
第2个[0] 第0位的元组的第0位值 #

8122

被折叠的 条评论
为什么被折叠?



