|
| 1 | +""" |
| 2 | +=========================================================== |
| 3 | +A demo of K-Means clustering on the handwritten digits data |
| 4 | +=========================================================== |
| 5 | +
|
| 6 | +In this example we compare the various initialization strategies for |
| 7 | +K-means in terms of runtime and quality of the results. |
| 8 | +
|
| 9 | +As the ground truth is known here, we also apply different cluster |
| 10 | +quality metrics to judge the goodness of fit of the cluster labels to the |
| 11 | +ground truth. |
| 12 | +
|
| 13 | +Cluster quality metrics evaluated (see :ref:`clustering_evaluation` for |
| 14 | +definitions and discussions of the metrics): |
| 15 | +
|
| 16 | +=========== ======================================================== |
| 17 | +Shorthand full name |
| 18 | +=========== ======================================================== |
| 19 | +homo homogeneity score |
| 20 | +compl completeness score |
| 21 | +v-meas V measure |
| 22 | +ARI adjusted Rand index |
| 23 | +AMI adjusted mutual information |
| 24 | +silhouette silhouette coefficient |
| 25 | +=========== ======================================================== |
| 26 | +手写体数字数据的k-均值聚类演示 |
| 27 | +在这个例子中,我们比较k-means的各种初始化策略在运行和结果的效果。 |
| 28 | +由于背景真实性是已知的,因此我们还应用不同的聚类质量度量来判断聚类标签对背景真实性的拟合程度。 |
| 29 | +评估聚类质量度量(参见定义和讨论度量的聚类性能评估): |
| 30 | +
|
| 31 | +""" |
| 32 | +print(__doc__) |
| 33 | + |
| 34 | +from time import time |
| 35 | +import numpy as np |
| 36 | +import matplotlib.pyplot as plt |
| 37 | + |
| 38 | +from sklearn import metrics |
| 39 | +from sklearn.cluster import KMeans |
| 40 | +from sklearn.datasets import load_digits |
| 41 | +from sklearn.decomposition import PCA |
| 42 | +from sklearn.preprocessing import scale |
| 43 | + |
| 44 | +np.random.seed(42) |
| 45 | + |
| 46 | +digits = load_digits() |
| 47 | +data = scale(digits.data) |
| 48 | + |
| 49 | +n_samples, n_features = data.shape |
| 50 | +n_digits = len(np.unique(digits.target)) |
| 51 | +labels = digits.target |
| 52 | + |
| 53 | +sample_size = 300 |
| 54 | + |
| 55 | +print("n_digits: %d, \t n_samples %d, \t n_features %d" |
| 56 | + % (n_digits, n_samples, n_features)) |
| 57 | + |
| 58 | + |
| 59 | +print(82 * '_') |
| 60 | +print('init\t\ttime\tinertia\thomo\tcompl\tv-meas\tARI\tAMI\tsilhouette') |
| 61 | + |
| 62 | + |
| 63 | +def bench_k_means(estimator, name, data): |
| 64 | + t0 = time() |
| 65 | + estimator.fit(data) |
| 66 | + print('%-9s\t%.2fs\t%i\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f' |
| 67 | + % (name, (time() - t0), estimator.inertia_, |
| 68 | + metrics.homogeneity_score(labels, estimator.labels_), |
| 69 | + metrics.completeness_score(labels, estimator.labels_), |
| 70 | + metrics.v_measure_score(labels, estimator.labels_), |
| 71 | + metrics.adjusted_rand_score(labels, estimator.labels_), |
| 72 | + metrics.adjusted_mutual_info_score(labels, estimator.labels_), |
| 73 | + metrics.silhouette_score(data, estimator.labels_, |
| 74 | + metric='euclidean', |
| 75 | + sample_size=sample_size))) |
| 76 | + |
| 77 | +bench_k_means(KMeans(init='k-means++', n_clusters=n_digits, n_init=10), |
| 78 | + name="k-means++", data=data) |
| 79 | + |
| 80 | +bench_k_means(KMeans(init='random', n_clusters=n_digits, n_init=10), |
| 81 | + name="random", data=data) |
| 82 | + |
| 83 | +# in this case the seeding of the centers is deterministic, hence we run the |
| 84 | +# kmeans algorithm only once with n_init=1 |
| 85 | +pca = PCA(n_components=n_digits).fit(data) |
| 86 | +bench_k_means(KMeans(init=pca.components_, n_clusters=n_digits, n_init=1), |
| 87 | + name="PCA-based", |
| 88 | + data=data) |
| 89 | +print(82 * '_') |
| 90 | + |
| 91 | +# ############################################################################# |
| 92 | +# Visualize the results on PCA-reduced data |
| 93 | + |
| 94 | +reduced_data = PCA(n_components=2).fit_transform(data) |
| 95 | +kmeans = KMeans(init='k-means++', n_clusters=n_digits, n_init=10) |
| 96 | +kmeans.fit(reduced_data) |
| 97 | + |
| 98 | +# Step size of the mesh. Decrease to increase the quality of the VQ. |
| 99 | +h = .02 # point in the mesh [x_min, x_max]x[y_min, y_max]. |
| 100 | + |
| 101 | +# Plot the decision boundary. For that, we will assign a color to each |
| 102 | +x_min, x_max = reduced_data[:, 0].min() - 1, reduced_data[:, 0].max() + 1 |
| 103 | +y_min, y_max = reduced_data[:, 1].min() - 1, reduced_data[:, 1].max() + 1 |
| 104 | +xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) |
| 105 | + |
| 106 | +# Obtain labels for each point in mesh. Use last trained model. |
| 107 | +Z = kmeans.predict(np.c_[xx.ravel(), yy.ravel()]) |
| 108 | + |
| 109 | +# Put the result into a color plot |
| 110 | +Z = Z.reshape(xx.shape) |
| 111 | +plt.figure(1) |
| 112 | +plt.clf() |
| 113 | +plt.imshow(Z, interpolation='nearest', |
| 114 | + extent=(xx.min(), xx.max(), yy.min(), yy.max()), |
| 115 | + cmap=plt.cm.Paired, |
| 116 | + aspect='auto', origin='lower') |
| 117 | + |
| 118 | +plt.plot(reduced_data[:, 0], reduced_data[:, 1], 'k.', markersize=2) |
| 119 | +# Plot the centroids as a white X |
| 120 | +centroids = kmeans.cluster_centers_ |
| 121 | +plt.scatter(centroids[:, 0], centroids[:, 1], |
| 122 | + marker='x', s=169, linewidths=3, |
| 123 | + color='w', zorder=10) |
| 124 | +plt.title('K-means clustering on the digits dataset (PCA-reduced data)\n' |
| 125 | + 'Centroids are marked with white cross') |
| 126 | +plt.xlim(x_min, x_max) |
| 127 | +plt.ylim(y_min, y_max) |
| 128 | +plt.xticks(()) |
| 129 | +plt.yticks(()) |
| 130 | +plt.show() |
0 commit comments