Numpy Flashcards
IMPORTING
What is the statement needed to import the numpy module.
import numpy as np
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[ ].
array1 = np.array(a) array2 = np.array([a,b]) array2 = np.array((a,b))
DIMENSIONS
a = np.array([ [1,2,3], [4,5,6] ])
How can you check the dimensións and size of the array?
a = np.array([ [1,2,3], [4,5,6] ])
print(a.ndim)
print(a.shape)
print(a.size)
print(a.itemsize)
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 = np.array([ [1,2,3], [4,5,6] ])
print(a.dtype)
a = a.astype(‘float32’)
print(a.dtype)
ARRAY DEFINITION
Create a 10x10 array with numbers from 1 to 100.
import numpy as np
b = np.arranage(1,101)
b.reshape(10,10)
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
import numpy as np
z = np.zeros((5,3))
print(z)
z = z.reshape((3,5))
print(z)
z = z.astype(‘int16’)
print(z)
ARRAY DEFINITION
(a) Create a 5x3 array of ones (dtype -> int).
(b) Reshape the new array to be 3x5.
import numpy as np
z = np.ones((5,3), dtype = np.int32)
print(z)
z = z.reshape((3,5))
print(z)
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[1]
(b) a[ [1, 3] ]
(c) a[ np.any( a % 3 == 0, axis = 1) ]
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 = 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)
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?
array-wise
a. max()
a. min()
a. mean()
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.
import numpy as np
a = np.arange(1,101).rashape(10,10)
z = np.zeros_like(a) f = np.full_like(a, 5)
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
taxes = prices * 1.16 total_1 = prices.sum()
taxes_inc = prices + taxes total_2 = taxes_inc.sum()
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
r1 = np.ceil(prices) r2 = np.floor(prices) r3 = np.rint(prices)
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]) — )
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) .
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.flatten()