我正在尝试学习一点机器学习(和 python),现在用 k-Nearest Neighbors 进行分类。我收到此错误(发生了什么?):
dataSetSize = dataSet.shape[0]
TypeError: 'tuple' object is not callable
检查可调用:
>>> callable(group)
False
>>> callable(labels)
False
>>> g = group()
它们是假的,但如何使它们成为真呢?
数据:
>>> group
array([[ 1. , 1.1],
[ 1. , 1. ],
[ 0. , 0. ],
[ 0. , 0.1]])
>>> labels
['A', 'A', 'B', 'B']
当我对我的组变量执行此操作时,我得到以下信息:
group.shape[0]
4
我对这个函数的调用:
>>> kNN.classify0([0, 0], group, labels, 3)
def classify0(inX, dataSet, labels, k):
dataSetSize = dataSet.shape[0]
diffMat = tile(inX, (dataSetSize,1)) - dataSet
sqDiffMat = diffMat**2
sqDistances = sqDiffMat.sum(axis=1)
distances = sqDistances**0.5
sortedDistIndices = distances.argsort()
classCount = {}
for i in range(k):
voteIlabel = labels[sortedDistIndices[i]]
classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1
sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse=True)
return sortedClassCount[0][0]