Python K-means toolkit, pythonk-means

Source: Internet
Author: User

Python K-means toolkit, pythonk-means

Write a Data Mining experimentK-means algorithmIt is not very difficult to finish writing. During the writing process, I thought that python must have a package. Although the Senior Brother said that it was not used up, But I had finished writing it myself. If I used the package, I was not very familiar with it, I found some information and learned it. In addition, I wrote too poorly and did not dare to take it out. I will write it again after improvement.

1. Note that the initial point must be converted to the numpy. array format.

2. If you directly calculate the center point, you can call the kmeans2 function directly. The following figure is used only for visualization.

#!/usr/bin/python import numpyimport matplotlibimport osmatplotlib.use('Agg')from scipy.cluster.vq import *import pylabpylab.close()xy1=[[2,10],[2,5],[8,4],[5,8],[7,5],[6,4],[1,2],[4,9],[7,3],[1,3]]xy2=numpy.array(xy1)cluster_num=3res, idx = kmeans2(numpy.array(zip(xy2[:,0],xy2[:,1])),cluster_num)print "local centre points:\n",rescolors = ([([0.4,1,0.4],[1,0.4,0.4],[0.1,0.8,1])[i] for i in idx])# plot colored pointspylab.scatter(xy2[:,0],xy2[:,1])# mark centroids as (X)pylab.scatter(res[:,0],res[:,1], marker='o', s = 500, linewidths=2, c='none')pylab.scatter(res[:,0],res[:,1], marker='x', s = 500, linewidths=2)#print os.getcwd()pylab.savefig('pic.png')
:

#---------------------------------------------------------------------------

Reference: http://blog.csdn.net/brandohero/article/details/39967663

#!/usr/bin/python # Adapted from http://hackmap.blogspot.com/2007/09/k-means-clustering-in-scipy.html import numpyimport matplotlibmatplotlib.use('Agg')from scipy.cluster.vq import *import pylabpylab.close() # generate 3 sets of normally distributed points around# different means with different variancespt1 = numpy.random.normal(1, 0.2, (100,2))pt2 = numpy.random.normal(2, 0.5, (300,2))pt3 = numpy.random.normal(3, 0.3, (100,2)) # slightly move sets 2 and 3 (for a prettier output)pt2[:,0] += 1pt3[:,0] -= 0.5 xy = numpy.concatenate((pt1, pt2, pt3)) # kmeans for 3 clustersres, idx = kmeans2(numpy.array(zip(xy[:,0],xy[:,1])),3) colors = ([([0.4,1,0.4],[1,0.4,0.4],[0.1,0.8,1])[i] for i in idx]) # plot colored pointspylab.scatter(xy[:,0],xy[:,1], c=colors) # mark centroids as (X)pylab.scatter(res[:,0],res[:,1], marker='o', s = 500, linewidths=2, c='none')pylab.scatter(res[:,0],res[:,1], marker='x', s = 500, linewidths=2) pylab.savefig('/tmp/kmeans.png')

#------------------------------------

Reprinted please certified in: http://write.blog.csdn.net/postedit/41158167

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.