100 Numpy Exercises - Hints, Discussions & Help

This is the discussion thread for Numpy 100 - an optional set of exercises on numerical computing with Numpy. You can find the notebook here:

Hints: https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_hints.md

Use this thread for:

  • Getting help if you are stuck and need help with a problem
  • Discussing & sharing your approach or code for a problems
  • Working with the community to find the shortest or most efficient solution
  • Helping others to fix errors and improve their solutions

IMPORTANT NOTE: Solving these 100 problems is not necessary for getting the certification. This is an optional exercise.


Use the “Embed Cell” option from Jovian, to share a single cell from your notebook within a forum post. Example:

Learn more: https://medium.com/jovianml/share-and-embed-jupyter-notebooks-online-with-jovian-ml-df709a03064e


some one please help how to solve this…
Create a 2d array with 1 on the border and 0 inside ?

arr = np.ones((5,5))


Help with this question:

  1. Create a 8x8 matrix and fill it with a checkerboard pattern

I found an answer but don’t understand one thing:

import numpy as np
x = np.ones((3,3))
print("Checkerboard pattern:")
x = np.zeros((8,8),dtype=int)
x[1::2,::2] = 1
x[::2,1::2] = 1

I understand the first part of x argument, that is, 1::2 (which means every 2nd row), but how does the second argument, ::2, function?


1 Like

in question 17 what to do is not understandable

Thanks for this exercise. I’d say this is both more interesting and exciting than the assignment 2. Moreover, I’m sure I can learn a lot from this. Even better, I hope I can look up for more creative answers here.

You already have the answer yourself. See if this helps: x[0:] = x[:].

Pls try this:

x = np.arange(12).reshape(2,6)
# play with this array whatever you want

Google is your best friend.

3x3 Identity matrix is
[[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]]

1 Like

Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?
please help how to think on this question to solve

1 Like

Let’s put it this way: there’s a big bag of balls, say 336 (678) balls. There’re 6 cartons, which can hold 7 boxes each, and each of these boxes, there’re 8 partitions in it to store these balls. Number these balls from 0 to 335, and store them into paritions in ascending order. Now, you need to find where the Ball 99 (this is the 100th ball) is stored.

We know 1 box carries 8 balls, and 1 carton carries 7 boxes. In math, we know 1 carton carries 7*8 balls. So, we basically know the Ball 99 is in Carton 1 (the first Carton is Carton 0). From here, I bet you have an idea how to look for Ball 99 with the help of // and %.

Hope this helps.


Is there any resource online where I can find the explanations to these exercises?

Googling these problems returns plenty of solutions for the questions, but not explanations. For problems after the first 20 or so, I’m unable to find explanations (specially ones where the code is already given).

I’d post the problems here but they may end up being too many eventually.

You can find some hints here: https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_hints.md

x = np.ones((6,6))
x[1:-1, 1:-1] = 0

Note: First 1:-1 means 2nd row to (last row - 1) as -1 indicates last one. Second one refers to column.



x[1::2,::2] means row1 to last row with step 2, col0 to last col with step 2
x[::2,1::2] means row0 to last row with step 2, col1 to last col with step 2

1 Like

On question no. 27 i.e. `

27. Consider an integer vector Z, which of these expressions are legal? (★☆☆)

2 << Z >> 2
Z <- Z

What does Legal actually means? I am not able to understand the question does it means runable. Please help.

1 Like

On Question number
35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆)
I tried dividing A = np.array([1,2,3,4]) on numpy 1.19.1


TypeError Traceback (most recent call last)
----> 1 np.divide(A,4,out=A)
TypeError: No loop matching the specified signature and casting was found for ufunc true_divide

Then doing google it was said to use np.true_divide(A,4,out=A) but in my case using that gave same error. But doing simple A=A/4 is giving answer. Please help

Legal means which can run without giving any error.

arr = np.random.randint(420, size=(6, 7, 8))

arr.reshape(-1) puts all the elements of arr into 1-D array.

1 Like