Zhiguang Huo (Caleb)
Monday Nov 7th, 2022
https://numpy.org/install/
n = 1000000
my_arr = np.arange(n)
my_list = list(range(n))
## run %timeit in jupyter notebook
%timeit my_arr2 = my_arr * 2
%timeit my_lis2 = [i * 2 for i in my_list]
ndarray: N-dimensional array object
1d array
## array([1.1, 2.2, 3.3, 4.4])
## array([[1, 2, 3, 4],
## [5, 6, 7, 8]])
## array([[1, 2, 3, 4],
## [5, 6, 7, 8]])
## (2, 4)
## 8
## dtype('int64')
ndarray for vectorized calculation.
vectorized (element-wise) calculation with a scalar
## array([11., 22., 33., 44.])
## array([[ 2, 4, 6, 8],
## [10, 12, 14, 16]])
## array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
## array([[0., 0., 0.],
## [0., 0., 0.]])
## array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
## array([[1., 1., 1.],
## [1., 1., 1.]])
## array([5, 5, 5, 5, 5, 5, 5, 5, 5, 5])
## array([[5, 5, 5],
## [5, 5, 5]])
## array([[0, 0, 0, 0],
## [0, 0, 0, 0]])
## array([[1, 1, 1, 1],
## [1, 1, 1, 1]])
## array([[5, 5, 5, 5],
## [5, 5, 5, 5]])
## array([[1., 0., 0.],
## [0., 1., 0.],
## [0., 0., 1.]])
## array([[1., 0., 0.],
## [0., 1., 0.],
## [0., 0., 1.]])
## array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
## array([3, 4, 5, 6, 7, 8, 9])
## array([3, 5, 7, 9])
## array([10, 9, 8, 7, 6, 5, 4])
arr1 = np.array([1,2,3], dtype=np.float64)
arr2 = np.array([1,2,3], dtype=np.int32)
arr3 = np.array([1.1,2.3,3], dtype=np.string_)
## array([1, 2, 3], dtype=int32)
## array([1.1, 2.3, 3. ])
## array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32)
## array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
## array([False, True, True, True, True, True, True, True, True,
## True])
## array([[1., 2., 3.],
## [4., 5., 6.]])
## array([[ 1., 4., 9.],
## [16., 25., 36.]])
## array([[0., 0., 0.],
## [0., 0., 0.]])
## array([[1. , 0.5 , 0.33333333],
## [0.25 , 0.2 , 0.16666667]])
## array([[1. , 1.41421356, 1.73205081],
## [2. , 2.23606798, 2.44948974]])
## array([[ 0., 2., 11.],
## [ 3., 12., 2.]])
## array([[False, False, True],
## [False, True, False]])
## array([[0, 1, 2, 3],
## [4, 5, 6, 7]])
## array([[0, 1, 2, 3],
## [4, 5, 6, 7]])
## array([[0, 1],
## [2, 3],
## [4, 5],
## [6, 7]])
## array([[0, 1, 2, 3],
## [4, 5, 6, 7]])
## array([[0, 1],
## [2, 3],
## [4, 5],
## [6, 7]])
## array([[0, 1, 2, 3],
## [4, 5, 6, 7]])
## Generator(PCG64)
## 0.43849621374551717
## <class 'float'>
## array([4, 3, 5])
## <class 'numpy.ndarray'>
## array([[0.77395605, 0.43887844, 0.85859792],
## [0.69736803, 0.09417735, 0.97562235],
## [0.7611397 , 0.78606431, 0.12811363]])
## array([[0.77395605, 0.43887844, 0.85859792],
## [0.69736803, 0.09417735, 0.97562235],
## [0.7611397 , 0.78606431, 0.12811363]])
## array([5, 2, 3, 2, 1])
## array([5, 1, 4, 2, 3])
## array([[1, 2],
## [3, 4]])
## array([0, 1, 2, 3, 4, 5])
## array([3, 2, 5, 4, 1, 0])
## array([0, 1, 2, 3, 4, 5])
## array([3, 2, 5, 4, 1, 0])
rng = np.random.default_rng(seed=42)
arr1 = np.arange(12).reshape(4,3)
rng.permutation(arr1) ## default axis=0
## array([[ 9, 10, 11],
## [ 6, 7, 8],
## [ 3, 4, 5],
## [ 0, 1, 2]])
## array([[ 2, 0, 1],
## [ 5, 3, 4],
## [ 8, 6, 7],
## [11, 9, 10]])
rng = np.random.default_rng(seed=5442)
arr1 = np.arange(12).reshape(4,3)
rng.shuffle(arr1) ## default axis=0
arr1
## array([[ 3, 4, 5],
## [ 0, 1, 2],
## [ 6, 7, 8],
## [ 9, 10, 11]])
## array([[ 0, 2, 1],
## [ 3, 5, 4],
## [ 6, 8, 7],
## [ 9, 11, 10]])
rng = np.random.default_rng(seed=42)
arr1 = np.arange(12).reshape(4,3); arr1
# rng.permuted(arr1)
# rng.permuted(arr1, axis=0)
# rng.permuted(arr1, axis=1)
## array([[ 0, 1, 2],
## [ 3, 4, 5],
## [ 6, 7, 8],
## [ 9, 10, 11]])
## array([ 1.0743261 , -0.67111716, 1.84744453, 0.17629145, -1.9324841 ,
## -0.45425202, 0.3724716 , -0.6123794 , -0.09135481, 0.41666957])
## array([[ 2.11050344, -0.12118146, 0.20957036],
## [ 0.70193464, 0.47244406, 0.85054383]])
## array([3.79329168, 3.92381599, 2.31392611, 4.10317495])
## array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
## 4
## array([4, 5, 6])
## array([ 0, 1, 2, 3, -1, -1, -1, 7, 8, 9])
## array([-1, -1, -1])
## array([ 0, 1, 2, 3, -1, 99, -1, 7, 8, 9])
## array([ 0, 1, 2, 3, 88, 88, 88, 7, 8, 9])
## array([4, 5, 6])
## array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
## array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
## array([[1, 2, 3],
## [4, 5, 6],
## [7, 8, 9]])
## array([4, 5, 6])
## 6
## 6
## array([[1, 2, 3],
## [4, 5, 6]])
## array([[2, 3],
## [5, 6]])
## array([5, 6])
## array([[1],
## [4],
## [7]])
## array([[9, 8, 7],
## [6, 5, 4],
## [3, 2, 1]])
## array([[1, 0, 0],
## [4, 0, 0],
## [7, 8, 9]])
nane_list = ["Bob", "Joe", "Will", "Bob", "Will", "James"]
names = np.array(nane_list)
data = rng.standard_normal(size=(6,3))
names
## array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'James'], dtype='<U5')
## array([[ 0.85563448, 0.95392466, 0.80504012],
## [ 0.27501004, -0.51532714, -1.25738591],
## [-1.80312924, -0.80664554, -0.78326034],
## [-0.50928028, 0.10581694, -0.98269014],
## [-1.27201944, 0.90385529, 0.95669103],
## [-0.3984569 , -1.60674072, 1.62947366]])
## array([ True, False, False, True, False, False])
## array([[ 0.85563448, 0.95392466, 0.80504012],
## [-0.50928028, 0.10581694, -0.98269014]])
## array([[ 0.95392466, 0.80504012],
## [ 0.10581694, -0.98269014]])
## array([False, True, True, False, True, True])
## array([False, True, True, False, True, True])
## array([[ 0.27501004, -0.51532714, -1.25738591],
## [-1.80312924, -0.80664554, -0.78326034],
## [-1.27201944, 0.90385529, 0.95669103],
## [-0.3984569 , -1.60674072, 1.62947366]])
## array([ True, False, True, True, True, False])
## array([[ 0.85563448, 0.95392466, 0.80504012],
## [-1.80312924, -0.80664554, -0.78326034],
## [-0.50928028, 0.10581694, -0.98269014],
## [-1.27201944, 0.90385529, 0.95669103]])
## array([False, False, False, False, False, False])
## array([], shape=(0, 3), dtype=float64)
## array([[0.85563448, 0.95392466, 0.80504012],
## [0.27501004, 0. , 0. ],
## [0. , 0. , 0. ],
## [0. , 0.10581694, 0. ],
## [0. , 0.90385529, 0.95669103],
## [0. , 0. , 1.62947366]])
## array([[99. , 99. , 99. ],
## [99. , 99. , 99. ],
## [ 0. , 0. , 0. ],
## [99. , 99. , 99. ],
## [ 0. , 0.90385529, 0.95669103],
## [99. , 99. , 99. ]])
## array([[10, 11, 12, 13, 14],
## [ 0, 1, 2, 3, 4]])
## array([[10, 11, 12, 13, 14],
## [ 0, 1, 2, 3, 4]])
## array([13, 1])
## array([[13, 11],
## [ 3, 1]])
## array([[0, 1],
## [2, 3],
## [4, 5],
## [6, 7]])
## array([[0, 2, 4, 6],
## [1, 3, 5, 7]])
## array([[56, 68],
## [68, 84]])
## array([[56, 68],
## [68, 84]])
## array([[56, 68],
## [68, 84]])
## array([[[ 0, 1, 2, 3],
## [ 4, 5, 6, 7]],
##
## [[ 8, 9, 10, 11],
## [12, 13, 14, 15]]])
## array([[[ 0, 4],
## [ 1, 5],
## [ 2, 6],
## [ 3, 7]],
##
## [[ 8, 12],
## [ 9, 13],
## [10, 14],
## [11, 15]]])
#arr = rng.standard_normal(6) ## try this one later
arr = np.arange(6)
arr
np.sqrt(arr)
np.exp(arr)
np.floor(arr); np.ceil(arr);
np.log(arr), np.log10(arr); np.log2(arr); np.log1p(arr)
np.isnan(np.sqrt(arr))
np.sin(arr); np.cos(arr); np.tan(arr)
arr2 = np.array([np.nan, np.NaN, np.inf, np.Inf, 1.0])
arr2
np.isnan(arr2); np.isinf(arr2); np.isfinite(arr2)
x = rng.standard_normal(4)
y = rng.standard_normal(4)
np.maximum(x,y); np.fmax(x,y) ## fmax ignores NaN
np.minimum(x,y); np.fmin(x,y) ## fmax ignores NaN
np.add(x,y); x + y
np.subtract(x,y); x - y
np.multiply(x,y); x * y
np.divide(x,y); x / y
np.power(x,2)
arr_power = np.arange(len(x))
np.power(x,arr_power)
np.greater(x,y); x > y
np.less(x,y); x < y
np.greater_equal(x,y); x >= y
np.less_equal(x,y); x <= y
np.equal(x,y); x == y
np.not_equal(x,y); x != y
x = np.array([True, True, False, False], dtype=np.bool_)
y = np.array([True, False, True, False], dtype=np.bool_)
np.logical_and(x,y); x & y
np.logical_or(x,y); x | y
np.logical_xor(x,y); x ^ y
xarr = np.array([1.1,1.2,1.4,1.5])
yarr = np.array([2.1,2.2,2.4,2.5])
condition = np.array([True, False, True, False])
res = [(x if c else y) for x, y, c in zip(xarr, yarr, condition)]
res
## [1.1, 2.2, 1.4, 2.5]
## array([1.1, 2.2, 1.4, 2.5])
## array([1.1, 2.2, 1.4, 2.5])
## array([[ 0.21984492, -1.28342596, 1.31520866],
## [ 0.85478637, -1.12501008, -2.36333542],
## [ 1.72194583, -1.29984418, 0.82499496]])
## array([[ True, False, True],
## [ True, False, False],
## [ True, False, True]])
## array([[ 1, -1, 1],
## [ 1, -1, -1],
## [ 1, -1, 1]])
## array([[ 0. , -1.28342596, 0. ],
## [ 0. , -1.12501008, -2.36333542],
## [ 0. , -1.29984418, 0. ]])
rng = np.random.default_rng(32608)
arr = rng.standard_normal(size = 6)
arr
# arr.mean(); np.mean(arr)
# arr.sum(); np.sum(arr)
# arr.std(); np.std(arr)
# arr.var(); np.var(arr)
# arr.min(); np.min(arr)
# arr.max(); np.max(arr)
# arr.argmin(); np.argmin(arr)
# arr.argmax(); np.argmax(arr)
# arr.cumsum(); np.cumsum(arr)
# arr.cumprod(); np.cumprod(arr)
## array([ 0.21984492, -1.28342596, 1.31520866, 0.85478637, -1.12501008,
## -2.36333542])
## array([[ 0.21984492, -1.28342596, 1.31520866],
## [ 0.85478637, -1.12501008, -2.36333542],
## [ 1.72194583, -1.29984418, 0.82499496],
## [ 0.55837717, -0.19774024, -1.2478677 ]])
## -2.022065687187067
## array([ 3.35495429, -3.90602046, -1.47099951])
## array([ 0.25162762, -2.63355913, 1.2470966 , -0.88723077])
## 44
## array([False, False, True, False])
## True
## False
## array([ 0.21984492, -1.28342596, 1.31520866, 0.85478637, -1.12501008])
## array([1, 4, 0, 3, 2])
## array([-1.28342596, -1.12501008, 0.21984492, 0.85478637, 1.31520866])
## array([-1.28342596, -1.12501008, 0.21984492, 0.85478637, 1.31520866])
## array([[ 0.21984492, -1.28342596, 1.31520866],
## [ 0.85478637, -1.12501008, -2.36333542],
## [ 1.72194583, -1.29984418, 0.82499496],
## [ 0.55837717, -0.19774024, -1.2478677 ]])
## array([[-1.28342596, 0.21984492, 1.31520866],
## [-2.36333542, -1.12501008, 0.85478637],
## [-1.29984418, 0.82499496, 1.72194583],
## [-1.2478677 , -0.19774024, 0.55837717]])
## array([[ 0.21984492, -1.29984418, -2.36333542],
## [ 0.55837717, -1.28342596, -1.2478677 ],
## [ 0.85478637, -1.12501008, 0.82499496],
## [ 1.72194583, -0.19774024, 1.31520866]])
## array([0, 1, 2, 5, 6])
## array([0, 1, 2])
## array([0, 1, 2, 3, 5, 6])
## array([ True, True, True, True, False, False, False, True])
## array([5, 6])
## array([[10, 13],
## [28, 40]])
## 55
## 53.99999999999996
## 54.00925846556311
## array([[ 0.92592593, -0.25925926],
## [-0.25925926, 0.09259259]])
## array([[ 1.00000000e+00, -1.94289029e-16],
## [-7.77156117e-16, 1.00000000e+00]])
eigen_value, eigen_vector = np.linalg.eigh(z)
eigen_vector.dot(np.diag(eigen_value)).dot(eigen_vector.T)
## array([[ 5., 14.],
## [14., 50.]])