Intro
Recently looking at machine learning related video tutorials, which talked about the clustering algorithm through Kmeans, the idea of studying the algorithm and showing it step-by-step through Python is the whole process of the algorithm. principle
The specific principle of kmeans found that a tutorial in the evening showed a clearer picture of Kmeans, as follows: Http://www.csdn.net/article/2012-07-03/2807073-k-means use Python to show
A good data organization and presentation can greatly enhance our understanding of the algorithm, so the first step is how to show a set of basic data. Data Source
Not to create a data source, and took a Chinese university Mooc gave a Kmeans test data: http://www.icourse163.org/learn/BIT-268001?tid=1002001005#/learn/ CONTENT?TYPE=DETAIL&ID=1002613060&CID=1002856188 can also be downloaded from my github: https://github.com/skyztt/ The Kmeans-python.git operation reads the original input through the NumPy You can use Np.loadtxt to read out the file, read the following data (previously did not understand the original data, that the original data only represent y-coordinates, and then find how to reduce the data dimension of the dimensions, but also a more understanding of an API bar, you can convert it through the flatten to one-dimensional data):
[[1.658985 4.285136]
[ -3.453687 3.424321] [
4.838138-1.151539]
...,
[-4.561235- 4.563214]
[5.461454-5.123464]
[4.012356-4.985623]]
Show it:
This is shown using Matplot, but here you need to extract the x,y coordinates from the raw data, and then use the NumPy slice: (attached to the previous blog post that was referenced at the time http://blog.csdn.net/liangzuojiayi/article/ details/51534164, one of the pictures is a good illustration of the problem.
Attached code, just a few words original data printing success ~
Import NumPy as NP
import Matplotlib.pyplot as plt
a = Np.loadtxt (". \\testSet.txt")
Plt.scatter (a[:,:1), a[ :, 1:2])
plt.show ()