Skip to main content

Numpy 100 Knocks Answers

Solutions to the numpy-100 exercises.

Q1. Import the numpy package under the name np​

Install numpy.

Answer​

import numpy as np

Q2.Print the numpy version and the configuration​

Check the numpy version.

Answer​

import numpy as np

print(np.__version__)

It was 1.20.2.

Q3.Create a null vector of size 10​

Create an array of zeros with size 10.

Answer​

import numpy as np

a = np.zeros(10)
print(a)

Q4.How to find the memory size of any array​

Check the memory size of an array.

Answer​

a = np.zeros((10, 10))
print(a.nbytes) #800

The byte size of a single element can be obtained with:

a.itemsize

Q5.How to get the documentation of the numpy add function from the command line?​

How to view the documentation for numpy's add function from the command line.

Answer​

python -c "import numpy; numpy.info(numpy.add)"

Q6.Create a null vector of size 10 but the fifth value which is 1​

Create a zero vector of size 10 where only the 5th value is 1.

Answer​

a = np.zeros((10))
a[4] = 1
print(a)

Q7.Create a vector with values ranging from 10 to 49​

Create an array with values in the range 10-49.

Answer​

b = np.arange(10, 50)
print(b)

Q8. Reverse a vector (first element becomes last)​

Reverse a vector.

Answer​

b = np.arange(10, 50)
print(b[::-1])

Q9.Create a 3x3 matrix with values ranging from 0 to 8​

Fill a 3x3 matrix with values 0-8.

Answer​

c = np.arange(0, 9)
print(c.reshape(3, 3))

reshape seems important to know.

Q10. Find indices of non-zero elements from [1,2,0,0,4,0]​

Find elements that are not zero from a vector.

Answer​

d = np.array([1, 2, 0, 0, 4, 0])
print(np.where(d != 0))

Q11. Create a 3x3 identity matrix​

Create an identity matrix.

Answer​

e = np.eye(3)
print(e)

Q12. Create a 3x3x3 array with random values​

Create a random array.

Answer​

f = np.random.random((3, 3, 3))
print(f)

Q13. Create a 10x10 array with random values and find the minimum and maximum values​

Get the maximum and minimum values from a random array.

Answer​

f = np.random.random((10, 10))
print(np.max(f), np.min(f))

Q14.Create a random vector of size 30 and find the mean value​

Get the mean value from a random vector.

Answer​

g = np.random.random(30)
print(np.mean(g))

Q15. Create a 2d array with 1 on the border and 0 inside​

Create a 2D array with 0 on the border and 1 inside.

Answer​

size = 5
h = np.zeros((size, size))
h[1:size-1, 1:size-1] = 1
print(h)

Q16.How to add a border (filled with 0's) around an existing array?​

Add zero padding around the array.

Answer​

i = np.ones((5, 5))
print(np.pad(i, 1))

Need to become more familiar with pad as well.

Q17.What is the result of the following expression?​

Answer​

print(0 * np.nan)
print(np.nan == np.nan)
print(np.inf > np.nan)
print(np.nan - np.nan)
print(np.nan in set([np.nan]))
print(0.3 == 3 * 0.1)

Execution result:

nan
False
False
nan
True
False

Need to understand the characteristics of np.nan thoroughly.

Q18.Create a 5x5 matrix with values 1,2,3,4 just below the diagonal​

Place values 1, 2, 3, 4 just below the diagonal.

Answer​

j = np.diag(np.arange(4) + 1, k=-1)
print(j)

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

Create a checkerboard pattern in a matrix.

Answer​

j = np.zeros((8, 8))
j[1::2, ::2] = 1
j[::2, 1::2] = 1
print(j)

I did not know the meaning of :: in array slicing.

Q20.Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element​

Extract the 100th element.

Answer​

print(np.unravel_index(99, (6, 7, 8)))

Q21. Create a checkerboard 8x8 matrix using the tile function​

Create a checkerboard pattern using the tile function.

Answer​

j = np.tile(((0, 1), (1, 0)), (4, 4))
print(j)

Q22. Normalize a 5x5 random matrix​

Normalize a matrix.

Answer​

k = np.random.random((5, 5))
k = (k - np.mean(k))/np.std(k)
print(k)

Q23. Create a custom dtype that describes a color as four unsigned bytes (RGBA)​

Create a custom dtype.

Answer​

color = np.dtype([("r", np.ubyte, 1),
("g", np.ubyte, 1),
("b", np.ubyte, 1),
("a", np.ubyte, 1)])

Creating a custom dtype here. I wonder if there will be opportunities to use this feature.

Q24.Multiply a 5x3 matrix by a 3x2 matrix (real matrix product)​

Perform matrix multiplication.

Answer​

p = np.ones((5, 3))
m = np.ones((3, 2))
n = np.dot(p, m)
print(n)

Q25 Given a 1D array, negate all elements which are between 3 and 8, in place​

Negate elements between 3 and 8.

Answer​

q = np.arange(10)
q[(3 < q) & (q <= 8)] *= -1
print(q)

Saw this pattern frequently in the Image Processing 100 Knocks as well.

Q26.What is the output of the following script?​

Check the output of the following:

print(sum(range(5),-1))
from numpy import *
print(sum(range(5),-1))

Result​

9
10

The first sum includes -1 in the computation, while the second sum specifies axis=-1.

Are the following expressions valid? (Z is an integer)

Z = np.arange(5)
Z**Z
2 << Z >> 2
Z <- Z
1j*Z
Z/1/1
Z<Z>Z

Result​

[  1   1   4  27 256]
[0 1 2 4 8]
[False False False False False]
[0.+0.j 0.+1.j 0.+2.j 0.+3.j 0.+4.j]
[0. 1. 2. 3. 4.]
error
  • Shows the result of element-wise exponentiation
[0^0,1^1,2^2,3^3,4^4]
  • Performing bitwise operations

  • Comparison of each element

  • Converting to imaginary numbers

  • Dividing by 1 twice

  • Two comparison operators cannot be chained