TypeError: get() takes no keyword arguments

问题描述

《机器学习实战》kNN算法中,

    for i in range(k):
        vote_label = distances_sorted_index[i]
        count[labels[vote_label]] = count.get(labels[vote_label], default=0) + 1

出现报错

Traceback (most recent call last):
  File "E:/workplace/python/code/test1.py", line 29, in 
    classify0(test_data, group, labels, k)
  File "E:/workplace/python/code/test1.py", line 20, in classify0
    count[labels[vote_label]] = count.get(labels[vote_label], default=0) + 1
TypeError: get() takes no keyword arguments

解决

count[labels[vote_label]] = count.get(labels[vote_label], 0) + 1

default并不是内置参数,文档中的参数只是作为参考,要按位置给参

参考

https://www.e-learn.cn/content/wangluowenzhang/152733

你可能感兴趣的:(错误集锦)