Python--k-mean-algorithm the classification of sample points

Source: Internet
Author: User

In pattern recognition, K-mean algorithm is mainly used for clustering with known classification number, the realization is simple, the algorithm is clear, and belongs to one of the simpler dynamic clustering algorithms.

In the algorithm, the convergence of the clustering centers of the two-times algorithm is determined by iteration, and then the iteration is decided whether to continue the iterative (the algorithm is exited and the classification is completed).

A simple example of the following K-mean algorithm:

#Clustering Analysis of K-means algorithm fromNumPyImport* fromMathImportsqrt#requires a program to cluster the following data, taking K to 2x=array ([[[0,0],[1,0],[0,1],[1,1],[2,1],        [1,2],[2,2],[3,2],[6,6],[7,6],        [8,6],[6,7],[7,7],[8,7],[9,7],        [7,8],[8,8],[9,8],[8,9],[9,9]])#finding the distance between the sample point and the cluster centerdefDis (b):returnsqrt ((a[0]-b[0]) **2+ (a[1]-b[1]) **2)    #Classification of SamplesdefDistribution (X,Z_1,Z_2,N1,N2): forIinchRange (0,len (x)):if(Dis (x[i],z_1) <dis (x[i],z_2)): N1.append (i)Else: N2.append (i)#Creating a new cluster centerdefNew_dis_center (x,z_1,z_2,n1,n2): Z_1_1=[0,0] Z_2_1=[0,0] forIinchRange (0, (Len (N1))): Z_1_1=z_1_1+x[n1[i]]/Len (N1) forIinchRange (0, (Len (N2))): Z_2_1=z_2_1+x[n2[i]]/Len (n2)returnZ_1_1,z_2_1#iterate function input data and initial cluster centerdefIteration_fun (x,z_1,z_2):#Initialize the intermediate amount of the iterative cluster centerz_1_1=[0,0] Z_2_1=[0,0] N1=[] N2=[] Distribution (X,Z_1,Z_2,N1,N2) (z_1_1,z_2_1)=New_dis_center (X,Z_1,Z_2,N1,N2)#Iterative judgment    if(z_1_1 = = z_1). All ()or(z_2_1==z_2). All ():Print("The clustering Center after iteration:")        Print("Clustering Center One:")        Print(z_1)Print("Clustering Center:")        Print(z_2)Else: Z_1=z_1_1 z_2=z_2_1 Iteration_fun (x,z_1,z_2)#initializing cluster centers (including alternate iterative clustering centers) cluster Sample setz_1=x[0]z_2=x[1]iteration_fun (x,z_1,z_2)

Python--k-mean-algorithm the classification of sample points

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.