Skip to content

Commit 0c0875a

Browse files
committed
Pushing the docs to dev/ for branch: main, commit f449af888e5b45e11436d309a0b2e46850ae328f
1 parent ea8aa9a commit 0c0875a

File tree

1,231 files changed

+4536
-4361
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,231 files changed

+4536
-4361
lines changed

dev/_downloads/006fc185672e58b056a5c134db26935c/plot_coin_segmentation.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"cell_type": "markdown",
1616
"metadata": {},
1717
"source": [
18-
"\n# Segmenting the picture of greek coins in regions\n\nThis example uses `spectral_clustering` on a graph created from\nvoxel-to-voxel difference on an image to break this image into multiple\npartly-homogeneous regions.\n\nThis procedure (spectral clustering on an image) is an efficient\napproximate solution for finding normalized graph cuts.\n\nThere are two options to assign labels:\n\n* with 'kmeans' spectral clustering will cluster samples in the embedding space\n using a kmeans algorithm\n* whereas 'discrete' will iteratively search for the closest partition\n space to the embedding space.\n"
18+
"\n# Segmenting the picture of greek coins in regions\n\nThis example uses `spectral_clustering` on a graph created from\nvoxel-to-voxel difference on an image to break this image into multiple\npartly-homogeneous regions.\n\nThis procedure (spectral clustering on an image) is an efficient\napproximate solution for finding normalized graph cuts.\n\nThere are three options to assign labels:\n\n* 'kmeans' spectral clustering clusters samples in the embedding space\n using a kmeans algorithm\n* 'discrete' iteratively searches for the closest partition\n space to the embedding space of spectral clustering.\n* 'cluster_qr' assigns labels using the QR factorization with pivoting\n that directly determines the partition in the embedding space.\n"
1919
]
2020
},
2121
{
@@ -26,14 +26,14 @@
2626
},
2727
"outputs": [],
2828
"source": [
29-
"# Author: Gael Varoquaux <[email protected]>, Brian Cheung\n# License: BSD 3 clause\n\nimport time\n\nimport numpy as np\nfrom scipy.ndimage.filters import gaussian_filter\nimport matplotlib.pyplot as plt\nimport skimage\nfrom skimage.data import coins\nfrom skimage.transform import rescale\n\nfrom sklearn.feature_extraction import image\nfrom sklearn.cluster import spectral_clustering\nfrom sklearn.utils.fixes import parse_version\n\n# these were introduced in skimage-0.14\nif parse_version(skimage.__version__) >= parse_version(\"0.14\"):\n rescale_params = {\"anti_aliasing\": False, \"multichannel\": False}\nelse:\n rescale_params = {}\n\n# load the coins as a numpy array\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(smoothened_coins, 0.2, mode=\"reflect\", **rescale_params)\n\n# Convert the image into a graph with the value of the gradient on the\n# edges.\ngraph = image.img_to_graph(rescaled_coins)\n\n# Take a decreasing function of the gradient: an exponential\n# The smaller beta is, the more independent the segmentation is of the\n# actual image. For beta=1, the segmentation is close to a voronoi\nbeta = 10\neps = 1e-6\ngraph.data = np.exp(-beta * graph.data / graph.data.std()) + eps\n\n# Apply spectral clustering (this step goes much faster if you have pyamg\n# installed)\nN_REGIONS = 25"
29+
"# Author: Gael Varoquaux <[email protected]>\n# Brian Cheung\n# Andrew Knyazev <[email protected]>\n# License: BSD 3 clause\n\nimport time\n\nimport numpy as np\nfrom scipy.ndimage.filters import gaussian_filter\nimport matplotlib.pyplot as plt\nimport skimage\nfrom skimage.data import coins\nfrom skimage.transform import rescale\n\nfrom sklearn.feature_extraction import image\nfrom sklearn.cluster import spectral_clustering\nfrom sklearn.utils.fixes import parse_version\n\n# these were introduced in skimage-0.14\nif parse_version(skimage.__version__) >= parse_version(\"0.14\"):\n rescale_params = {\"anti_aliasing\": False, \"multichannel\": False}\nelse:\n rescale_params = {}\n\n# load the coins as a numpy array\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(smoothened_coins, 0.2, mode=\"reflect\", **rescale_params)\n\n# Convert the image into a graph with the value of the gradient on the\n# edges.\ngraph = image.img_to_graph(rescaled_coins)\n\n# Take a decreasing function of the gradient: an exponential\n# The smaller beta is, the more independent the segmentation is of the\n# actual image. For beta=1, the segmentation is close to a voronoi\nbeta = 10\neps = 1e-6\ngraph.data = np.exp(-beta * graph.data / graph.data.std()) + eps\n\n# The number of segmented regions to display needs to be chosen manually.\n# The current version of 'spectral_clustering' does not support determining\n# the number of good quality clusters automatically.\nn_regions = 26"
3030
]
3131
},
3232
{
3333
"cell_type": "markdown",
3434
"metadata": {},
3535
"source": [
36-
"Visualize the resulting regions\n\n"
36+
"Compute and visualize the resulting regions\n\n"
3737
]
3838
},
3939
{
@@ -44,7 +44,7 @@
4444
},
4545
"outputs": [],
4646
"source": [
47-
"for assign_labels in (\"kmeans\", \"discretize\"):\n t0 = time.time()\n labels = spectral_clustering(\n graph, n_clusters=N_REGIONS, assign_labels=assign_labels, random_state=42\n )\n t1 = time.time()\n labels = labels.reshape(rescaled_coins.shape)\n\n plt.figure(figsize=(5, 5))\n plt.imshow(rescaled_coins, cmap=plt.cm.gray)\n for l in range(N_REGIONS):\n plt.contour(labels == l, colors=[plt.cm.nipy_spectral(l / float(N_REGIONS))])\n plt.xticks(())\n plt.yticks(())\n title = \"Spectral clustering: %s, %.2fs\" % (assign_labels, (t1 - t0))\n print(title)\n plt.title(title)\nplt.show()"
47+
"# Computing a few extra eigenvectors may speed up the eigen_solver.\n# The spectral clustering quality may also benetif from requesting\n# extra regions for segmentation.\nn_regions_plus = 3\n\n# Apply spectral clustering using the default eigen_solver='arpack'.\n# Any implemented solver can be used: eigen_solver='arpack', 'lobpcg', or 'amg'.\n# Choosing eigen_solver='amg' requires an extra package called 'pyamg'.\n# The quality of segmentation and the speed of calculations is mostly determined\n# by the choice of the solver and the value of the tolerance 'eigen_tol'.\n# TODO: varying eigen_tol seems to have no effect for 'lobpcg' and 'amg' #21243.\nfor assign_labels in (\"kmeans\", \"discretize\", \"cluster_qr\"):\n t0 = time.time()\n labels = spectral_clustering(\n graph,\n n_clusters=(n_regions + n_regions_plus),\n eigen_tol=1e-7,\n assign_labels=assign_labels,\n random_state=42,\n )\n\n t1 = time.time()\n labels = labels.reshape(rescaled_coins.shape)\n plt.figure(figsize=(5, 5))\n plt.imshow(rescaled_coins, cmap=plt.cm.gray)\n\n plt.xticks(())\n plt.yticks(())\n title = \"Spectral clustering: %s, %.2fs\" % (assign_labels, (t1 - t0))\n print(title)\n plt.title(title)\n for l in range(n_regions):\n colors = [plt.cm.nipy_spectral((l + 4) / float(n_regions + 4))]\n plt.contour(labels == l, colors=colors)\n # To view individual segments as appear comment in plt.pause(0.5)\nplt.show()\n\n# TODO: After #21194 is merged and #21243 is fixed, check which eigen_solver\n# is the best and set eigen_solver='arpack', 'lobpcg', or 'amg' and eigen_tol\n# explicitly in this example."
4848
]
4949
}
5050
],
Binary file not shown.

dev/_downloads/2e86a4838807f09bbbb529d9643d45ab/plot_coin_segmentation.py

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@
1010
This procedure (spectral clustering on an image) is an efficient
1111
approximate solution for finding normalized graph cuts.
1212
13-
There are two options to assign labels:
13+
There are three options to assign labels:
1414
15-
* with 'kmeans' spectral clustering will cluster samples in the embedding space
15+
* 'kmeans' spectral clustering clusters samples in the embedding space
1616
using a kmeans algorithm
17-
* whereas 'discrete' will iteratively search for the closest partition
18-
space to the embedding space.
19-
17+
* 'discrete' iteratively searches for the closest partition
18+
space to the embedding space of spectral clustering.
19+
* 'cluster_qr' assigns labels using the QR factorization with pivoting
20+
that directly determines the partition in the embedding space.
2021
"""
2122

22-
# Author: Gael Varoquaux <[email protected]>, Brian Cheung
23+
# Author: Gael Varoquaux <[email protected]>
24+
# Brian Cheung
25+
# Andrew Knyazev <[email protected]>
2326
# License: BSD 3 clause
2427

2528
import time
@@ -61,28 +64,51 @@
6164
eps = 1e-6
6265
graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps
6366

64-
# Apply spectral clustering (this step goes much faster if you have pyamg
65-
# installed)
66-
N_REGIONS = 25
67+
# The number of segmented regions to display needs to be chosen manually.
68+
# The current version of 'spectral_clustering' does not support determining
69+
# the number of good quality clusters automatically.
70+
n_regions = 26
6771

6872
# %%
69-
# Visualize the resulting regions
70-
71-
for assign_labels in ("kmeans", "discretize"):
73+
# Compute and visualize the resulting regions
74+
75+
# Computing a few extra eigenvectors may speed up the eigen_solver.
76+
# The spectral clustering quality may also benetif from requesting
77+
# extra regions for segmentation.
78+
n_regions_plus = 3
79+
80+
# Apply spectral clustering using the default eigen_solver='arpack'.
81+
# Any implemented solver can be used: eigen_solver='arpack', 'lobpcg', or 'amg'.
82+
# Choosing eigen_solver='amg' requires an extra package called 'pyamg'.
83+
# The quality of segmentation and the speed of calculations is mostly determined
84+
# by the choice of the solver and the value of the tolerance 'eigen_tol'.
85+
# TODO: varying eigen_tol seems to have no effect for 'lobpcg' and 'amg' #21243.
86+
for assign_labels in ("kmeans", "discretize", "cluster_qr"):
7287
t0 = time.time()
7388
labels = spectral_clustering(
74-
graph, n_clusters=N_REGIONS, assign_labels=assign_labels, random_state=42
89+
graph,
90+
n_clusters=(n_regions + n_regions_plus),
91+
eigen_tol=1e-7,
92+
assign_labels=assign_labels,
93+
random_state=42,
7594
)
95+
7696
t1 = time.time()
7797
labels = labels.reshape(rescaled_coins.shape)
78-
7998
plt.figure(figsize=(5, 5))
8099
plt.imshow(rescaled_coins, cmap=plt.cm.gray)
81-
for l in range(N_REGIONS):
82-
plt.contour(labels == l, colors=[plt.cm.nipy_spectral(l / float(N_REGIONS))])
100+
83101
plt.xticks(())
84102
plt.yticks(())
85103
title = "Spectral clustering: %s, %.2fs" % (assign_labels, (t1 - t0))
86104
print(title)
87105
plt.title(title)
106+
for l in range(n_regions):
107+
colors = [plt.cm.nipy_spectral((l + 4) / float(n_regions + 4))]
108+
plt.contour(labels == l, colors=colors)
109+
# To view individual segments as appear comment in plt.pause(0.5)
88110
plt.show()
111+
112+
# TODO: After #21194 is merged and #21243 is fixed, check which eigen_solver
113+
# is the best and set eigen_solver='arpack', 'lobpcg', or 'amg' and eigen_tol
114+
# explicitly in this example.
Binary file not shown.

dev/_downloads/scikit-learn-docs.zip

67.1 KB
Binary file not shown.
19 Bytes
372 Bytes
100 Bytes
335 Bytes
13 Bytes

0 commit comments

Comments
 (0)