KNN algorithm Python implementation

Source: Internet
Author: User


Haven't written a blog for a long time, whim. To write a just learn the KNN algorithm, in fact, is more than the similarity, by the high similarity

Nonsense, no code.

From numpy import  *import operator #创建初始矩阵group = Array ([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]]) label = [' A ', ' a ', ' B ', ' B ']def classfy (inx,dataset,labes,k):    datasetsize = dataset.shape[0] #获取矩阵的维度, which is the number of rows in the matrix    Diffmat = Tile (InX, ( datasetsize,1))-dataset# the relative distance    Sqdiffmat = diffmat**2    sqdistance = sqdiffmat.sum (axis=1)    distance = sqdistance**0.5# distance between two points formula is not interpreted    sorteddistinicies = Distance.argsort () #得到排序后的下标 "2,3,1,0"    ClassCount = {}    For I in range (k):        Voteilabel = labes[sorteddistinicies[i]] #此处就和K相关了        Classcount[voteilabel] = Classcount.get (voteilabel,0) +1# record occurrences    sortedclasscount = sorted (Classcount.iteritems (), Key=operator.itemgetter (1), Reverse=true)    return sortedclasscount[0][0] #返回最终的结果classfy ([0,0],group,label,3)


It's all in the code annotations, OK.

KNN algorithm Python implementation

Related Article

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.