Skip to content

Commit c9af39e

Browse files
committed
Pushing the docs to dev/ for branch: main, commit 7ec1bfc2ab7425bcd984d631b5bfeb9082ce11bf
1 parent 28d12ec commit c9af39e

File tree

1,259 files changed

+4492
-4494
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,259 files changed

+4492
-4494
lines changed
Binary file not shown.
Binary file not shown.

dev/_downloads/82ec115874a062f9e8fa17efc63384c0/plot_color_quantization.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"outputs": [],
2828
"source": [
29-
"# Authors: Robert Layton <[email protected]>\n# Olivier Grisel <[email protected]>\n# Mathieu Blondel <[email protected]>\n#\n# License: BSD 3 clause\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.cluster import KMeans\nfrom sklearn.metrics import pairwise_distances_argmin\nfrom sklearn.datasets import load_sample_image\nfrom sklearn.utils import shuffle\nfrom time import time\n\nn_colors = 64\n\n# Load the Summer Palace photo\nchina = load_sample_image(\"china.jpg\")\n\n# Convert to floats instead of the default 8 bits integer coding. Dividing by\n# 255 is important so that plt.imshow behaves works well on float data (need to\n# be in the range [0-1])\nchina = np.array(china, dtype=np.float64) / 255\n\n# Load Image and transform to a 2D numpy array.\nw, h, d = original_shape = tuple(china.shape)\nassert d == 3\nimage_array = np.reshape(china, (w * h, d))\n\nprint(\"Fitting model on a small sub-sample of the data\")\nt0 = time()\nimage_array_sample = shuffle(image_array, random_state=0, n_samples=1_000)\nkmeans = KMeans(n_clusters=n_colors, random_state=0).fit(image_array_sample)\nprint(f\"done in {time() - t0:0.3f}s.\")\n\n# Get labels for all points\nprint(\"Predicting color indices on the full image (k-means)\")\nt0 = time()\nlabels = kmeans.predict(image_array)\nprint(f\"done in {time() - t0:0.3f}s.\")\n\n\ncodebook_random = shuffle(image_array, random_state=0, n_samples=n_colors)\nprint(\"Predicting color indices on the full image (random)\")\nt0 = time()\nlabels_random = pairwise_distances_argmin(codebook_random, image_array, axis=0)\nprint(f\"done in {time() - t0:0.3f}s.\")\n\n\ndef recreate_image(codebook, labels, w, h):\n \"\"\"Recreate the (compressed) image from the code book & labels\"\"\"\n return codebook[labels].reshape(w, h, -1)\n\n\n# Display all results, alongside original image\nplt.figure(1)\nplt.clf()\nplt.axis(\"off\")\nplt.title(\"Original image (96,615 colors)\")\nplt.imshow(china)\n\nplt.figure(2)\nplt.clf()\nplt.axis(\"off\")\nplt.title(f\"Quantized image ({n_colors} colors, K-Means)\")\nplt.imshow(recreate_image(kmeans.cluster_centers_, labels, w, h))\n\nplt.figure(3)\nplt.clf()\nplt.axis(\"off\")\nplt.title(f\"Quantized image ({n_colors} colors, Random)\")\nplt.imshow(recreate_image(codebook_random, labels_random, w, h))\nplt.show()"
29+
"# Authors: Robert Layton <[email protected]>\n# Olivier Grisel <[email protected]>\n# Mathieu Blondel <[email protected]>\n#\n# License: BSD 3 clause\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.cluster import KMeans\nfrom sklearn.metrics import pairwise_distances_argmin\nfrom sklearn.datasets import load_sample_image\nfrom sklearn.utils import shuffle\nfrom time import time\n\nn_colors = 64\n\n# Load the Summer Palace photo\nchina = load_sample_image(\"china.jpg\")\n\n# Convert to floats instead of the default 8 bits integer coding. Dividing by\n# 255 is important so that plt.imshow behaves works well on float data (need to\n# be in the range [0-1])\nchina = np.array(china, dtype=np.float64) / 255\n\n# Load Image and transform to a 2D numpy array.\nw, h, d = original_shape = tuple(china.shape)\nassert d == 3\nimage_array = np.reshape(china, (w * h, d))\n\nprint(\"Fitting model on a small sub-sample of the data\")\nt0 = time()\nimage_array_sample = shuffle(image_array, random_state=0, n_samples=1_000)\nkmeans = KMeans(n_clusters=n_colors, n_init=\"auto\", random_state=0).fit(\n image_array_sample\n)\nprint(f\"done in {time() - t0:0.3f}s.\")\n\n# Get labels for all points\nprint(\"Predicting color indices on the full image (k-means)\")\nt0 = time()\nlabels = kmeans.predict(image_array)\nprint(f\"done in {time() - t0:0.3f}s.\")\n\n\ncodebook_random = shuffle(image_array, random_state=0, n_samples=n_colors)\nprint(\"Predicting color indices on the full image (random)\")\nt0 = time()\nlabels_random = pairwise_distances_argmin(codebook_random, image_array, axis=0)\nprint(f\"done in {time() - t0:0.3f}s.\")\n\n\ndef recreate_image(codebook, labels, w, h):\n \"\"\"Recreate the (compressed) image from the code book & labels\"\"\"\n return codebook[labels].reshape(w, h, -1)\n\n\n# Display all results, alongside original image\nplt.figure(1)\nplt.clf()\nplt.axis(\"off\")\nplt.title(\"Original image (96,615 colors)\")\nplt.imshow(china)\n\nplt.figure(2)\nplt.clf()\nplt.axis(\"off\")\nplt.title(f\"Quantized image ({n_colors} colors, K-Means)\")\nplt.imshow(recreate_image(kmeans.cluster_centers_, labels, w, h))\n\nplt.figure(3)\nplt.clf()\nplt.axis(\"off\")\nplt.title(f\"Quantized image ({n_colors} colors, Random)\")\nplt.imshow(recreate_image(codebook_random, labels_random, w, h))\nplt.show()"
3030
]
3131
}
3232
],

dev/_downloads/d0e47fc5f3661efb101abfd4d9461afe/plot_color_quantization.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
print("Fitting model on a small sub-sample of the data")
5353
t0 = time()
5454
image_array_sample = shuffle(image_array, random_state=0, n_samples=1_000)
55-
kmeans = KMeans(n_clusters=n_colors, random_state=0).fit(image_array_sample)
55+
kmeans = KMeans(n_clusters=n_colors, n_init="auto", random_state=0).fit(
56+
image_array_sample
57+
)
5658
print(f"done in {time() - t0:0.3f}s.")
5759

5860
# Get labels for all points

dev/_downloads/scikit-learn-docs.zip

5.51 KB
Binary file not shown.
187 Bytes
178 Bytes
202 Bytes
212 Bytes
80 Bytes

0 commit comments

Comments
 (0)