python - TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('S32') dtype('S32') dtype('S32') -
i know question has been asked many times have seen corresponding answers it, still unable rectify error.
from numpy import * import operator import sys def createdataset(): group = array([ [1.0, 2.0], [1.0,4.0], [4.0, 1.0], [4.0, 2.0] ]) labels = ['action', 'action', 'romantic', 'romantic'] return group, labels def calcdistance(inx, dataset, labels, k): datasetsize = dataset.shape[0] print datasetsize mat = tile(inx, (datasetsize, 1)) - dataset sqdiffmat = mat ** 2 sqdistances = sqdiffmat.sum(axis=1) distances = sqdistances ** 0.5 sorteddistindices = distances.argsort() return sorteddistindices def findmajorityclass(inx, dataset, labels, k, sorteddistindices): classcount = {} in range(k): voteilabel = labels[sorteddistindices[i]] classcount[voteilabel] = classcount.get(voteilabel, 0) + 1 return sorted(classcount.iteritems(), key=operator.itemgetter(1), reverse=true) def classify0(inx, dataset, labels, k): sorteddistindices = calcdistance(inx, dataset, labels, k) sortedclasscount = findmajorityclass(inx, dataset, labels, k,sorteddistindices) return sortedclasscount[0][0] line in sys.stdin: line = line.strip() line = line.split('\t') key1,key2 = line group, labels = createdataset() result = classify0([key1,key2], group, labels,3) print "%s\t%s\t%s" %(key1,key2,result)
error :
traceback (most recent call last): file "./reducer_knn.py", line 71, in <module> result = classify([key1,key2], group, labels, 3) file "./reducer_knn.py", line 59, in classify sorteddistindices = calcdistance(inx, dataset, labels, k) file "./reducer_knn.py", line 21, in calcdistance sqrt = [(((dataset[i]-inx) ** 2).sum(axis=0) ** 0.5) in range(datasetsize)] typeerror: ufunc 'subtract' did not contain loop signature matching types dtype('s32') dtype('s32') dtype('s32')
Comments
Post a Comment