 # 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:

• 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.

6 Likes

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

5 Likes

Create a 2d array with 1 on the border and 0 inside ?

arr = np.ones((5,5))
arr[1:-1,1:-1]=0

2 Likes

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
print(x)
``````

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?

Thanks.

2 Likes

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.

1 Like

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

Pls try this:

``````x = np.arange(12).reshape(2,6)
print(x[0,::2])
print(x[1,1::2])
# play with this array whatever you want
``````

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?

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.

3 Likes

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.

1 Like

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

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

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

2 Likes

@cfran064

``````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? (★☆☆)

``````Z**Z
2 << Z >> 2
Z <- Z
1j*Z
Z/1/1
Z<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

np.divide(A,4,out=A)

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

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