SML - Week 1. # [[0. 0. 0.
]
1. open Jupyter Lab (better than Jupyter notebook): [0. 0. 0.]]
Ananconda navigator -> jupyter lab np.full((2, 3), 3)
# (2, 3) = (row, column), element = 3
2. short cut: # [[3 3 3]
Add cell above: esc + a [3 3 3]]
Add cell below: esc + b
Delete cell: esc + d + d 7. np array property
Run code: ctrl + enter (current), shift + enter (next) b = np.array([[0, 1, 2, 3], [5, 6, 7, 8]], dtype = float)
Stop kernel: shut down
b.size # rows*columns (total elements) = 8
3. numpy arange -> creates np array b.ndim # rows (dimension) = 2
import numpy as np b.shape # (row, col) = (2, 4)
x = np.arange(0.0, 2.0, 0.01) b.dtype # float64
# start=0, stop=2 (exclusive), step=0.01
# x = [0.0 0.01 0.02 … 1.99] 8. np array calculations
a = np.array([0, 1, 2, 3])
c = np.arange(0, 10) b = np.array([[0, 1, 2, 3], [5, 6, 7, 8]], dtype = float)
# default step = 1, c = [0 1 2 3 … 9] c = np.array([[1, 6], [2, 7], [3, 8], [4, 9]], dtype = int)
d = np.array([5, 6, 7, 8])
4. plot
import matplotlib.pyplot as plt 8.1 element-wise calc
plt.plot(x,y) a + d # [5 7 9 11]
plt.ylabel("y") a – d # [-5 -5 -5 -5]
plt.xlabel("x") a * d # [0 6 14 24]
plt.title("Parabola") a / d # [0 0.167 0.286 0.375]
plt.show() a == a # [True True True True]
b < 2 # [[True True False False]
5. numpy array -> creates np array [False False False False]]
a = np.array([0, 1, 2, 3]) a + 10 # [10 11 12 13]
# a = [0 1 2 3] np.exp(a) # e^element [1.0 2.718 7.389 20.085]
np.sin(a) # sin(element) [0. 0.841 0.909 0.141]
b = np.array([[0, 1, 2, 3], [5, 6, 7, 8]], dtype = float) np.log(c) # ln [[0. 1.79175947]
# b = [[0. 1. 2. 3.] [0.69314718 1.94591015]
[5. 6. 7. 8.]] [1.09861229 2.07944154]
# dtype = float, int [1.38629436 2.19722458]]
np.abs(-b) # [[0 1 2 3]
c = np.arange(0, 10) [5 6 7 8]]
# default step = 1, so c = [0 1 2 3 … 9] np.power(b, 3) # element^3 [[ 0. 1. 8. 27.]
[125. 216. 343. 512.]]
6. numpy number -> creates constant array
np.zeros((2, 3)) 8.2 aggregation (all eleemnts in np array)
# [[0. 0. 0.] b.sum() # 32.0
[0. 0. 0.]] b.min() # 0.0
# rows = 2, columns = 3, default element = 0. (float) b.max() # 8.0
b.mean() # 4.0
np.ones(2) np.array_equal(a, b) # False (shape + elements)
# [1. 1.]
# default row = 1, columns = 2 8.3 matrix calc -> new indep array
b.T # [[0. 5.] transpose
np.empty((2, 3)) [1. 6.]
[2. 7.]
[3. 8.]] # [array([[0., 1., 2., 3.]]), array([[5., 6., 7., 8.]])]
a.dot(a) # 14 dot product of 2 np arrays # split to 2 rows of np arrays (keep same brackets)
b @ c # [[ 20. 50.] matrix multiplication
[ 70. 200.]] 11. numpy array indexing
x = c.T @ c a = np.array([0, 1, 2, 3])
np.linalg.inv(x) # [[ 0.46 -0.16] inverse matrix b = np.array([[0, 1, 2, 3], [5, 6, 7, 8]], dtype = float)
[-0.16 0.06]]
b.trace() # 6.0 (sum of diagonal) b[0,1] # 1 (row = 0, col = 1)
np.eye(3) # [[1 0 0] identity matrix (3x3) a[0:4] # [0 1 2 3] row = 0, col index = 0 ~ (4-1)
[0 1 0] a[0:4:2] # [0 2] step = 2
[0 0 1]] a[::-1] # [3 2 1 0] step = -1 (count backwards)
b[:,2] # [2 7] all rows, col index = 2
9. copy numpy array a[:, np.newaxis] # [[0] add new axis
a = np.array([0, 1, 2, 3]) [1]
[2]
aa = a.copy() # independent copy [3]]
aa.fill(0) # a = [0 1 2 3], aa = [0 0 0 0] a[a>2] # [3]
aaa = aa.view() # refer to same array
aaa.fill(1) # aa = [1 1 1 1] = aaa
10. change array shape -> new indep array
a = np.array([0, 1, 2, 3])
b = np.array([[0, 1, 2, 3], [5, 6, 7, 8]], dtype = float)
b.ravel() # [0 1 2 3 5 6 7 8] 1 row
b.reshape(4,2) # [[0 1]
[2 3]
[5 6]
[7 8]]
np.resize(b, (6,2)) # [[0 1]
[2 3]
[5 6]
[7 8]
[0 1]
[2 3]]
np.append(a, [1,1]) # [0 1 2 3 1 1]
np.insert(a, 2, [1,1]) # [0 1 1 1 2 3]
# insert at index 2
np.delete(a, [0,2]) # [1 3]
# delete at index 0, index 2
np.concatenate((b, b+10), axis = 0)
alternatively, np.r_[b, b + 10]
# axis = 0 add new rows
# [[ 0. 1. 2. 3.]
[ 5. 6. 7. 8.]
[10. 11. 12. 13.]
[15. 16. 17. 18.]]
np.c_[b, b + 10]
# add new columns
# [[ 0. 1. 2. 3. 10. 11. 12. 13.]
[ 5. 6. 7. 8. 15. 16. 17. 18.]]
np.split(b, 2, axis = 0)