NumPy Exercises

NumPy Exercises#

  1. Import the numpy package under the name np (★☆☆)

  1. Print the numpy version and the configuration (★☆☆)

  1. Create a null vector of size 10 (★☆☆)

  1. How to find the memory size of any array (★☆☆)

  1. How to get the documentation of the numpy add function from the command line? (★☆☆)

  1. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)

  1. Create a vector with values ranging from 10 to 49 (★☆☆)

  1. Reverse a vector (first element becomes last) (★☆☆)

  1. Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)

  1. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)

  1. Create a 3x3 identity matrix (★☆☆)

  1. Create a 3x3x3 array with random values (★☆☆)

  1. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)

  1. Create a random vector of size 30 and find the mean value (★☆☆)

  1. Create a 2d array with 1 on the border and 0 inside (★☆☆)

  1. How to add a border (filled with 0’s) around an existing array? (★☆☆)

  1. What is the result of the following expression? (★☆☆)

0 * np.nan
np.nan == np.nan
np.inf > np.nan
np.nan - np.nan
np.nan in set([np.nan])
0.3 == 3 * 0.1
  1. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)

  1. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)

  1. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element? (★☆☆)

  1. Create a checkerboard 8x8 matrix using the tile function (★☆☆)

  1. Normalize a 5x5 random matrix (★☆☆)

  1. Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆)

  1. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)

  1. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)

  1. What is the output of the following script? (★☆☆)

# Author: Jake VanderPlas

print(sum(range(5),-1))
from numpy import *
print(sum(range(5),-1))
  1. 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
  1. What are the result of the following expressions? (★☆☆)

np.array(0) / np.array(0)
np.array(0) // np.array(0)
np.array([np.nan]).astype(int).astype(float)
  1. How to round away from zero a float array ? (★☆☆)

  1. How to find common values between two arrays? (★☆☆)

  1. How to ignore all numpy warnings (not recommended)? (★☆☆)

  1. Is the following expressions true? (★☆☆)

np.sqrt(-1) == np.emath.sqrt(-1)
  1. How to get the dates of yesterday, today and tomorrow? (★☆☆)