NumPy - Exercise
import numpy as np
Create an array of 10 zeros
np.zeros(10)
Create an array of 10 ones
np.ones(10)
Create an array of 10 fives
np.ones(10) * 5
Create an array of the integers from 10 to 50
np.arange(10,51)
Create an array of all the even integers from 10 to 50
print(np.arange(10,51,2))
x = np.arange(10,51)
y = (x%2 == 0)
z = x[y]
print(z)
Create a 3x3 matrix with values ranging from 0 to 8
np.arange(0,9).reshape((3,3))
Create a 3x3 identity matrix
np.eye(3)
Generate a random number between 0 and 1
np.random.randint(0,1)
Create an array of 20 linearly spaced points between 0 and 1
np.linspace(0,1,20)
Numpy Indexing and Selection
Now you will be given a few matrices, and be asked to replicate the resulting matrix outputs:
mat = np.arange(1,26).reshape(5,5)
mat
mat[2: ,1:]
mat[3,4]
mat[0:3,1:2]
mat[4]
mat[3:]
Get the sum of all the values in mat
mat.sum()
Get the standard deviation of the values in mat
mat.std()
Get the sum of all the columns in mat
mat.sum(axis=1)
Get the sum of all the rows in mat
mat.sum(axis=0)