numpy-basics-checkpoint
NumPy Basics¶
In [1]:
import numpy as np
Creating arrays¶
In [ ]:
a = np.array([0, 1, 2, 3, 4])
b = np.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]], dtype = float)
c = np.array([[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]], dtype = int)
d = np.array([5, 6, 7, 8, 9])
In [ ]:
np.arange(0, 10) # array of evenly spaced values
In [ ]:
np.zeros((2, 3)) # array of zeros with the given shape
In [ ]:
np.ones(2) # array of ones with the given shape
In [ ]:
np.empty((2, 3)) # empty array (arbitrary values)
In [ ]:
np.full((2, 3), 3) # fill new array with given shape
Inspecting an array¶
In [ ]:
b.size # number of elements in the array
In [ ]:
b.ndim # number of dimensions
In [ ]:
b.shape # lengths of each dimension
In [ ]:
b.dtype # data type of array elements
Basic operations¶
Element-wise arithmetic¶
In [ ]:
c
In [ ]:
a + d # element-wise addition (or np.add)
In [ ]:
a – d # element-wise subtraction (or np.subtract)
In [ ]:
a * d # element-wise multiplication (or np.multiply)
In [ ]:
a / d # element-wise division (or np.divide)
Aggregation¶
In [ ]:
b.sum() # sum elements
In [ ]:
b.min() # minimum element
In [ ]:
b.max() # maximum element
In [ ]:
b.mean() # mean of elements
Element-wise comparison¶
In [ ]:
a == a # element-wise comparison
In [ ]:
b < 2 # element-wise comparison Array-level equality¶ In [ ]: np.array_equal(a, b) # check whether arrays have the same shape and elements Linear algebra¶ In [ ]: b.T # reverse the array dimensions (or np.transpose) In [ ]: a.dot(a) # dot product (or np.dot) In [ ]: b @ c # matrix multiplication (or np.matmul) In [ ]: x = c.T @ c np.linalg.inv(x) # matrix inverse In [ ]: b.trace() # trace of a matrix In [ ]: np.eye(3) # identity matrix Universal functions (ufuncs)¶ In [ ]: np.exp(a) # exponential function In [ ]: np.sin(a) # sine function In [ ]: np.log(c) # natural logarithm In [ ]: np.abs(-b) # absolute value In [ ]: np.power(b, 3) # raise to a power Copying arrays¶ In [ ]: aa = a.copy() # make a deep copy of the array aa.fill(0) print(a) print(aa) In [ ]: aaa = aa.view() # create a view of the array (points to the same data in memory) aaa.fill(1) print(aa) print(aaa) Array manipulation¶ In [ ]: b.ravel() # flatten the array (or np.ravel) In [ ]: b.reshape(5,2) # change shape, but don't change values In [ ]: np.resize(b, (6,2)) # create a new array with a different shape In [ ]: np.append(a, [1, 1]) # append values to the end of an array In [ ]: np.insert(a, 2, [1, 1]) # insert values at a given location In [ ]: np.delete(a, [0,2]) # delete elements at given locations In [ ]: np.concatenate((b, b+10), axis = 0) # concatenate arrays In [ ]: np.r_[b, b + 10] # shorthand: concatenate arrays along their first axis (row-wise) In [ ]: np.c_[b, b + 10] # shorthand: concatenate arrays along their second axis (column-wise) In [ ]: np.split(b, 2, axis = 0) # split an array into sub-arrays Indexing/slicing¶ In [ ]: b[0,1] # select a single element (at row 0, column 1) In [ ]: a[0:4] # select a slice In [ ]: a[0:4:2] # select a slice with a stride In [ ]: a[::-1] # select all elements in reverse In [ ]: b[:,2] # select all elements along the first axis (all rows) and only the elements with index 2 along the second axis In [ ]: a[:, np.newaxis] # insert a new axis (here: convert a 1D array to a column vector) In [ ]: a[a>2] # boolean indexing