Skip to content

Commit 07ea3f6

Browse files
committed
Pushing the docs to dev/ for branch: master, commit 32ac22870d004b52b06d12f18ff3ffbf20862b3b
1 parent 3a97d36 commit 07ea3f6

File tree

1,035 files changed

+3225
-3185
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,035 files changed

+3225
-3185
lines changed
315 Bytes
Binary file not shown.
310 Bytes
Binary file not shown.

dev/_downloads/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\nprint(__doc__)\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)[:1000]\nkmeans = KMeans(n_clusters=n_colors, random_state=0).fit(image_array_sample)\nprint(\"done in %0.3fs.\" % (time() - t0))\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(\"done in %0.3fs.\" % (time() - t0))\n\n\ncodebook_random = shuffle(image_array, random_state=0)[:n_colors + 1]\nprint(\"Predicting color indices on the full image (random)\")\nt0 = time()\nlabels_random = pairwise_distances_argmin(codebook_random,\n image_array,\n axis=0)\nprint(\"done in %0.3fs.\" % (time() - t0))\n\n\ndef recreate_image(codebook, labels, w, h):\n \"\"\"Recreate the (compressed) image from the code book & labels\"\"\"\n d = codebook.shape[1]\n image = np.zeros((w, h, d))\n label_idx = 0\n for i in range(w):\n for j in range(h):\n image[i][j] = codebook[labels[label_idx]]\n label_idx += 1\n return image\n\n# Display all results, alongside original image\nplt.figure(1)\nplt.clf()\nax = plt.axes([0, 0, 1, 1])\nplt.axis('off')\nplt.title('Original image (96,615 colors)')\nplt.imshow(china)\n\nplt.figure(2)\nplt.clf()\nax = plt.axes([0, 0, 1, 1])\nplt.axis('off')\nplt.title('Quantized image (64 colors, K-Means)')\nplt.imshow(recreate_image(kmeans.cluster_centers_, labels, w, h))\n\nplt.figure(3)\nplt.clf()\nax = plt.axes([0, 0, 1, 1])\nplt.axis('off')\nplt.title('Quantized image (64 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\nprint(__doc__)\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)[:1000]\nkmeans = KMeans(n_clusters=n_colors, random_state=0).fit(image_array_sample)\nprint(\"done in %0.3fs.\" % (time() - t0))\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(\"done in %0.3fs.\" % (time() - t0))\n\n\ncodebook_random = shuffle(image_array, random_state=0)[:n_colors + 1]\nprint(\"Predicting color indices on the full image (random)\")\nt0 = time()\nlabels_random = pairwise_distances_argmin(codebook_random,\n image_array,\n axis=0)\nprint(\"done in %0.3fs.\" % (time() - t0))\n\n\ndef recreate_image(codebook, labels, w, h):\n \"\"\"Recreate the (compressed) image from the code book & labels\"\"\"\n d = codebook.shape[1]\n image = np.zeros((w, h, d))\n label_idx = 0\n for i in range(w):\n for j in range(h):\n image[i][j] = codebook[labels[label_idx]]\n label_idx += 1\n return image\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('Quantized image (64 colors, K-Means)')\nplt.imshow(recreate_image(kmeans.cluster_centers_, labels, w, h))\n\nplt.figure(3)\nplt.clf()\nplt.axis('off')\nplt.title('Quantized image (64 colors, Random)')\nplt.imshow(recreate_image(codebook_random, labels_random, w, h))\nplt.show()"
3030
]
3131
}
3232
],

dev/_downloads/plot_color_quantization.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,18 @@ def recreate_image(codebook, labels, w, h):
8484
# Display all results, alongside original image
8585
plt.figure(1)
8686
plt.clf()
87-
ax = plt.axes([0, 0, 1, 1])
8887
plt.axis('off')
8988
plt.title('Original image (96,615 colors)')
9089
plt.imshow(china)
9190

9291
plt.figure(2)
9392
plt.clf()
94-
ax = plt.axes([0, 0, 1, 1])
9593
plt.axis('off')
9694
plt.title('Quantized image (64 colors, K-Means)')
9795
plt.imshow(recreate_image(kmeans.cluster_centers_, labels, w, h))
9896

9997
plt.figure(3)
10098
plt.clf()
101-
ax = plt.axes([0, 0, 1, 1])
10299
plt.axis('off')
103100
plt.title('Quantized image (64 colors, Random)')
104101
plt.imshow(recreate_image(codebook_random, labels_random, w, h))

dev/_downloads/plot_dict_face_patches.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-
"print(__doc__)\n\nimport time\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n\nfrom sklearn import datasets\nfrom sklearn.cluster import MiniBatchKMeans\nfrom sklearn.feature_extraction.image import extract_patches_2d\n\nfaces = datasets.fetch_olivetti_faces()\n\n# #############################################################################\n# Learn the dictionary of images\n\nprint('Learning the dictionary... ')\nrng = np.random.RandomState(0)\nkmeans = MiniBatchKMeans(n_clusters=81, random_state=rng, verbose=True)\npatch_size = (20, 20)\n\nbuffer = []\nindex = 1\nt0 = time.time()\n\n# The online learning part: cycle over the whole dataset 6 times\nindex = 0\nfor _ in range(6):\n for img in faces.images:\n data = extract_patches_2d(img, patch_size, max_patches=50,\n random_state=rng)\n data = np.reshape(data, (len(data), -1))\n buffer.append(data)\n index += 1\n if index % 10 == 0:\n data = np.concatenate(buffer, axis=0)\n data -= np.mean(data, axis=0)\n data /= np.std(data, axis=0)\n kmeans.partial_fit(data)\n buffer = []\n if index % 100 == 0:\n print('Partial fit of %4i out of %i'\n % (index, 6 * len(faces.images)))\n\ndt = time.time() - t0\nprint('done in %.2fs.' % dt)\n\n# #############################################################################\n# Plot the results\nplt.figure(figsize=(4.2, 4))\nfor i, patch in enumerate(kmeans.cluster_centers_):\n plt.subplot(9, 9, i + 1)\n plt.imshow(patch.reshape(patch_size), cmap=plt.cm.gray,\n interpolation='nearest')\n plt.xticks(())\n plt.yticks(())\n\n\nplt.suptitle('Patches of faces\\nTrain time %.1fs on %d patches' %\n (dt, 8 * len(faces.images)), fontsize=16)\nplt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23)\n\nplt.show()"
29+
"print(__doc__)\n\nimport time\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n\nfrom sklearn import datasets\nfrom sklearn.cluster import MiniBatchKMeans\nfrom sklearn.feature_extraction.image import extract_patches_2d\n\nfaces = datasets.fetch_olivetti_faces()\n\n# #############################################################################\n# Learn the dictionary of images\n\nprint('Learning the dictionary... ')\nrng = np.random.RandomState(0)\nkmeans = MiniBatchKMeans(n_clusters=81, random_state=rng, verbose=True)\npatch_size = (20, 20)\n\nbuffer = []\nt0 = time.time()\n\n# The online learning part: cycle over the whole dataset 6 times\nindex = 0\nfor _ in range(6):\n for img in faces.images:\n data = extract_patches_2d(img, patch_size, max_patches=50,\n random_state=rng)\n data = np.reshape(data, (len(data), -1))\n buffer.append(data)\n index += 1\n if index % 10 == 0:\n data = np.concatenate(buffer, axis=0)\n data -= np.mean(data, axis=0)\n data /= np.std(data, axis=0)\n kmeans.partial_fit(data)\n buffer = []\n if index % 100 == 0:\n print('Partial fit of %4i out of %i'\n % (index, 6 * len(faces.images)))\n\ndt = time.time() - t0\nprint('done in %.2fs.' % dt)\n\n# #############################################################################\n# Plot the results\nplt.figure(figsize=(4.2, 4))\nfor i, patch in enumerate(kmeans.cluster_centers_):\n plt.subplot(9, 9, i + 1)\n plt.imshow(patch.reshape(patch_size), cmap=plt.cm.gray,\n interpolation='nearest')\n plt.xticks(())\n plt.yticks(())\n\n\nplt.suptitle('Patches of faces\\nTrain time %.1fs on %d patches' %\n (dt, 8 * len(faces.images)), fontsize=16)\nplt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23)\n\nplt.show()"
3030
]
3131
}
3232
],

dev/_downloads/plot_dict_face_patches.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
patch_size = (20, 20)
4242

4343
buffer = []
44-
index = 1
4544
t0 = time.time()
4645

4746
# The online learning part: cycle over the whole dataset 6 times

dev/_downloads/plot_digits_kde_sampling.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-
"import numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.datasets import load_digits\nfrom sklearn.neighbors import KernelDensity\nfrom sklearn.decomposition import PCA\nfrom sklearn.model_selection import GridSearchCV\n\n# load the data\ndigits = load_digits()\ndata = digits.data\n\n# project the 64-dimensional data to a lower dimension\npca = PCA(n_components=15, whiten=False)\ndata = pca.fit_transform(digits.data)\n\n# use grid search cross-validation to optimize the bandwidth\nparams = {'bandwidth': np.logspace(-1, 1, 20)}\ngrid = GridSearchCV(KernelDensity(), params)\ngrid.fit(data)\n\nprint(\"best bandwidth: {0}\".format(grid.best_estimator_.bandwidth))\n\n# use the best estimator to compute the kernel density estimate\nkde = grid.best_estimator_\n\n# sample 44 new points from the data\nnew_data = kde.sample(44, random_state=0)\nnew_data = pca.inverse_transform(new_data)\n\n# turn data into a 4x11 grid\nnew_data = new_data.reshape((4, 11, -1))\nreal_data = digits.data[:44].reshape((4, 11, -1))\n\n# plot real digits and resampled digits\nfig, ax = plt.subplots(9, 11, subplot_kw=dict(xticks=[], yticks=[]))\nfor j in range(11):\n ax[4, j].set_visible(False)\n for i in range(4):\n im = ax[i, j].imshow(real_data[i, j].reshape((8, 8)),\n cmap=plt.cm.binary, interpolation='nearest')\n im.set_clim(0, 16)\n im = ax[i + 5, j].imshow(new_data[i, j].reshape((8, 8)),\n cmap=plt.cm.binary, interpolation='nearest')\n im.set_clim(0, 16)\n\nax[0, 5].set_title('Selection from the input data')\nax[5, 5].set_title('\"New\" digits drawn from the kernel density model')\n\nplt.show()"
29+
"import numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.datasets import load_digits\nfrom sklearn.neighbors import KernelDensity\nfrom sklearn.decomposition import PCA\nfrom sklearn.model_selection import GridSearchCV\n\n# load the data\ndigits = load_digits()\n\n# project the 64-dimensional data to a lower dimension\npca = PCA(n_components=15, whiten=False)\ndata = pca.fit_transform(digits.data)\n\n# use grid search cross-validation to optimize the bandwidth\nparams = {'bandwidth': np.logspace(-1, 1, 20)}\ngrid = GridSearchCV(KernelDensity(), params)\ngrid.fit(data)\n\nprint(\"best bandwidth: {0}\".format(grid.best_estimator_.bandwidth))\n\n# use the best estimator to compute the kernel density estimate\nkde = grid.best_estimator_\n\n# sample 44 new points from the data\nnew_data = kde.sample(44, random_state=0)\nnew_data = pca.inverse_transform(new_data)\n\n# turn data into a 4x11 grid\nnew_data = new_data.reshape((4, 11, -1))\nreal_data = digits.data[:44].reshape((4, 11, -1))\n\n# plot real digits and resampled digits\nfig, ax = plt.subplots(9, 11, subplot_kw=dict(xticks=[], yticks=[]))\nfor j in range(11):\n ax[4, j].set_visible(False)\n for i in range(4):\n im = ax[i, j].imshow(real_data[i, j].reshape((8, 8)),\n cmap=plt.cm.binary, interpolation='nearest')\n im.set_clim(0, 16)\n im = ax[i + 5, j].imshow(new_data[i, j].reshape((8, 8)),\n cmap=plt.cm.binary, interpolation='nearest')\n im.set_clim(0, 16)\n\nax[0, 5].set_title('Selection from the input data')\nax[5, 5].set_title('\"New\" digits drawn from the kernel density model')\n\nplt.show()"
3030
]
3131
}
3232
],

dev/_downloads/plot_digits_kde_sampling.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
# load the data
2222
digits = load_digits()
23-
data = digits.data
2423

2524
# project the 64-dimensional data to a lower dimension
2625
pca = PCA(n_components=15, whiten=False)

0 commit comments

Comments
 (0)