Skip to content

Commit c85b344

Browse files
author
xuming06
committed
add kmeans demo.
1 parent 70722a7 commit c85b344

13 files changed

Lines changed: 3264 additions & 0 deletions

11scikit-learn/kmeans/demo.ipynb

Lines changed: 405 additions & 0 deletions
Large diffs are not rendered by default.

11scikit-learn/kmeans/plot_cluster_iris.ipynb

Lines changed: 213 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
=========================================================
6+
K-means Clustering
7+
=========================================================
8+
9+
The plots display firstly what a K-means algorithm would yield
10+
using three clusters. It is then shown what the effect of a bad
11+
initialization is on the classification process:
12+
By setting n_init to only 1 (default is 10), the amount of
13+
times that the algorithm will be run with different centroid
14+
seeds is reduced.
15+
The next plot displays what using eight clusters would deliver
16+
and finally the ground truth.
17+
18+
"""
19+
print(__doc__)
20+
21+
22+
# Code source: Gaël Varoquaux
23+
# Modified for documentation by Jaques Grobler
24+
# License: BSD 3 clause
25+
26+
import numpy as np
27+
import matplotlib.pyplot as plt
28+
# Though the following import is not directly being used, it is required
29+
# for 3D projection to work
30+
from mpl_toolkits.mplot3d import Axes3D
31+
32+
from sklearn.cluster import KMeans
33+
from sklearn import datasets
34+
35+
np.random.seed(5)
36+
37+
iris = datasets.load_iris()
38+
X = iris.data
39+
y = iris.target
40+
41+
estimators = [('k_means_iris_8', KMeans(n_clusters=8)),
42+
('k_means_iris_3', KMeans(n_clusters=3)),
43+
('k_means_iris_bad_init', KMeans(n_clusters=3, n_init=1,
44+
init='random'))]
45+
46+
fignum = 1
47+
titles = ['8 clusters', '3 clusters', '3 clusters, bad initialization']
48+
for name, est in estimators:
49+
fig = plt.figure(fignum, figsize=(4, 3))
50+
ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)
51+
est.fit(X)
52+
labels = est.labels_
53+
54+
ax.scatter(X[:, 3], X[:, 0], X[:, 2],
55+
c=labels.astype(np.float), edgecolor='k')
56+
57+
ax.w_xaxis.set_ticklabels([])
58+
ax.w_yaxis.set_ticklabels([])
59+
ax.w_zaxis.set_ticklabels([])
60+
ax.set_xlabel('Petal width')
61+
ax.set_ylabel('Sepal length')
62+
ax.set_zlabel('Petal length')
63+
ax.set_title(titles[fignum - 1])
64+
ax.dist = 12
65+
fignum = fignum + 1
66+
67+
# Plot the ground truth
68+
fig = plt.figure(fignum, figsize=(4, 3))
69+
ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)
70+
71+
for name, label in [('Setosa', 0),
72+
('Versicolour', 1),
73+
('Virginica', 2)]:
74+
ax.text3D(X[y == label, 3].mean(),
75+
X[y == label, 0].mean(),
76+
X[y == label, 2].mean() + 2, name,
77+
horizontalalignment='center',
78+
bbox=dict(alpha=.2, edgecolor='w', facecolor='w'))
79+
# Reorder the labels to have colors matching the cluster results
80+
y = np.choose(y, [1, 2, 0]).astype(np.float)
81+
ax.scatter(X[:, 3], X[:, 0], X[:, 2], c=y, edgecolor='k')
82+
83+
ax.w_xaxis.set_ticklabels([])
84+
ax.w_yaxis.set_ticklabels([])
85+
ax.w_zaxis.set_ticklabels([])
86+
ax.set_xlabel('Petal width')
87+
ax.set_ylabel('Sepal length')
88+
ax.set_zlabel('Petal length')
89+
ax.set_title('Ground Truth')
90+
ax.dist = 12
91+
92+
fig.show()

11scikit-learn/kmeans/plot_color_quantization.ipynb

Lines changed: 227 additions & 0 deletions
Large diffs are not rendered by default.

11scikit-learn/kmeans/plot_kmeans_assumptions.ipynb

Lines changed: 142 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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()

11scikit-learn/kmeans/plot_kmeans_silhouette_analysis.ipynb

Lines changed: 288 additions & 0 deletions
Large diffs are not rendered by default.

11scikit-learn/kmeans/plot_kmeans_stability_low_dim_dense.ipynb

Lines changed: 243 additions & 0 deletions
Large diffs are not rendered by default.

11scikit-learn/kmeans/plot_mini_batch_kmeans.ipynb

Lines changed: 204 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)