Data book - Numpy Flashcards
How do you make a 2x3x2 empty np array?
np.empty((2,3,2))
What is np’s equivalent to the range function?
Np.arange(n)
Out: array([0, 1, 2, …, n])
How do you take an existing array a and make a new array with the same shape but all 1’s?
Np.ones_like(a)
How do you make a square n x n matrix?
Np.eye(n)
How do you change an np dtype from one type to another?
array_to_change.astype(dtype)
E.g
a = np.array([‘1’]) a = a.astype(np.float64)
Does slicing an ndarray copy that section and return the copy, or zoom into that section?
Zoom in
How do you make a copy of an ndarray slice?
arr = np.array([…])
barr = arr[5:8].copy()
How do you select the top-left most element of the 2d array arr2d?
arr2d[0,0]
How do you generate randomly distributed data 7 rows by 4 columns?
Np.random.randn(7, 4)
How do you do both a boolean and an array slice in one line?
data[names==‘Bob’, 2:]
How do you select everything except ‘Bob’ in an ndarray?
names != ‘Bob’
Or
data[-(names == ‘Bob’)]
How do you use and/or logic in ndarray slicing?
Mask = (names == ‘Bob’) |/& (names == ‘Will’)
What is fancy indexing?
Indexing with integer arrays
E.g arr[4, 3, 0, 6] pops out those rows in that order
Say you call arr[[1, 5, 7, 2], [0, 3, 1, 2]] on a big 2D ndarray. What happens?
Outputs a 1D array with elements selected by [[rows], [columns]] in the input.
How do you convert two 1D ndarrays to an indexer that selects a square region?
arr[np.ix_([], [])]
How do you transpose?
arr.T
How to compute the dot product of two arrays?
np.dot(a, b)
How do you swap axis in general(e.g inn dimensions)?
arr.swapaxes(n1, n2, …, n)
What is a universal function in numpy?
Funct that performs elementwise operations on data in ndarrays
What are examples of ufuncs?
np. sqrt(arr)
np. exp(arr)
(Unary funcs - take one array)
x = np.array([1, 3, 3,])
y = np.array([3, 1, 1])
What does np.maximum(x, y) output?
array([3, 3, 3)]
Also its a binary ufunc
What does np.modf(arr) do?
Returns fractional and integral parts of an array
What ufunc gives booleans corresponding to whether an element is NaN?
Np.isnan
How do you make a 3 by 5 array filled with the integers 3.14?
np.full((3, 5), 3.14, dtype=float)
How do you make a 3 by 3 array filled with random integers between 0 and 1?
np.random.random((3, 3))
How do you make a 3 by 3 array with random values from a normal distribution with mean 0 and SD 1?
np.random.normal(0, 1, (3, 3))
How do you make a 3 by 3 array with random values between 0 and 10?
np.random.randint(0, 10, (3, 3))
How do you make a 3x3 identity matrix?
np.eye(3)
What 3 methods are generally used for array concatenation?
np. concatenate
np. vstack
np. hstack
How do you stack elements on the 3rd axis?
np.dstack
What 3 methods are used for splitting?
np. split
np. hsplit
np. vsplit
How do you apply a ufunc multiple times on an array x to remove a dimension?
np.ufunc.reduce(x)
How do you apply a ufunc multiple times to an array x cumulatively, and save the result to an array?
np.ufunc.accumulate(x)
What numpy function could you use to make an easy multiplication table in one line from an array x?
np.multiply.outer(x)