Python k近邻算法
这是我的k最近邻算法代码:Python k近邻算法,python,algorithm,Python,Algorithm,这是我的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): ''
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'
回溯(最近一次呼叫最后一次):
文件“C:\Users\Toshiba\workspace\assignment5\src\knn2.py”,第76行,格式为knn=findKnn(k)
文件“C:\Users\Toshiba\workspace\assignment5\src\knn2.py”,第64行,findkn-knns=knns.append(knn)
AttributeError:“非类型”对象没有属性“附加”
我知道这段代码非常业余,但有人能帮我解决这个问题吗?list.append不会返回列表。简单地做:
knns.append(knn)
而不是:
knns = knns.append(knn)
append
不返回列表,它不返回任何值,因此您在第一次循环后对其进行重击。您这样做是因为您想实现该算法,了解其工作原理,等等?或者你需要一个最近的邻居搜索一个更大的项目?如果您只需要最近邻搜索,请查看scipy.spatial。KDTree@Elliottscipy.spatial.cKDTree也在那里。。。