Numpy Flashcards

1
Q

IMPORTING

What is the statement needed to import the numpy module.

A

import numpy as np

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

ARRAY CREATION

a = [1, 2, 3, 4]
b = [5, 6, 7, 8]

(a) Make a 1d array from list a[ ].
(b) Make a 2d array from lists a[ ] and b[ ].

A
array1 = np.array(a)
array2 = np.array([a,b])  
array2 = np.array((a,b))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

DIMENSIONS

a = np.array([ [1,2,3], [4,5,6] ])

How can you check the dimensións and size of the array?

A

a = np.array([ [1,2,3], [4,5,6] ])

print(a.ndim)
print(a.shape)
print(a.size)
print(a.itemsize)

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

DATA TYPE

a = np.array([ [1,2,3], [4,5,6] ])

(a) How can you check the data type of the new array?
(b) Can you change the type from int to float?

A

a = np.array([ [1,2,3], [4,5,6] ])
print(a.dtype)

a = a.astype(‘float32’)
print(a.dtype)

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

ARRAY DEFINITION

Create a 10x10 array with numbers from 1 to 100.

A

import numpy as np

b = np.arranage(1,101)
b.reshape(10,10)

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

ARRAY DEFINITION

(a) Create a 5x3 array of zeros.
(b) Reshape the new array to be 3x5.
(c) Change the type of the array from float to int

A

import numpy as np

z = np.zeros((5,3))
print(z)

z = z.reshape((3,5))
print(z)

z = z.astype(‘int16’)
print(z)

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

ARRAY DEFINITION

(a) Create a 5x3 array of ones (dtype -> int).
(b) Reshape the new array to be 3x5.

A

import numpy as np

z = np.ones((5,3), dtype = np.int32)
print(z)

z = z.reshape((3,5))
print(z)

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

ARRAY INDEXING

a = np.array( [ [5,7,8,6], [5,9,7,8], [5,4,9,2], [5,3,9,6] ] )

(a) Select the second row (index = 1).
(b) Select the second and fourth colums.
(c) Get the rows where there is at least one mulltiple of three.

A

(a) a[1]
(b) a[ [1, 3] ]
(c) a[ np.any( a % 3 == 0, axis = 1) ]

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

SUM

a = np.array( [ [5,7,8], [5,9,7], [5,4,9], [6,3,9] ] )

(a) Compute the sum of all the elements in the array.
(b) Compute the sum of the elements in each row.
(c) Compute the sum of the elements in each column.

A

a = np.array( [ [5,7,8], [5,9,7], [5,4,9], [6,3,9] ] )

# (a) ---------------
a.sum( )
# (b) ---------------
a.sum(axis = 1)
# (c) ---------------
a.sum(axis = 0)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

MAX, MIN, MEAN

a = np.array( [ [5,7,8], [5,9,7], [5,4,9], [6,3,9] ] )

How can you compute the min, max and mean of the elements in the array?

A

array-wise

a. max()
a. min()
a. mean()

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

COPYNG SHAPE AN DIMS

import numpy as np
a = np.arange(1,101).rashape(10,10)

(a) Create a zeros array with the same shape and dtype of a.
(b) Create a full array with the same shape and dtype of a.

A

import numpy as np
a = np.arange(1,101).rashape(10,10)

z = np.zeros_like(a)
f = np.full_like(a, 5)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

ARITHMETIC

prices = np.array([100, 25, 40, 33, 60])

(a) Compute the taxes corresponding to every price, consider a 16% tax.
(b) Compute the total with and without taxes

A
taxes = prices * 1.16
total_1 = prices.sum() 
taxes_inc = prices + taxes
total_2 = taxes_inc.sum()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

ROUNDING

prices = np.array([50, 53.5, 58.9, 62.57])

(a) Round prices to the next integer
(b) Round prices to the previous integer
(c) Round prices to the nearest intger

A
r1 = np.ceil(prices)
r2 = np.floor(prices)
r3 = np.rint(prices)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

COMPARING ARRAYS

prices_1 = np.array([50, 53.5, 58.9, 62.57])
prices_2 = np.array([48, 55.5, 57, 63])

prices_1 and prices_2 correspond to prices of the same products (product 1:4) in two different places. Find the cheapest and most expensive price for every product.

(Bonus: How would you solve the same problem if there were three or more lists of prices? — prices_3 = np.array([47, 60.5, 56, 67]) — )

A
cheapest = np,minimum(prices_1, prices_2)
most_expensive = np.maximum(prices_1, prices2)

Bonus: use vstack() to create a prices array with one row per list, then use prices.min(axis = 0) / prices.max(axis = 0) .

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

ARRAY RESHAPING

# Consider the 2x2x3 array
a = np.array([[[1,2,3],[4,5,6]],[[8,9,10],[12,13,14]]])

How can you get a 1xn array from a?

A

a = a.flatten()

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

ARRAY SORTING

arr = np.random.randn(3, 5)

How can you reorder the array by its first row?

A

arr = np.random.randn(3, 5)

result = array[ : , arr[0].argsort() ]

17
Q

ARRAY CREATION

(a) Create a 3x5 array with random numbers
(b) Create a 3x5 array with random ints between 0 and 100

A
arr_1 = np.random.randn(3,5)
arr_2 = np.random.randint(100, size = (3,5))
18
Q

BOOLEAN MASKING

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

How can you get all the entries divisible by 3?

A

arr[arr % 3 ==0]

19
Q

SLICING

arr = np.random.randn(10,10)

Create an arr2 with all the elements in the midle of arr.

(“in the middle” means any element which is not in the edges of the array)

A

arr2 = arr[1:-1,1:-1]

20
Q

BOOLEAN SLICING

arr = np.random.randint(10, size = (10,10))

Get all the rows in which at least 2 entries are equal to 1.

A

arr = np.random.randint(10, size = (10,10))

one_counter = (arr == 1).sum(axis = 1)
indexer = one_counter >= 2

arr[indexer]

21
Q

RESHAPING

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

reshape array a to be a -row vector-, -column vector-, 2 x k matrix.

A
b = a.reshape(-1,1)
c = a.reshaper(1,-1)
d = a.reshape(2,-1)