我已经设法使用python中的均值偏移聚类方法生成了一个聚类区域。数据取自大约 7000 个经度和纬度数据的 CSV 文件。代码和结果如下所示。问题是如何从每个集群区域生成集群成员?
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()