You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
NumPy is short for Numerical Python, using to deal with ndarray data structure (array or vector/matrix-based computations)
Most computational packages providing scientific functionality use NumPy
Import convention: import numpy as np
1.1 Motivation for Numpy
Add 2 Lists: The loop structure provides a good mechanism to finish a piece of repetitive work with a reasonable amount of code. However, when number of iterations is large, a loop could take a very long time.
size=10000000# Add 2 lists using For Loopdefadd_list(size_of_list):
l1=list(range(size_of_list))
l2=list(range(size_of_list))
l= [l1[i] +l2[i] foriinrange(size_of_list)]
returnl# Add 2 list using Numpy defadd_ndarray(size_of_array):
l1=np.arange(size_of_array)
l2=np.arange(size_of_array)
returnl1+l2%%timeadd_list(size) #time: 2.44 s%%timeadd_ndarray(size) #time: 50.9 ms much Numpy faster than using For Loop to add 2 lists
1.2. ndarray
NumPy's ndarray gives a tool to represent vectors and matrices, represents an N-dimensional data.
ndarray takes a block of memory space. NumPy functionalities can operate this entire memory block without a loop structure, making the computation very fast and efficient.
1.2.1. Creating ndarray From List
1.2.1.1. Creating ndarray From List using np.array()
np.array() method can create an array or multi-dimensional array from a list or nested list.
np.zeros(shape) and np.ones(shape): to create all-zero vectors/matrices
np.arange(end): equivalence of range() function
np.random.randn(d1, d2, d3, ...): return a sample (or samples) from the “standard normal” distribution.
d1, d2, d3, ... are sizes of each dimension
np.random.rand(d1, d2, d3, ...): return random samples from a uniform distribution over [0, 1).
np.eye(N): NxN identity matrix
1.2.2. ndarray.ndim and ndarray.shape
ndarray.ndim: return number of dimensions
ndarray.shape: return size of each dimension. For 1D array, shape will be (row, ).
print(arr.ndim) #1print(arr.shape) #(6,0) - For 1D array, each element in the array will be treated as a row, so it will has 6 rows, 0 columnprint(arr2d.ndim) #2print(arr2d.shape) #(2,3) - For 2D array, 2 rows 3 columns
1.3. Indexing and Slicing
1-dimensional array: similar to Python list
arr1d=np.array([2, 3, 4, 5, 6, 7, 9])
print(arr1d[2]) #4print(arr1d[2:5]) #[4 5 6]print(arr1d[:-1:2]) #[2 4 6] from begin to the last one (but not include the last one)
Higher-dimensional array: use indexing and slicing for each dimension – separate each dimension with comma (,)
Use & for “and”, | for “or” and ~ for “not” to apply multiple conditions.
# random data for 4 users in 4x2 matrixarr_dat=np.random.rand(4, 2)
[[0.451413910.19962473]
[0.513392080.35115272]
[0.433087320.32066871]
[0.820722260.45147328]]
# user id from 0 to 3arr_id=np.arange(4)
# pick data for user id 2print(arr_dat[arr_id==2]) #[[0.43308732 0.32066871]]# pick data for users id 0 and 3print(arr_dat[(arr_id==0)|(arr_id==3)])
[[0.451413910.19962473]
[0.820722260.45147328]]
Arithmetic operations with scalars propagate the scalar argument to each element.
Comparisons between arrays of the same size yield Boolean arrays.
arr1=np.arange(5)
arr2=np.array(range(5, 0, -1))
print(arr1+arr2)
print(arr1-arr2)
print(arr1*arr2)
print(arr1/arr2)
#arithmetic operations with scalars propagate the scalar argument to each elementprint(arr1+1)
print(arr1*2)
print(1/arr2)
#comparisons between arrays of the same sizeprint(arr1>arr2) #[False False False True True]
2.2. Matrix Shape Manipulation
reshape() method can be used to change the shape of the matrix.
Use -1 to imply the size of the other dimension
arr1=np.arange(9)
print(arr1, arr1.shape) #[0 1 2 3 4 5 6 7 8] (9,)mat1=arr1.reshape(3, 3)
[[012]
[345]
[678]] (3, 3)
mat2=arr1.reshape(3, -1) #use -1 to imply the size of the other dimension.
[[012]
[345]
[678]] (3, 3)