3

this is my code for the k nearest neighbor algorithm:

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

But I keep getting:

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'

I know the code is really amateur, but could someone please help me just solve the issue?

2
  • 2
    Are you doing this because you want to implement the algorithm, learn how it works, etc? Or do you need a nearest neighbor search for a larger project? If you just need a nearest neighbor search, take a look at scipy.spatial.KDTree Commented Nov 18, 2013 at 15:59
  • @Elliott scipy.spatial.cKDTree is also there... Commented Nov 18, 2013 at 16:02

2 Answers 2

6

list.append doesn't return the list. Simply do:

knns.append(knn)

instead of:

knns = knns.append(knn)
Sign up to request clarification or add additional context in comments.

Comments

5

append does not return the list, it returns None, so you are clobbering it after the first loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.