Python implementation of K-means clustering

Source: Internet
Author: User

The principle of bioinformatics Operation V-bomb: The realization of K-means clustering.

Reprint please keep the source!

Python implementation of K-means clustering

Principle Reference: K-means cluster (upper)

The data is given by the teacher, two-dimensional, 2 * 3800 data. You can see that there are 7 classes in plot.

How to determine the number of categories I am learning, this script directly to the initial classification, and so I learned to send again.

The Python code is posted below, and the version is Python3.6.

1 #-*-coding:utf-8-*-2 """3 Created on Wed Dec 6 16:01:174 5 @author: Zxzhu6 """7 ImportNumPy as NP8 ImportMatplotlib.pyplot as Plt9  fromNumPyImportRandomTen  One defDistance (x): A     defDis (y): -         returnNP.SQRT (sum (x-y) **2)#European distance -     returnDis the  - defInit_k_means (k): -K_means = {} -      forIinchRange (k): +K_means[i] = [] -     returnK_means +  A defCal_seed (K_mean):#Recalculate seed points atK_mean =Np.array (K_mean) -New_seed = Np.mean (k_mean,axis=0)#the mean values of each dimension -     returnNew_seed -      - defK_means (Data,seed_k,k_means): -      forIinchData: inf =Distance (i) -dis = list (map (f,seed_k))#the distance from all seed points at a certain point toindex =dis.index (min (dis)) + k_means[index].append (i) -      theNew_seed = []#Storing new seeds *      forIinchRange (len (seed_k)): $ new_seed.append (Cal_seed (K_means[i]))Panax NotoginsengNew_seed =Np.array (new_seed) -     returnK_means,new_seed the      + defRun_k_means (data,k): ASeed_k = Data[random.randint (len (data), size=k)]#randomly generated seed points theK_means = Init_k_means (k)#initialize each type of +result =K_means (Data,seed_k,k_means) -Count =0 $      while  not(result[1] = = Seed_k). All ():#seed Point Change, continue clustering $Count+=1 -Seed_k = result[1] -K_means = Init_k_means (k=7)    theresult =K_means (Data,seed_k,k_means) -     Print(' Done')Wuyi     #print (result[1]) the     Print(count) -Plt.figure (figsize= (8,8)) WuColor ='rbgyckm' -      forIinchRange (k): AboutMyData =Np.array (Result[0][i]) $Plt.scatter (Mydata[:,0],mydata[:,1],color =Color[i]) -     returnResult[0] -  -data = Np.loadtxt ('K-means_data') ARun_k_means (data,k=7)   

Enclose the result diagram:

This algorithm is too dependent on the initial seed point selection, random points are likely to get local optimal results, so next learn how to set the initial seed point and the number of categories.

Python implementation of K-means clustering

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.