Python k neighbor algorithm instance sharing
- 2020-04-02 13:42:35
- OfStack
Simple instructions
The main job of this algorithm is to measure the distance between the different eigenvalues, and if you have this distance, you can classify.
KNN for short.
Known: the training set, and the label for each training set.
Next: compare with the data in the training set and calculate the most similar k distances. Select the category with the most similar data. As a classification of new data.
Python instance
# -*- coding: cp936 -*-
#win Application in system cp936 Coding, linux Medium best or utf-8 Is better.
from numpy import *# Introduce the scientific computing package
import operator # classic python Function library. Operator module.
# Create data set
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
# Algorithm is the core
#inX : input vector for classification. It's about to be classified.
#dataSet : training sample set
#labels: Label vector
def classfy0(inX,dataSet,labels,k):
# Distance calculation
dataSetSize =dataSet.shape[0]# I get the number of rows in the array. You know that there are several training data
diffMat =tile(inX,(dataSetSize,1))-dataSet#tile:numpy The function of. tile I'm going to expand this array from one to the other 4 The same array. diffMat The difference between the target and the training value is obtained.
sqDiffMat =diffMat**2# Each element is squared separately
sqDistances =sqDiffMat.sum(axis=1)# Multiply the columns, and you get the square of each distance
distances =sqDistances**0.5# The square root, you get the distance.
sortedDistIndicies=distances.argsort()# Ascending order
# Choose the one with the smallest distance k A point.
classCount={}
for i in range(k):
voteIlabel=labels[sortedDistIndicies[i]]
classCount[voteIlabel]=classCount.get(voteIlabel,0)+1
# The sorting
sortedClassCount=sorted(classCount.iteritems(),key=operator.itemgetter(1),reverse=True)
return sortedClassCount[0][0]
bonus
Add your module to the python default search path: create a xxx. PTH file in the python/lib/-packages directory and write to the path where your module is written