Skip to content

Commit 109d4df

Browse files
committed
Pushing the docs to dev/ for branch: main, commit 1e965785fd529e9c3f12953782c165eaa90f2171
1 parent ade8c86 commit 109d4df

File tree

1,230 files changed

+4680
-4389
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,230 files changed

+4680
-4389
lines changed
Binary file not shown.

dev/_downloads/5eeecece5c41d6edcf4555b5e7c34350/plot_coin_ward_segmentation.py

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,51 @@
1313
# Alexandre Gramfort, 2011
1414
# License: BSD 3 clause
1515

16-
import time as time
17-
18-
import numpy as np
19-
from scipy.ndimage import gaussian_filter
20-
21-
import matplotlib.pyplot as plt
16+
# %%
17+
# Generate data
18+
# -------------
2219

2320
from skimage.data import coins
24-
from skimage.transform import rescale
2521

26-
from sklearn.feature_extraction.image import grid_to_graph
27-
from sklearn.cluster import AgglomerativeClustering
28-
29-
30-
# #############################################################################
31-
# Generate data
3222
orig_coins = coins()
3323

24+
# %%
3425
# Resize it to 20% of the original size to speed up the processing
3526
# Applying a Gaussian filter for smoothing prior to down-scaling
3627
# reduces aliasing artifacts.
28+
29+
import numpy as np
30+
from scipy.ndimage import gaussian_filter
31+
from skimage.transform import rescale
32+
3733
smoothened_coins = gaussian_filter(orig_coins, sigma=2)
3834
rescaled_coins = rescale(
39-
smoothened_coins, 0.2, mode="reflect", anti_aliasing=False, multichannel=False
35+
smoothened_coins,
36+
0.2,
37+
mode="reflect",
38+
anti_aliasing=False,
4039
)
4140

4241
X = np.reshape(rescaled_coins, (-1, 1))
4342

44-
# #############################################################################
45-
# Define the structure A of the data. Pixels connected to their neighbors.
43+
# %%
44+
# Define structure of the data
45+
# ----------------------------
46+
#
47+
# Pixels are connected to their neighbors.
48+
49+
from sklearn.feature_extraction.image import grid_to_graph
50+
4651
connectivity = grid_to_graph(*rescaled_coins.shape)
4752

48-
# #############################################################################
53+
# %%
4954
# Compute clustering
55+
# ------------------
56+
57+
import time as time
58+
59+
from sklearn.cluster import AgglomerativeClustering
60+
5061
print("Compute structured hierarchical clustering...")
5162
st = time.time()
5263
n_clusters = 27 # number of regions
@@ -55,12 +66,20 @@
5566
)
5667
ward.fit(X)
5768
label = np.reshape(ward.labels_, rescaled_coins.shape)
58-
print("Elapsed time: ", time.time() - st)
59-
print("Number of pixels: ", label.size)
60-
print("Number of clusters: ", np.unique(label).size)
69+
print(f"Elapsed time: {time.time() - st:.3f}s")
70+
print(f"Number of pixels: {label.size}")
71+
print(f"Number of clusters: {np.unique(label).size}")
6172

62-
# #############################################################################
73+
# %%
6374
# Plot the results on an image
75+
# ----------------------------
76+
#
77+
# Agglomerative clustering is able to segment each coin however, we have had to
78+
# use a ``n_cluster`` larger than the number of coins because the segmentation
79+
# is finding a large in the background.
80+
81+
import matplotlib.pyplot as plt
82+
6483
plt.figure(figsize=(5, 5))
6584
plt.imshow(rescaled_coins, cmap=plt.cm.gray)
6685
for l in range(n_clusters):
@@ -70,6 +89,5 @@
7089
plt.cm.nipy_spectral(l / float(n_clusters)),
7190
],
7291
)
73-
plt.xticks(())
74-
plt.yticks(())
92+
plt.axis("off")
7593
plt.show()
Binary file not shown.

dev/_downloads/c8c7c5458b84586cacd8498015126bc4/plot_coin_ward_segmentation.ipynb

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,97 @@
2626
},
2727
"outputs": [],
2828
"source": [
29-
"# Author : Vincent Michel, 2010\n# Alexandre Gramfort, 2011\n# License: BSD 3 clause\n\nimport time as time\n\nimport numpy as np\nfrom scipy.ndimage import gaussian_filter\n\nimport matplotlib.pyplot as plt\n\nfrom skimage.data import coins\nfrom skimage.transform import rescale\n\nfrom sklearn.feature_extraction.image import grid_to_graph\nfrom sklearn.cluster import AgglomerativeClustering\n\n\n# #############################################################################\n# Generate data\norig_coins = coins()\n\n# Resize it to 20% of the original size to speed up the processing\n# Applying a Gaussian filter for smoothing prior to down-scaling\n# reduces aliasing artifacts.\nsmoothened_coins = gaussian_filter(orig_coins, sigma=2)\nrescaled_coins = rescale(\n smoothened_coins, 0.2, mode=\"reflect\", anti_aliasing=False, multichannel=False\n)\n\nX = np.reshape(rescaled_coins, (-1, 1))\n\n# #############################################################################\n# Define the structure A of the data. Pixels connected to their neighbors.\nconnectivity = grid_to_graph(*rescaled_coins.shape)\n\n# #############################################################################\n# Compute clustering\nprint(\"Compute structured hierarchical clustering...\")\nst = time.time()\nn_clusters = 27 # number of regions\nward = AgglomerativeClustering(\n n_clusters=n_clusters, linkage=\"ward\", connectivity=connectivity\n)\nward.fit(X)\nlabel = np.reshape(ward.labels_, rescaled_coins.shape)\nprint(\"Elapsed time: \", time.time() - st)\nprint(\"Number of pixels: \", label.size)\nprint(\"Number of clusters: \", np.unique(label).size)\n\n# #############################################################################\n# Plot the results on an image\nplt.figure(figsize=(5, 5))\nplt.imshow(rescaled_coins, cmap=plt.cm.gray)\nfor l in range(n_clusters):\n plt.contour(\n label == l,\n colors=[\n plt.cm.nipy_spectral(l / float(n_clusters)),\n ],\n )\nplt.xticks(())\nplt.yticks(())\nplt.show()"
29+
"# Author : Vincent Michel, 2010\n# Alexandre Gramfort, 2011\n# License: BSD 3 clause"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"## Generate data\n\n"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"metadata": {
43+
"collapsed": false
44+
},
45+
"outputs": [],
46+
"source": [
47+
"from skimage.data import coins\n\norig_coins = coins()"
48+
]
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"metadata": {},
53+
"source": [
54+
"Resize it to 20% of the original size to speed up the processing\nApplying a Gaussian filter for smoothing prior to down-scaling\nreduces aliasing artifacts.\n\n"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": null,
60+
"metadata": {
61+
"collapsed": false
62+
},
63+
"outputs": [],
64+
"source": [
65+
"import numpy as np\nfrom scipy.ndimage import gaussian_filter\nfrom skimage.transform import rescale\n\nsmoothened_coins = gaussian_filter(orig_coins, sigma=2)\nrescaled_coins = rescale(\n smoothened_coins,\n 0.2,\n mode=\"reflect\",\n anti_aliasing=False,\n)\n\nX = np.reshape(rescaled_coins, (-1, 1))"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {},
71+
"source": [
72+
"## Define structure of the data\n\nPixels are connected to their neighbors.\n\n"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"metadata": {
79+
"collapsed": false
80+
},
81+
"outputs": [],
82+
"source": [
83+
"from sklearn.feature_extraction.image import grid_to_graph\n\nconnectivity = grid_to_graph(*rescaled_coins.shape)"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"## Compute clustering\n\n"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": null,
96+
"metadata": {
97+
"collapsed": false
98+
},
99+
"outputs": [],
100+
"source": [
101+
"import time as time\n\nfrom sklearn.cluster import AgglomerativeClustering\n\nprint(\"Compute structured hierarchical clustering...\")\nst = time.time()\nn_clusters = 27 # number of regions\nward = AgglomerativeClustering(\n n_clusters=n_clusters, linkage=\"ward\", connectivity=connectivity\n)\nward.fit(X)\nlabel = np.reshape(ward.labels_, rescaled_coins.shape)\nprint(f\"Elapsed time: {time.time() - st:.3f}s\")\nprint(f\"Number of pixels: {label.size}\")\nprint(f\"Number of clusters: {np.unique(label).size}\")"
102+
]
103+
},
104+
{
105+
"cell_type": "markdown",
106+
"metadata": {},
107+
"source": [
108+
"## Plot the results on an image\n\nAgglomerative clustering is able to segment each coin however, we have had to\nuse a ``n_cluster`` larger than the number of coins because the segmentation\nis finding a large in the background.\n\n"
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"execution_count": null,
114+
"metadata": {
115+
"collapsed": false
116+
},
117+
"outputs": [],
118+
"source": [
119+
"import matplotlib.pyplot as plt\n\nplt.figure(figsize=(5, 5))\nplt.imshow(rescaled_coins, cmap=plt.cm.gray)\nfor l in range(n_clusters):\n plt.contour(\n label == l,\n colors=[\n plt.cm.nipy_spectral(l / float(n_clusters)),\n ],\n )\nplt.axis(\"off\")\nplt.show()"
30120
]
31121
}
32122
],

dev/_downloads/scikit-learn-docs.zip

13.3 KB
Binary file not shown.
212 Bytes
-162 Bytes
99 Bytes
73 Bytes
62 Bytes

0 commit comments

Comments
 (0)