標籤:knn python 演算法
有監督的kNN近鄰演算法:
(1)計算已知類別資料集中的點與當前點之間的距離
(2)按照距離遞增次序排序
(3)選取與當前點距離最小的k個點
(4)確定前k個點所在類別的出現頻率
(5)返回前k個點出現頻率最高的類別作為當前點的預測分類
#資料範例
1 2:a
1 3:a
1 4:a
1 5:b
6 2:b
6 3:b
100 200:c
101 199:c
300 444:d
299 50:d
1000 10000:d
#版本0:純python
"kNN"from math import sqrtfrom collections import Counterdistance=lambda a,b:sqrt(sum(map(lambda ai,bi:pow(ai-bi,2),a,b))) if len(a)==len(b) else "Error0:data length match fail"distance2=lambda a,b:distance([int(i) for i in a.split()],[int(i) for i in b.split()]) # for strings#print(distance2('1 2 4 7 8','2 5 5 6 110'))readData=lambda file:{line.split(':')[0]:line.strip().split(':')[1] for line in open(file)}#print(readData())def judgeSpot(fileIn='test0.txt',x='1 2',num=5): distanceDict,data={},readData(fileIn) for k in data: distanceDict[str(distance2(x,k))]=data[k] # sortDistance=sorted(distanceDict.items(),key=lambda x:float(x[0]))[:num] # kindDict=[item[1] for item in sortDistance] return sorted(dict(Counter(item[1] for item in sorted(distanceDict.items(),key=lambda x:float(x[0]))[:num])).items(),key=lambda x:x[1],reverse=True)[0][0]#print(judgeSpot('1000 10000','test0.txt'),)def judgeSpot2(dataIn,x='1 2',num=5): distanceDict,data={},dataIn for k in data: distanceDict[str(distance2(x,k))]=data[k] # sortDistance=sorted(distanceDict.items(),key=lambda x:float(x[0]))[:num] # kindDict=[item[1] for item in sortDistance] return sorted(dict(Counter(item[1] for item in sorted(distanceDict.items(),key=lambda x:float(x[0]))[:num])).items(),key=lambda x:x[1],reverse=True)[0][0]print(judgeSpot('test0.txt','1000 10000'),)#Rate of Rightdef rateRight(fileIn='test0.txt',num=5): countRight,data=0,readData(fileIn) for k in data: if judgeSpot2(data,k,num)==data[k]: countRight+=1 return countRight/float(len(open(fileIn).readlines()))print(rateRight())
#版本1:numpy版 (待實現)
搜尋
複製
<Python><有監督>kNN--近鄰分類演算法