CheatSheet Python 7 - NumPy 1
CheatSheet Python 7 - NumPy 1
a.shape The shape attribute of NumPy array a keeps a tuple of a = np.array([[1,2],[1,1],[0,0]])
integers. Each integer describes the number of elements of print(np.shape(a)) # (3, 2)
the axis.
a.ndim The ndim attribute is equal to the length of the shape tuple. print(np.ndim(a)) # 2
np.matmul(a,b), a@b The standard matrix multiplication operator. Equivalent to the print(np.matmul(a,b))
@ operator. # [[2 2] [2 2]]
np.arange([start, ]stop, Creates a new 1D numpy array with evenly spaced values print(np.arange(0,10,2))
[step, ]) # [0 2 4 6 8]
np.linspace(start, stop, Creates a new 1D numpy array with evenly spread elements print(np.linspace(0,10,3))
num=50) within the given interval # [ 0. 5. 10.]
np.average(a) Averages over all the values in the numpy array a = np.array([[2, 0], [0, 2]])
print(np.average(a)) # 1.0
<slice> = <val> Replace the <slice> as selected by the slicing operator with a = np.array([0, 1, 0, 0
, 0])
the value <val>. a[::2] = 2
print(a) [2 1 2 0 2]
#
np.diff(a) Calculates the difference between subsequent values in fibs = np.array([0, 1
, 1, 2, 3, 5])
NumPy array a print(np.diff(fibs, n=1))
# [1 0 1 1 2]
np.sort(a) Creates a new NumPy array with the values from a a = np.array([10,3,7,1,0])
(ascending). print(np.sort(a))
# [ 0 1 3 7 10]
np.argsort(a) Returns the indices of a NumPy array so that the indexed a = np.array([10,3,7,1,0])
values would be sorted. print(np.argsort(a))
# [4 3 1 2 0]
np.argmax(a) Returns the index of the element with maximal value in the a = np.array([10,3,7,1,0])
NumPy array a. print(np.argmax(a)) # 0
np.nonzero(a) Returns the indices of the nonzero elements in NumPy array a = np.array([10,3,7,1,0])
a. print(np.nonzero(a)) # [0 1 2 3]