今天用sklearn里面的K-Means实现了简单的点的聚类。
#!/usr/bin/python
#-*- coding : UTF-8 -*-
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from sklearn import cluster
X = [[1,1], [14,15], [0.3, 2], [2, 1.9], [2,4], [13,16]
, [12,17], [13, 13]]
k_means = cluster.KMeans(n_clusters = 2)
k_means.fit(X)
center = k_means.cluster_centers_
labels = k_means.labels_
error = k_means.inertia_
#plot points using different colors
colors = cm.spectral(labels.astype(float) / n_clusters)
x1 = [x[0] for x in X]
y1 = [x[1] for x in X]
plt.scatter(x1, y1, c=colors)
x2 = [x[0] for x in center]
y2 = [x[1] for x in center]
plt.scatte