問題描述
《機器學習實戰》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 <module>
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並不是內置參數,文檔中的參數只是作爲參考,要按位置給參