Zhiguang Huo (Caleb)
Wednesday Nov 9th, 2022
## array([[0., 0., 0., 0.],
## [0., 0., 0., 0.]])
## array([[0., 0.],
## [0., 0.],
## [0., 0.],
## [0., 0.]])
## array([0., 0., 0., 0., 0., 0., 0., 0.])
## array([0., 0., 0., 0., 0., 0., 0., 0.])
## array([[0., 0., 0.],
## [0., 0., 0.]])
## array([[1., 1., 1.],
## [1., 1., 1.]])
## array([[0., 0., 0.],
## [0., 0., 0.],
## [1., 1., 1.],
## [1., 1., 1.]])
## array([[0., 0., 0., 1., 1., 1.],
## [0., 0., 0., 1., 1., 1.]])
## array([[0., 0., 0.],
## [0., 0., 0.],
## [1., 1., 1.],
## [1., 1., 1.]])
## array([[0., 0., 0., 1., 1., 1.],
## [0., 0., 0., 1., 1., 1.]])
## array([[0., 0., 0., 1., 1., 1.],
## [0., 0., 0., 1., 1., 1.]])
## array([[0., 0., 0.],
## [0., 0., 0.],
## [1., 1., 1.],
## [1., 1., 1.]])
rng = np.random.default_rng(32608)
data = rng.standard_normal((5,2))
np.split(data, [2]) ## default: axis = 0
## [array([[ 0.21984492, -1.28342596],
## [ 1.31520866, 0.85478637]]), array([[-1.12501008, -2.36333542],
## [ 1.72194583, -1.29984418],
## [ 0.82499496, 0.55837717]])]
## array([[ 0.21984492, -1.28342596],
## [ 1.31520866, 0.85478637]])
## array([[-1.12501008, -2.36333542],
## [ 1.72194583, -1.29984418],
## [ 0.82499496, 0.55837717]])
## array([[ 0.21984492, -1.28342596]])
## array([[ 1.31520866, 0.85478637],
## [-1.12501008, -2.36333542]])
## array([[ 1.72194583, -1.29984418],
## [ 0.82499496, 0.55837717]])
## array([[ 0.21984492],
## [ 1.31520866],
## [-1.12501008],
## [ 1.72194583],
## [ 0.82499496]])
## array([[-1.28342596],
## [ 0.85478637],
## [-2.36333542],
## [-1.29984418],
## [ 0.55837717]])
## array([[ 0.21984492, -1.28342596]])
## array([[ 1.31520866, 0.85478637],
## [-1.12501008, -2.36333542]])
## array([[ 1.72194583, -1.29984418],
## [ 0.82499496, 0.55837717]])
## array([[ 0.21984492],
## [ 1.31520866],
## [-1.12501008],
## [ 1.72194583],
## [ 0.82499496]])
## array([[-1.28342596],
## [ 0.85478637],
## [-2.36333542],
## [-1.29984418],
## [ 0.55837717]])
## array([1, 1, 2, 2, 3, 3])
## array([1, 1, 2, 2, 2, 3, 3, 3, 3])
## array([1, 1, 2, 2, 3, 3, 4, 4])
## array([[1, 2],
## [1, 2],
## [3, 4],
## [3, 4]])
## array([[1, 1, 2, 2],
## [3, 3, 4, 4]])
## array([[1, 1, 2, 2, 2],
## [3, 3, 4, 4, 4]])
## array([[1, 2, 1, 2],
## [3, 4, 3, 4]])
## array([[1, 2],
## [3, 4],
## [1, 2],
## [3, 4]])
## array([[1, 2, 1, 2],
## [3, 4, 3, 4],
## [1, 2, 1, 2],
## [3, 4, 3, 4],
## [1, 2, 1, 2],
## [3, 4, 3, 4]])
## array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])
## array([90, 50, 20, 70])
## array([90, 50, 20, 70])
## array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])
## array([ 0, 10, -99, 30, 40, -99, 60, -99, 80, -99])
## array([ 0, 10, 2, 30, 40, 1, 60, 3, 80, 0])
## array([1, 1, 0, 0])
## array([[4, 5, 6, 7],
## [4, 5, 6, 7],
## [0, 1, 2, 3],
## [0, 1, 2, 3]])
## array([[1, 1, 0, 0],
## [5, 5, 4, 4],
## [9, 9, 8, 8]])
Broadcasting is about arithmetic work between arrays with different shapes.
## array([ 0, 5, 10, 15, 20, 25])
## array([5, 5, 5, 5, 5, 5])
## array([ 0, 5, 10, 15, 20, 25])
## array([[0, 2],
## [2, 4],
## [4, 6]])
## array([[0, 1],
## [0, 1],
## [0, 1]])
## array([[0, 2],
## [2, 4],
## [4, 6]])
## array([[0, 1],
## [3, 4],
## [6, 7]])
## array([[0, 0],
## [1, 1],
## [2, 2]])
## array([[0, 1],
## [3, 4],
## [6, 7]])
data2d = np.arange(6).reshape(3,2)
vec1d = np.arange(3)
data2d + vec1d
## array([[ 0, 1, 2],
## [ 3, 4, 5],
## [ 6, 7, 8],
## [ 9, 10, 11]])
## array([[-1., 0., 1.],
## [-1., 0., 1.],
## [-1., 0., 1.],
## [-1., 0., 1.]])
## array([[-4.5, -4.5, -4.5],
## [-1.5, -1.5, -1.5],
## [ 1.5, 1.5, 1.5],
## [ 4.5, 4.5, 4.5]])
## (3, 1, 3)
## (2, 3, 4)
## array([[ 0.2766035 , -0.76656096, -0.01555895],
## [-0.7650591 , 0.12205003, 0.13344552]])
## array([[ 0.00000000e+00, -5.55111512e-17, 1.11022302e-16],
## [ 0.00000000e+00, -2.77555756e-17, 1.38777878e-17]])
## 3
## 10
## array([ 1, 3, 6, 10])
## array([ 1, 3, 6, 10])
## 24
## array([ 1, 2, 6, 24])
## array([ 1, 2, 6, 24])
np.save will save the numpy data in binary format, which is efficient
data = np.arange(1,10)
np.save("my_data.npy", data)
np.load("my_data.npy")
data1 = np.arange(1,10); data2 = np.arange(2,11);
np.savez("my_data2.npz", x=data1, y=data2)
dat = np.load("my_data2.npz")
dat["x"]
dat["y"]