Python 2.7
IDE Pycharm 5.0.3
numpy 1.11.0
前言
總算邁入機(jī)器學(xué)習(xí)第一步剂癌,總比原地踏步要好境肾。
什么是K-近鄰?
一句話總結(jié)永丝,物以類聚锹漱,人以群分,更‘靠近’哪一個(gè)點(diǎn)慕嚷,就認(rèn)為它屬于那一個(gè)點(diǎn)凌蔬。以一篇碩士論文截圖說(shuō)明
Knn算法思想
根據(jù)這個(gè)偽代碼流程,我們就可以使用python進(jìn)行算法重構(gòu)了闯冷,分三步砂心,一算距離,二排序蛇耀,三取值
實(shí)現(xiàn)Knn基礎(chǔ)代碼
第一部分辩诞,被調(diào)函數(shù),在主程序中需要導(dǎo)入
# -*- coding: utf-8 -*-
from numpy import *
import operator
def creatDataSet():
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): # inX用于需要分類的數(shù)據(jù)纺涤,dataSet輸入訓(xùn)練集
######輸入與訓(xùn)練樣本之間的距離計(jì)算######
dataSetSize = dataSet.shape[0] # 讀取行數(shù),shape[1]則為列數(shù)
diffMat = tile(inX,(dataSetSize,1))-dataSet # tile,重復(fù)inX數(shù)組的行(dataSize)次译暂,列重復(fù)1
sqDiffMat = diffMat**2 #平方操作
sqDistances = sqDiffMat.sum(axis=1) # 每一個(gè)列向量相加,axis=0為行相加
distances = sqDistances**0.5
sortedDistIndicies = distances.argsort() # argsort函數(shù)返回的是數(shù)組值從小到大的索引值
#print sortedDistIndicies #產(chǎn)生的是一個(gè)排序號(hào)組成的矩陣
classCount={}
######累計(jì)次數(shù)構(gòu)成字典######
for i in range(k):
#print sortedDistIndicies[i]
voteIlabel = labels[sortedDistIndicies[i]] #排名前k個(gè)貼標(biāo)簽
#print voteIlabel
classCount[voteIlabel] = classCount.get(voteIlabel,0)+1 # 不斷累加計(jì)數(shù)的過(guò)程,體現(xiàn)在字典的更新中
#print classCount.get(voteIlabel,0)
#print classCount
#get(key,default=None),就是造字典
######找到出現(xiàn)次數(shù)最大的點(diǎn)######
sortedClassCount = sorted(classCount.iteritems(),key = operator.itemgetter(1),reverse=True)
#以value值大小進(jìn)行排序撩炊,reverse=True降序
#print classCount.iteritems()
#print sortedClassCount
#key = operator.itemgetter(1)外永,operator.itemgetter函數(shù)獲取的不是值,而是定義了一個(gè)函數(shù)拧咳,通過(guò)該函數(shù)作用到對(duì)象上才能獲取值
return sortedClassCount[0][0]
#返回出現(xiàn)次數(shù)最多的value的key
第二部分伯顶,主程序部分,只要將主程序和被調(diào)函數(shù)放在同一工作目錄下骆膝,直接運(yùn)行主程序即可
# -*- coding: utf-8 -*-
import Knn_bymyself
group,labels = Knn_bymyself.creatDataSet() # 調(diào)用Knn_bymyself中的creatDataSet()方法
while 1:
try:
a=input('please input x:')
b=input('please input y:')
#classify0(inX,dataSet,labels,k)對(duì)應(yīng)起來(lái)看
print 'belong to :'+Knn_bymyself.classify0([a,b],group,labels,3)+' class'
except:
break
跑出來(lái)大概是這樣的:
please input x:2
please input y:1
belong to :A class
分析一下
group也就是輸入訓(xùn)練集祭衩,label就是訓(xùn)練集代表的標(biāo)簽
從圖形上分析
從上圖可以看出,我測(cè)試的點(diǎn)是(2,1)阅签,明顯可以看出它是距離A類比較靠近掐暮,所以歸屬于A類沒(méi)有問(wèn)題。
再?gòu)乃惴ㄉ戏治觯?/em>
計(jì)算unknown點(diǎn)到每個(gè)點(diǎn)的距離政钟,然后取最小距離的K個(gè)點(diǎn)路克,在K個(gè)點(diǎn)中樟结,哪個(gè)類出現(xiàn)的概率高就歸屬于哪一類,應(yīng)該沒(méi)什么問(wèn)題了精算。
你可能需要的知道
先過(guò)一眼NumPy快速入門(mén)→點(diǎn)擊這里快速學(xué)習(xí)NumPy
附加:上述代碼中出現(xiàn)的方法使用解析
In[138]: marray
Out[138]:
array([[1, 2, 3],
[4, 5, 6]])
- sum(axis= )狭吼,行(列)相加形成新的行(列)
In[135]: marray.sum(axis=0)
Out[135]: array([5, 7, 9])
- tile(a,(n,m)),將數(shù)組a的行重復(fù)n次殖妇,列重復(fù)m次
In[136]: b = tile(marray,(2,3))
In[137]: b
Out[137]:
array([[1, 2, 3, 1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6, 4, 5, 6],
[1, 2, 3, 1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6, 4, 5, 6]])
dict.get(k,d)
get相當(dāng)于一條if...else...語(yǔ)句,參數(shù)k在字典中,字典將返回dict[k]也就是k對(duì)應(yīng)的value值破花;如果參數(shù)k不在字典中則返回參數(shù)d谦趣。
舉栗子
In[149]: dict = {'A':1,'B':2}
In[150]: dict.get('A',0)
Out[150]: 1
In[151]: dict.get('C',1)
Out[151]: 1
- .iteritems()迭代器的使用
#使用next()
In[165]: dict.iteritems().next()
Out[164]: ('A', 1)
#使用for循環(huán)
In[169]: for i in dict.iteritems():
... print i
...
('A', 1)
('B', 2)
#轉(zhuǎn)化成list之后檢索
In[170]: list_dict = list(dict.iteritems())
In[171]: list_dict[0]
Out[170]: ('A', 1)
最后
這里只是解釋了如何使用最基本的KNN的方法,代碼也是最簡(jiǎn)單的座每,配合圖的講解前鹅,應(yīng)該會(huì)更加清楚一些,代碼我抄自機(jī)器學(xué)習(xí)峭梳,但是它的解釋比較少舰绘,算是加上了自己的備注,也算是自己的一個(gè)學(xué)習(xí)過(guò)程吧葱椭,希望對(duì)你也有幫助
附錄
如果你對(duì)那副點(diǎn)陣圖感興趣捂寿,也是用python寫(xiě)的代碼如下,可作參考孵运。
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
plt.title("KNN ")
plt.xlim(xmax=2.2,xmin=0)
plt.ylim(ymax=2.2,ymin=0)
#標(biāo)識(shí)箭頭
plt.annotate("unknown", xy = (2, 1), xytext = (1.5, 2), arrowprops = dict(facecolor = 'black', shrink = 0.1))
plt.annotate("A class", xy = (1, 1.2), xytext = (1, 2), arrowprops = dict(facecolor = 'black', shrink = 0.1))
plt.annotate("B class", xy = (0.1,0.1), xytext = (0.5,1), arrowprops = dict(facecolor = 'black', shrink = 0.1))
plt.xlabel("x")
plt.ylabel("y")
plt.plot([1,1,0,0],[1.1,1,0,0.1],'ro') # A.B類的點(diǎn)陣
plt.plot([2],[1],'ro') # unknown點(diǎn)
plt.show()
致謝
利用python進(jìn)行數(shù)據(jù)分析.Wes McKinney著
機(jī)器學(xué)習(xí)實(shí)戰(zhàn).Peter Harrington著
@jihite--字典訪問(wèn)的三種方法
@MrLevo520--numpy快速入門(mén)