python - 如何在均值漂移中查看集群成员

标签 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

最佳答案

请注意,在再次阅读您的问题后,我确实理解了您的问题。如果您正在寻找与每个功能相对应的集群 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

关于python - 如何在均值漂移中查看集群成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36518311/

相关文章:

python - LOAD XML INFILE 将嵌套子项保存为普通文件

python - Scikit学习: Applying Mean Shift on a multi-dimensional dataset

python - python的MeanShift模块中估计带宽时如何选择合适的分位数值?

python - Numpy 数组 : Extracting preferentially ordered values from array with Nans without padding?

python/BeautifulSoup 使用字符串值访问子对象/标签

python - 使用分区列写入 Pandas Dataframe parquet 元数据

filesystems - 轻量级的开源网络共享文件系统

javascript - 将集群与 Nodejs/Expressjs 结合使用时页面会永久加载

java - Jetty/Tomcat 加密的基于 cookie 的 session 存储?

python - 如何使用 scikit learn/pandas/python 打印任意一个集群的样本/观察结果/行?