这是k近邻算法的代码:

import numpy as np

from EuclideanDistance import EuclideanDistance

dataset = np.loadtxt('C:\Users\Toshiba\Documents\machine learning\RealEstate.csv',      delimiter=',', usecols=(2,3,4,5))
p1 = ()

def normalizeToZscores(data):
    '''Normalizes the variables to z-scores'''
    zScores = list()
for s in data:
    zScore = (s - np.mean(data))/np.std(data)
    zScores.append(zScore)
    return np.asarray(zScores)

def InOutBudget(data):
    '''Decides whether a particular house is within
    or outside the budget of $153000 and assigns values of 1 and 0 respectively'''
    data2 = list()
    for i in data:
        if (i > 153000): data2.append(0)
        else: data2.append(1)
    return np.array(data2)

classes = dataset[:,0]
classes = classes.reshape((dataset.shape[0],1))
classes = InOutBudget(classes)

data = dataset[:20,:]
data = normalizeToZscores(data)

p1s = dataset[20:400,:]

def measureDis(data, p1):
    listD = []
    for x in data:
    D = EuclideanDistance(x, p1)
    listD.append(D)
return listD




def most_common(lst):
    '''Finds the most frequently occuring element of a list.
    It will be used to predict a class based on the classification
    of the k-nearest neighbours'''
    return max(set(lst), key=lst.count)

def findKnn(k):
    '''K nearest neighbours algorithm'''
    knns = list()
    errors = list()
    #for i in k:
    for p1 in p1s:
        # Create a list of tuples containing distance and class,
        # Then sort them by shortest distance
        tuples = zip(measureDis(data,p1), classes[20:400])
        tuples = sorted(tuples)
        knn = tuples[:k]
        print knn
        knn = [x[1] for x in knn]
        knn = most_common(knn)
        knns = knns.append(knn)
        print knn
        error = np.abs(knn - p1)
        errors = errors.append(error)
    errorsNum = np.sum(errors)

    return knns

但我一直在想:
Traceback (most recent call last):
File "C:\Users\Toshiba\workspace\assignment5\src\knn2.py", line 76, in <module> knn = findKnn(k)
File "C:\Users\Toshiba\workspace\assignment5\src\knn2.py", line 64, in findKnn knns = knns.append(knn)
AttributeError: 'NoneType' object has no attribute 'append'

我知道代码是业余的,但是有人能帮我解决这个问题吗?

最佳答案

list.append不返回列表。简单地做:

knns.append(knn)

而不是:
knns = knns.append(knn)

10-06 05:18