简单二维点的聚类

来源:互联网 发布:js 隐藏域赋值 编辑:程序博客网 时间:2024/06/16 04:28

今天用sklearn里面的K-Means实现了简单的点的聚类。

#!/usr/bin/python#-*- coding : UTF-8 -*-import numpy  as npimport scipy as spimport matplotlib.pyplot as pltimport matplotlib.cm as cmfrom sklearn import clusterX = [[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 colorscolors = 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.scatter(x2, y2, c="red")xlim(0, 20)  #limited the length of axisylim(0, 20)


0 0
原创粉丝点击