Numpy Flashcards

1
Q

create identity matrix of dimension n

A

a=np.eye(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

convert a 1D array to a 3D array

A

a=np.array([i for i in range(27)]

y=a.reshape((3,3,3))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

convert all elements of array from float to int, from binary to boolean

A

y=a.astype(‘int’)

y=a.astype(‘bool’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

From 2 numpy arrays, extract the indexes in which the elements in the 2 arrays match

A

print(np.where(a == b))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Output a sequence of equally gapped 5 numbers in the range 0 to 100

A

o = np.linspace(0, 100, 5)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Output a matrix (numpy array) of dimension 2-by-3 with each and every value equal to 5

A

o = np.full((2, 3), 5)

o=5*np.ones((2,3))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Output an array by repeating a smaller array of 2 dimensions, 10 times

A

o = np.tile(a, 10)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Output a 5-by-5 array of random integers between 0 (inclusive) and 10 (exclusive)

A

o=np.random.randint(0,10,(5,5))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Output a 3-by-3 array of random numbers following normal distribution

A
o = np.random.normal(size = (3,3))
#normal takes (mu,sigma,size)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Given 2 numpy arrays as matrices, output the result of multiplying the 2 matrices

A

o = a@b
also possible:
o = np.matmul(a, b)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Create a 1D array of numbers from 0 to 9

A

a=np.arange(10)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Create a 3×3 numpy array of all True’s

A

a=np.full((3,3), True, dtype=bool)

np.ones((3,3), dtype=bool)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Extract all odd numbers from arr

A

arr[arr % 2 == 1]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Replace all odd numbers in arr with -1

A

arr[arr % 2 == 1] = -1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Replace all odd numbers in arr with -1 without changing arr

A

out = np.where(arr % 2 == 1, -1, arr)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Convert a 1D array to a 2D array with 2 rows

A
arr.reshape(2, -1)  
# Setting to -1 automatically decides the number of rows or cols !!!!!!
17
Q

Stack arrays a and b vertically

A

np. concatenate([a, b], axis=0)
np. vstack([a, b])
np. r_[a,b]

18
Q

Stack arrays a and b horizontally

A

np. concatenate([a, b], axis=1)
np. hstack([a, b])
np. c_[a,b]

19
Q

a = np.array([1,2,3])
Desired output:
array([1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])

A

np.r_[np.repeat(a,3),np.tile(a,3)]

20
Q

Get the common items between a and b

A

np.intersect1d(a,b)

21
Q

From array a remove all items present in array b

A

np.setdiff1d(a,b)

22
Q

Get the positions where elements of a and b match

A

np.where(a == b)

a[a==b]

23
Q

Convert the function maxx that works on two scalars, to work on two arrays.

A
func=np.vectorize(scalar_func, otypes=[float])
func(a,b)
24
Q

Swap columns 1 and 2 in the array arr.

A

arr[:, [1,0,2]]

25
Q

Swap rows 1 and 2 in the array arr:

A

arr[[1,0,2], :]

26
Q

Reverse the rows of a 2D array arr

A

arr[::-1]

27
Q

Reverse the columns of a 2D array arr.

A

arr[:, ::-1]

28
Q

Create a 2D array of shape 5x3 to contain random decimal numbers between 5 and 10.

A
# Solution Method 1:
rand_arr = np.random.randint(low=5, high=10, size=(5,3)) + np.random.random((5,3))
# Solution Method 2:
rand_arr = np.random.uniform(5,10, size=(5,3))
29
Q

Print or show only 3 decimal places of the numpy array rand_arr

A

np.set_printoptions(precision=3)

30
Q

Pretty print rand_arr by suppressing the scientific notation (like 1e10)

A

np.set_printoptions(suppress=True, precision=6) # precision is optional

31
Q

Limit the number of items printed in python numpy array a to a maximum of 6 elements, show all

A

np. set_printoptions(threshold=6)

np. set_printoptions(threshold=np.nan)

32
Q

Find the mean, median, standard deviation of iris’s sepallength

A

np. mean(sepallength)
np. median(sepallength)
np. std(sepallength)

33
Q

Flatten array a into one D

A

a.ravel(), np.ravel(a)

34
Q

Compute the maximum for each row in the given array.

A

np.amax(a, axis=1)

35
Q

Compute the min-by-max for each row for given 2d numpy array.

A

np.apply_along_axis(lambda x: np.min(x)/np.max(x), arr=a, axis=1)