Merge branch 'edition2'
commit
5822444ba7
|
@ -197,16 +197,24 @@
|
||||||
"def plot_digits(instances, images_per_row=10, **options):\n",
|
"def plot_digits(instances, images_per_row=10, **options):\n",
|
||||||
" size = 28\n",
|
" size = 28\n",
|
||||||
" images_per_row = min(len(instances), images_per_row)\n",
|
" images_per_row = min(len(instances), images_per_row)\n",
|
||||||
" images = [instance.reshape(size,size) for instance in instances]\n",
|
" # This is equivalent to n_rows = ceil(len(instances) / images_per_row):\n",
|
||||||
" n_rows = (len(instances) - 1) // images_per_row + 1\n",
|
" n_rows = (len(instances) - 1) // images_per_row + 1\n",
|
||||||
" row_images = []\n",
|
"\n",
|
||||||
|
" # Append empty images to fill the end of the grid, if needed:\n",
|
||||||
" n_empty = n_rows * images_per_row - len(instances)\n",
|
" n_empty = n_rows * images_per_row - len(instances)\n",
|
||||||
" images.append(np.zeros((size, size * n_empty)))\n",
|
" padded_instances = np.concatenate([instances, np.zeros((n_empty, size * size))], axis=0)\n",
|
||||||
" for row in range(n_rows):\n",
|
"\n",
|
||||||
" rimages = images[row * images_per_row : (row + 1) * images_per_row]\n",
|
" # Reshape the array so it's organized as a grid containing 28×28 images:\n",
|
||||||
" row_images.append(np.concatenate(rimages, axis=1))\n",
|
" image_grid = padded_instances.reshape((n_rows, images_per_row, size, size))\n",
|
||||||
" image = np.concatenate(row_images, axis=0)\n",
|
"\n",
|
||||||
" plt.imshow(image, cmap = mpl.cm.binary, **options)\n",
|
" # Combine axes 0 and 2 (vertical image grid axis, and vertical image axis),\n",
|
||||||
|
" # and axes 1 and 3 (horizontal axes). We first need to move the axes that we\n",
|
||||||
|
" # want to combine next to each other, using transpose(), and only then we\n",
|
||||||
|
" # can reshape:\n",
|
||||||
|
" big_image = image_grid.transpose(0, 2, 1, 3).reshape(n_rows * size,\n",
|
||||||
|
" images_per_row * size)\n",
|
||||||
|
" # Now that we have a big image, we just need to show it:\n",
|
||||||
|
" plt.imshow(big_image, cmap = mpl.cm.binary, **options)\n",
|
||||||
" plt.axis(\"off\")"
|
" plt.axis(\"off\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
|
@ -947,19 +947,28 @@
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
|
"# EXTRA\n",
|
||||||
"def plot_digits(instances, images_per_row=5, **options):\n",
|
"def plot_digits(instances, images_per_row=5, **options):\n",
|
||||||
" size = 28\n",
|
" size = 28\n",
|
||||||
" images_per_row = min(len(instances), images_per_row)\n",
|
" images_per_row = min(len(instances), images_per_row)\n",
|
||||||
" images = [instance.reshape(size,size) for instance in instances]\n",
|
" # This is equivalent to n_rows = ceil(len(instances) / images_per_row):\n",
|
||||||
" n_rows = (len(instances) - 1) // images_per_row + 1\n",
|
" n_rows = (len(instances) - 1) // images_per_row + 1\n",
|
||||||
" row_images = []\n",
|
"\n",
|
||||||
|
" # Append empty images to fill the end of the grid, if needed:\n",
|
||||||
" n_empty = n_rows * images_per_row - len(instances)\n",
|
" n_empty = n_rows * images_per_row - len(instances)\n",
|
||||||
" images.append(np.zeros((size, size * n_empty)))\n",
|
" padded_instances = np.concatenate([instances, np.zeros((n_empty, size * size))], axis=0)\n",
|
||||||
" for row in range(n_rows):\n",
|
"\n",
|
||||||
" rimages = images[row * images_per_row : (row + 1) * images_per_row]\n",
|
" # Reshape the array so it's organized as a grid containing 28×28 images:\n",
|
||||||
" row_images.append(np.concatenate(rimages, axis=1))\n",
|
" image_grid = padded_instances.reshape((n_rows, images_per_row, size, size))\n",
|
||||||
" image = np.concatenate(row_images, axis=0)\n",
|
"\n",
|
||||||
" plt.imshow(image, cmap = mpl.cm.binary, **options)\n",
|
" # Combine axes 0 and 2 (vertical image grid axis, and vertical image axis),\n",
|
||||||
|
" # and axes 1 and 3 (horizontal axes). We first need to move the axes that we\n",
|
||||||
|
" # want to combine next to each other, using transpose(), and only then we\n",
|
||||||
|
" # can reshape:\n",
|
||||||
|
" big_image = image_grid.transpose(0, 2, 1, 3).reshape(n_rows * size,\n",
|
||||||
|
" images_per_row * size)\n",
|
||||||
|
" # Now that we have a big image, we just need to show it:\n",
|
||||||
|
" plt.imshow(big_image, cmap = mpl.cm.binary, **options)\n",
|
||||||
" plt.axis(\"off\")"
|
" plt.axis(\"off\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue