欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TypeError: ‘tuple‘ object is not callable

程序员文章站 2022-05-29 08:04:18
...

复现机器学习KNN出现的错误。

def createDataset():
    group = array([[1.0, 1.1],[1.0, 1.0], [0, 0], [0, 0.1]])
    labels = ['A','A','B','B']
    return group, labels

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
    sortedDistIndicies = distances.argsort()
    classCount={}
    for i in range(k):
        voteIlabel = labels[sortedDistIndicies[i]]
        classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1
    sortedClassCount = sorted(classCount.iteritems(),key = operator.itemgetter(1),reverse=True
    )
    return sortedClassCount[0][0]在这里插入代码片

第一个错误
TypeError: ‘tuple’ object is not callable
将下列代码

   dataSetSize = dataSet.shape(0)

改成`

   dataSetSize = dataSet.shape[0]

对于数据特征的引用一般都用[]

data.feature[index]

第二个错误:
AttributeError: ‘dict’ object has no attribute ‘iteritems’

 sortedClassCount = sorted(classCount.items(),key = operator.itemgetter(1),reverse=True

中iteritems改成items

相关标签: 机器学习