如何在均值转换中看到集群成员

时间:2016-04-09 14:43:26

标签: python cluster-computing mean-shift

我已经设法使用python中的均值移位聚类方法生成一个聚类区域。数据来自CSV文件,包含7000个经度和纬度数据。代码和结果如下所示。问题是我如何从每个集群区域生成集群成员?

import numpy as np
from sklearn.cluster import MeanShift, estimate_bandwidth
from sklearn.datasets.samples_generator import make_blobs

###############################################################################
# Generate sample data
centers = [[1, 1], [-1, -1], [1, -1]]
import csv
X1 = [[0 for x in range(2)] for x in range(7161)]
counter = 0
with open('datatotal1.csv', 'rb') as f:
    reader = csv.reader(f, delimiter='\t')
    for row in reader:
        print row[1]
        #X.append([row[1], row[2]])
        #X = (X, [row[1], row[2]])
        X1[counter][0] = float(row[1])
        X1[counter][1] = float(row[2])
        counter = counter + 1

#X, _ = make_blobs(n_samples=13, centers=centers, cluster_std=0.6)
X = np.array(X1)
#print X
#print type(X[1])
#print X.shape
###############################################################################
# Compute clustering with MeanShift

# The following bandwidth can be automatically detected using
bandwidth = estimate_bandwidth(X, quantile=0.2, n_samples=1168)
bandwidth = 0.00447023
ms = MeanShift(bandwidth=bandwidth, bin_seeding=True)
ms.fit(X)
labels = ms.labels_
cluster_centers = ms.cluster_centers_

labels_unique = np.unique(labels)
n_clusters_ = len(labels_unique)

print("number of estimated clusters : %d" % n_clusters_)
print("bandwidth: %f" % bandwidth)

###############################################################################
# Plot result
import matplotlib.pyplot as plt
from itertools import cycle

plt.figure(1)
plt.clf()

colors = cycle('bgrcmykbgrcmykbgrcmykbgrcmyk')
for k, col in zip(range(n_clusters_), colors):
    my_members = labels == k
    cluster_center = cluster_centers[k]
    print cluster_center
    plt.plot(X[my_members, 1], X[my_members, 0], col + '.')
    plt.plot(cluster_center[1], cluster_center[0], 'o', markerfacecolor=col,
             markeredgecolor='k', markersize=14)
plt.title('Estimated number of clusters: %d' % n_clusters_)
plt.show()

Clustering Result

1 个答案:

答案 0 :(得分:2)

请注意,在再次阅读您的问题后,我真的明白了您的要求。如果您要查找与每个功能相对应的群集ID,我猜他们可以在 MeanShift 对象的labels_属性中找到它们,所以ms.labels_在您的情况下,与输入数组的顺序相同。

使用documentation中的示例:

In [12]: centers = [[1, 1], [-1, -1], [1, -1]]
    ...: 
    ...: X, _ = make_blobs(n_samples=10000, centers=centers, cluster_std=0.6)
    ...: bandwidth = estimate_bandwidth(X, quantile=0.2, n_samples=500)
    ...: 
    ...: ms = MeanShift(bandwidth=bandwidth, bin_seeding=True)
    ...: ms.fit(X)
    ...: labels = ms.labels_
    ...: cluster_centers = ms.cluster_centers_
    ...: 

In [16]: df = pd.DataFrame({"coords": X.tolist(), "label": labels})

In [17]: df.head()
Out[17]: 
                                       coords  label
0  [-1.5539732702382636, -0.4869122066516913]      2
1    [0.6348936165717934, 0.7771388572513406]      1
2   [0.7891587478804111, -1.1108675230054534]      0
3   [0.5728268980231348, 0.16462711784126938]      1
4   [1.1696896258095544, 0.17203555282372351]      1