Skip to content

Commit 0c53fba

Browse files
committed
Pushing the docs to dev/ for branch: main, commit 0822851f5cb17827939a7d7b4f8c84f43184ae89
1 parent 5cd7f2d commit 0c53fba

File tree

1,233 files changed

+6478
-4790
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,233 files changed

+6478
-4790
lines changed
Binary file not shown.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"collapsed": false
8+
},
9+
"outputs": [],
10+
"source": [
11+
"%matplotlib inline"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"\n# Bisecting K-Means and Regular K-Means Performance Comparison\n\nThis example shows differences between Regular K-Means algorithm and Bisecting K-Means.\n\nWhile K-Means clusterings are different when with increasing n_clusters,\nBisecting K-Means clustering build on top of the previous ones.\n\nThis difference can visually be observed.\n"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"metadata": {
25+
"collapsed": false
26+
},
27+
"outputs": [],
28+
"source": [
29+
"import matplotlib.pyplot as plt\n\nfrom sklearn.datasets import make_blobs\nfrom sklearn.cluster import BisectingKMeans, KMeans\n\n\nprint(__doc__)\n\n\n# Generate sample data\nn_samples = 1000\nrandom_state = 0\n\nX, _ = make_blobs(n_samples=n_samples, centers=2, random_state=random_state)\n\n# Number of cluster centers for KMeans and BisectingKMeans\nn_clusters_list = [2, 3, 4, 5]\n\n# Algorithms to compare\nclustering_algorithms = {\n \"Bisecting K-Means\": BisectingKMeans,\n \"K-Means\": KMeans,\n}\n\n# Make subplots for each variant\nfig, axs = plt.subplots(\n len(clustering_algorithms), len(n_clusters_list), figsize=(15, 5)\n)\n\naxs = axs.T\n\nfor i, (algorithm_name, Algorithm) in enumerate(clustering_algorithms.items()):\n for j, n_clusters in enumerate(n_clusters_list):\n algo = Algorithm(n_clusters=n_clusters, random_state=random_state)\n algo.fit(X)\n centers = algo.cluster_centers_\n\n axs[j, i].scatter(X[:, 0], X[:, 1], s=10, c=algo.labels_)\n axs[j, i].scatter(centers[:, 0], centers[:, 1], c=\"r\", s=20)\n\n axs[j, i].set_title(f\"{algorithm_name} : {n_clusters} clusters\")\n\n\n# Hide x labels and tick labels for top plots and y ticks for right plots.\nfor ax in axs.flat:\n ax.label_outer()\n ax.set_xticks([])\n ax.set_yticks([])\n\nplt.show()"
30+
]
31+
}
32+
],
33+
"metadata": {
34+
"kernelspec": {
35+
"display_name": "Python 3",
36+
"language": "python",
37+
"name": "python3"
38+
},
39+
"language_info": {
40+
"codemirror_mode": {
41+
"name": "ipython",
42+
"version": 3
43+
},
44+
"file_extension": ".py",
45+
"mimetype": "text/x-python",
46+
"name": "python",
47+
"nbconvert_exporter": "python",
48+
"pygments_lexer": "ipython3",
49+
"version": "3.9.12"
50+
}
51+
},
52+
"nbformat": 4,
53+
"nbformat_minor": 0
54+
}
Binary file not shown.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
=============================================================
3+
Bisecting K-Means and Regular K-Means Performance Comparison
4+
=============================================================
5+
6+
This example shows differences between Regular K-Means algorithm and Bisecting K-Means.
7+
8+
While K-Means clusterings are different when with increasing n_clusters,
9+
Bisecting K-Means clustering build on top of the previous ones.
10+
11+
This difference can visually be observed.
12+
13+
"""
14+
import matplotlib.pyplot as plt
15+
16+
from sklearn.datasets import make_blobs
17+
from sklearn.cluster import BisectingKMeans, KMeans
18+
19+
20+
print(__doc__)
21+
22+
23+
# Generate sample data
24+
n_samples = 1000
25+
random_state = 0
26+
27+
X, _ = make_blobs(n_samples=n_samples, centers=2, random_state=random_state)
28+
29+
# Number of cluster centers for KMeans and BisectingKMeans
30+
n_clusters_list = [2, 3, 4, 5]
31+
32+
# Algorithms to compare
33+
clustering_algorithms = {
34+
"Bisecting K-Means": BisectingKMeans,
35+
"K-Means": KMeans,
36+
}
37+
38+
# Make subplots for each variant
39+
fig, axs = plt.subplots(
40+
len(clustering_algorithms), len(n_clusters_list), figsize=(15, 5)
41+
)
42+
43+
axs = axs.T
44+
45+
for i, (algorithm_name, Algorithm) in enumerate(clustering_algorithms.items()):
46+
for j, n_clusters in enumerate(n_clusters_list):
47+
algo = Algorithm(n_clusters=n_clusters, random_state=random_state)
48+
algo.fit(X)
49+
centers = algo.cluster_centers_
50+
51+
axs[j, i].scatter(X[:, 0], X[:, 1], s=10, c=algo.labels_)
52+
axs[j, i].scatter(centers[:, 0], centers[:, 1], c="r", s=20)
53+
54+
axs[j, i].set_title(f"{algorithm_name} : {n_clusters} clusters")
55+
56+
57+
# Hide x labels and tick labels for top plots and y ticks for right plots.
58+
for ax in axs.flat:
59+
ax.label_outer()
60+
ax.set_xticks([])
61+
ax.set_yticks([])
62+
63+
plt.show()

dev/_downloads/scikit-learn-docs.zip

234 KB
Binary file not shown.
346 Bytes
27 Bytes
66 Bytes
-227 Bytes
9 Bytes

0 commit comments

Comments
 (0)