I’m trying to learn a little machine learning (and python), for now classifying with k-Nearest Neighbors. I get this error (what’s going on?):
dataSetSize = dataSet.shape[0]
TypeError: 'tuple' object is not callable
Checking for callable:
>>> callable(group)
False
>>> callable(labels)
False
>>> g = group()
They are false, but how to make them True?
The data:
>>> group
array([[ 1. , 1.1],
[ 1. , 1. ],
[ 0. , 0. ],
[ 0. , 0.1]])
>>> labels
['A', 'A', 'B', 'B']
When I do this on my group variable I get this:
group.shape[0]
4
My call to this function:
>>> 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]
Check using the built in
callablefunction to check if a object is callable :callabe(object)=> returnsTrueif object is callable