python中的numpy和pandas---機器學習實戰第一篇KNN
阿新 • • 發佈:2018-11-29
http://www.cnblogs.com/prpl/p/5537417.html
熟悉其基本操作
from numpy import *
import operator
def data():
datas = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
labels = ['A','A','B','B']
return datas,labels
def classifiaction(a,datas1,labels,k):
datasize = datas1.shape[0]
datas2 = tile(a,(datasize,1 )) - datas1
datas3 = datas2**2
datas4 = datas3.sum(axis=1)
datas5 = datas4**0.5
sortdatas6 = datas5.argsort()
classifiy1 ={}
for i in range(k):
m = labels[sortdatas6[i]]
classifiy1[m]=classifiy1.get(m,0)+1
sortclassifiy2 = sorted(classifiy1.items(),key=operator.itemgetter(1 ),reverse=True)
return sortclassifiy2[0][0]
操作行輸入
import knn
datas,labels = knn.data()
datas
labels
knn.classifiaction([0,0],datas,labels,3)