Handson-ml: plot_digits

Created on 18 Jul 2018  Â·  5Comments  Â·  Source: ageron/handson-ml

I'm new to these libraries and while I was running the code in Chapter 3:

cl_a, cl_b = 3, 5
X_aa = X_train[(y_train == cl_a) & (y_train_pred == cl_a)]
X_ab = X_train[(y_train == cl_a) & (y_train_pred == cl_b)]
X_ba = X_train[(y_train == cl_b) & (y_train_pred == cl_a)]
X_bb = X_train[(y_train == cl_b) & (y_train_pred == cl_b)]

plt.figure(figsize=(8,8))
plt.subplot(221); plot_digits(X_aa[:25], images_per_row=5)
plt.subplot(222); plot_digits(X_ab[:25], images_per_row=5)
plt.subplot(223); plot_digits(X_ba[:25], images_per_row=5)
plt.subplot(224); plot_digits(X_bb[:25], images_per_row=5)

I get the following error:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-91-df1c82686923> in <module>()
      1 plt.figure(figsize=(8,8))
----> 2 plt.subplot(221); plot_digits(X_aa[:25], images_per_row = 5)
      3 plt.subplot(222); plot_digits(X_ab[:25], images_per_row=5)
      4 plt.subplot(223); plot_digits(X_ba[:25], images_per_row=5)
      5 plt.subplot(224); plot_digits(X_bb[:25], images_per_row=5)

NameError: name 'plot_digits' is not defined

I searched online but can't find resources on this plot_digits function. Can you help?

Most helpful comment

Hi @batblah ,

This function is defined in the Jupyter notebook (in cell In 8):

# EXTRA
def plot_digits(instances, images_per_row=10, **options):
    size = 28
    images_per_row = min(len(instances), images_per_row)
    images = [instance.reshape(size,size) for instance in instances]
    n_rows = (len(instances) - 1) // images_per_row + 1
    row_images = []
    n_empty = n_rows * images_per_row - len(instances)
    images.append(np.zeros((size, size * n_empty)))
    for row in range(n_rows):
        rimages = images[row * images_per_row : (row + 1) * images_per_row]
        row_images.append(np.concatenate(rimages, axis=1))
    image = np.concatenate(row_images, axis=0)
    plt.imshow(image, cmap = matplotlib.cm.binary, **options)
    plt.axis("off")

Make sure you run the cells in the Jupyter notebooks
in the right order, everything should work fine.

Hope this helps,
Aurélien

All 5 comments

Hi @batblah ,

This function is defined in the Jupyter notebook (in cell In 8):

# EXTRA
def plot_digits(instances, images_per_row=10, **options):
    size = 28
    images_per_row = min(len(instances), images_per_row)
    images = [instance.reshape(size,size) for instance in instances]
    n_rows = (len(instances) - 1) // images_per_row + 1
    row_images = []
    n_empty = n_rows * images_per_row - len(instances)
    images.append(np.zeros((size, size * n_empty)))
    for row in range(n_rows):
        rimages = images[row * images_per_row : (row + 1) * images_per_row]
        row_images.append(np.concatenate(rimages, axis=1))
    image = np.concatenate(row_images, axis=0)
    plt.imshow(image, cmap = matplotlib.cm.binary, **options)
    plt.axis("off")

Make sure you run the cells in the Jupyter notebooks
in the right order, everything should work fine.

Hope this helps,
Aurélien

thank you so much.

On Tue, Jul 24, 2018 at 6:40 PM Aurélien Geron notifications@github.com
wrote:

Hi @batblah https://github.com/batblah ,

This function is defined in the Jupyter notebook (in cell In 8):

EXTRAdef plot_digits(instances, images_per_row=10, **options):

size = 28
images_per_row = min(len(instances), images_per_row)
images = [instance.reshape(size,size) for instance in instances]
n_rows = (len(instances) - 1) // images_per_row + 1
row_images = []
n_empty = n_rows * images_per_row - len(instances)
images.append(np.zeros((size, size * n_empty)))
for row in range(n_rows):
    rimages = images[row * images_per_row : (row + 1) * images_per_row]
    row_images.append(np.concatenate(rimages, axis=1))
image = np.concatenate(row_images, axis=0)
plt.imshow(image, cmap = matplotlib.cm.binary, **options)
plt.axis("off")

Make sure you run the cells in the Jupyter notebooks
in the right order, everything should work fine.

Hope this helps,
Aurélien

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/ageron/handson-ml/issues/257#issuecomment-407400563,
or mute the thread
https://github.com/notifications/unsubscribe-auth/Al-gLHaMUnuTLQB6u5ACBTAueGn4wzsUks5uJxzZgaJpZM4VU5C2
.

Thanks for your reply.

Hey after defining that 'plot_digit' function I am getting this new zero divisibility error!

`---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-64-46cb86a220bd> in <module>
      2 plt.subplot(221);plot_digits(X_aa[:25],images_per_row = 5)
      3 plt.subplot(222);plot_digits(X_ab[:25],images_per_row=5)
----> 4 plt.subplot(223);plot_digits(X_ba[:25],images_per_row = 5)
      5 plt.subplot(224);plot_digits(X_bb[:25],images_per_row=5)
      6 plt.show()

<ipython-input-63-a42fe1f80d92> in plot_digits(instances, images_per_row, **options)
      4     images_per_row = min(len(instances), images_per_row)
      5     images = [instance.reshape(size,size) for instance in instances]
----> 6     n_rows = (len(instances) - 1) // images_per_row + 1
      7     row_images = []
      8     n_empty = n_rows * images_per_row - len(instances)

ZeroDivisionError: integer division or modulo by zero`

Hi @XxAakashxX!

So it seems that when we define cl_a and cl_b, from now on we have to define them as characters and not integers (as the y_train and y_train_pred labels are characters and not integers)

So in your code, replace
cl_a, cl_b = 3, 5

with
cl_a, cl_b = '3', '5'

Hope this helps!

Was this page helpful?
0 / 5 - 0 ratings