我做了一些聚类,我想可视化结果。
这是我为绘制集群而编写的函数:
import sklearn
from sklearn.cluster import DBSCAN
from sklearn import metrics
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import DBSCAN
from sklearn import metrics
from sklearn.datasets.samples_generator import make_blobs
from sklearn.preprocessing import StandardScaler
def plot_cluster(cluster, sample_matrix):
'''Input: "cluster", which is an object from DBSCAN,
e.g. dbscan_object = DBSCAN(3.0,4)
"sample_matrix" which is a data matrix:
X = [
[0,5,1,2],
[0,4,1,3],
[0,5,1,3],
[0,5,0,2],
[5,5,5,5],
]
Output: Plots the clusters nicely.
'''
import matplotlib.pyplot as plt
import numpy as np
f = lambda row: [float(x) for x in row]
sample_matrix = map(f,sample_matrix)
print sample_matrix
sample_matrix = StandardScaler().fit_transform(sample_matrix)
core_samples_mask = np.zeros_like(cluster.labels_, dtype=bool)
core_samples_mask[cluster.core_sample_indices_] = True
labels = cluster.labels_
# Black removed and is used for noise instead.
unique_labels = set(labels)
colors = plt.cm.Spectral(np.linspace(0, 1, len(unique_labels)))
for k, col in zip(unique_labels, colors):
if k == -1:
# Black used for noise.
col = 'k'
class_member_mask = (labels == k) # generator comprehension
# X is your data matrix
X = np.array(sample_matrix)
xy = X[class_member_mask & core_samples_mask]
plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=col,
markeredgecolor='k', markersize=14)
xy = X[class_member_mask & ~core_samples_mask]
plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=col,
markeredgecolor='k', markersize=6)
plt.ylim([0,10])
plt.xlim([0,10])
# plt.title('Estimated number of clusters: %d' % n_clusters_)
plt.savefig('cluster.png')
上面的函数几乎是从此处的 scikit-learn 演示中逐字复制的。
然而,当我尝试以下方法时:
dbscan_object = DBSCAN(3.0,4)
X = [
[0,5,1,2],
[0,4,1,3],
[0,5,1,3],
[0,5,0,2],
[5,5,5,5],
]
result = dbscan_object.fit(X)
print result.labels_
print 'plotting '
plot_cluster(result, X)
...它产生一个点。在 python 中绘制集群的最佳方法是什么?