Simple description
The main task of this algorithm is to measure the distance between different eigenvalues, there is a distance, it can be classified.
For short, KNN.
Known: the training set, and the label for each training set.
Next: Compare the data in the training set to calculate the most similar k distance. Select the most common category of similar data. As a classification of new data.
Python instance
Copy Code code as follows:
#-*-coding:cp936-*-
#win系统中应用cp936编码, it's better to be utf-8 in Linux.
From numpy import * #引入科学计算包
Import operator #经典python函数库. The operator module.
#创建数据集
Def createdataset ():
Group=array ([[[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
Labels=[' A ', ' a ', ' B ', ' B '
Return Group,labels
#算法核心
#inX: An input vector for categorization. It is about to be sorted.
#dataSet: Training Sample Set
#labels: Label Vector
def classfy0 (inx,dataset,labels,k):
#距离计算
Datasetsize =dataset.shape[0] #得到数组的行数. Knowing that there are several training data
Functions in Diffmat =tile (InX, (datasetsize,1))-dataset#tile:numpy. Tile the original array and expands it into 4 identical arrays. Diffmat, the difference between the target and the training value is obtained.
Sqdiffmat =diffmat**2# each element squared separately
Sqdistances =sqdiffmat.sum (Axis=1) #对应列相乘, that gets the square of every distance
Distances =sqdistances**0.5#, get the distance.
Sorteddistindicies=distances.argsort () #升序排列
#选择距离最小的k个点.
classcount={}
For I in range (k):
Voteilabel=labels[sorteddistindicies[i]]
Classcount[voteilabel]=classcount.get (voteilabel,0) +1
#排序
Sortedclasscount=sorted (Classcount.iteritems (), Key=operator.itemgetter (1), reverse=true)
return sortedclasscount[0][0]
Unexpected harvest
Add your own modules to the Python default search path: Create a xxx.pth file in the Python/lib/-packages directory and write the path to the module where you wrote it.