Python K-Neighbor algorithm instance sharing _python

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.