Open
Description
NumPy 学习笔记
相关资源
- NumPy Documentation including a basic introduction: NumPy.org
- A challenging feature topic: NumPy Broadcasting
创建 Vector
使用长度创建 Vector
# NumPy routines which allocate memory and fill arrays with value
a = np.zeros(4); print(f"np.zeros(4) : a = {a}, a shape = {a.shape}, a data type = {a.dtype}")
a = np.zeros((4,)); print(f"np.zeros(4,) : a = {a}, a shape = {a.shape}, a data type = {a.dtype}")
a = np.random.random_sample(4); print(f"np.random.random_sample(4): a = {a}, a shape = {a.shape}, a data type = {a.dtype}")
# Output
# np.zeros(4) : a = [0. 0. 0. 0.], a shape = (4,), a data type = float64
# np.zeros(4,) : a = [0. 0. 0. 0.], a shape = (4,), a data type = float64
# np.random.random_sample(4): a = [0.43275971 0.78989577 0.39071854 0.3555822 ], a shape = (4,), a data type = float64
# NumPy routines which allocate memory and fill arrays with value but do not accept shape as input argument
a = np.arange(4.); print(f"np.arange(4.): a = {a}, a shape = {a.shape}, a data type = {a.dtype}")
a = np.random.rand(4); print(f"np.random.rand(4): a = {a}, a shape = {a.shape}, a data type = {a.dtype}")
# Output
# np.arange(4.): a = [0. 1. 2. 3.], a shape = (4,), a data type = float64
# np.random.rand(4): a = [0.11359497 0.04380214 0.84767525 0.06349301], a shape = (4,), a data type = float64
使用元素创建 Vector
# NumPy routines which allocate memory and fill with user specified values
a = np.array([5,4,3,2]); print(f"np.array([5,4,3,2]): a = {a}, a shape = {a.shape}, a data type = {a.dtype}")
a = np.array([5.,4,3,2]); print(f"np.array([5.,4,3,2]): a = {a}, a shape = {a.shape}, a data type = {a.dtype}")
# Output
# np.array([5,4,3,2]): a = [5 4 3 2], a shape = (4,), a data type = int32
# np.array([5.,4,3,2]): a = [5. 4. 3. 2.], a shape = (4,), a data type = float64
Vector 操作
索引访问
#vector indexing operations on 1-D vectors
a = np.arange(10)
print(a)
# [0 1 2 3 4 5 6 7 8 9]
#access an element
print(f"a[2].shape: {a[2].shape} a[2] = {a[2]}, Accessing an element returns a scalar")
# a[2].shape: () a[2] = 2, Accessing an element returns a scalar
# access the last element, negative indexes count from the end
print(f"a[-1] = {a[-1]}")
# a[-1] = 9
切片访问
#vector slicing operations
a = np.arange(10)
print(f"a = {a}")
#access 5 consecutive elements (start:stop:step)
c = a[2:7:1]; print("a[2:7:1] = ", c)
# access 3 elements separated by two
c = a[2:7:2]; print("a[2:7:2] = ", c)
# access all elements index 3 and above
c = a[3:]; print("a[3:] = ", c)
# access all elements below index 3
c = a[:3]; print("a[:3] = ", c)
# access all elements
c = a[:]; print("a[:] = ", c)
# Output
# a = [0 1 2 3 4 5 6 7 8 9]
# a[2:7:1] = [2 3 4 5 6]
# a[2:7:2] = [2 4 6]
# a[3:] = [3 4 5 6 7 8 9]
# a[:3] = [0 1 2]
# a[:] = [0 1 2 3 4 5 6 7 8 9]
对一整个 Vector 操作
a = np.array([1,2,3,4])
print(f"a : {a}")
# negate elements of a
b = -a
print(f"b = -a : {b}")
# sum all elements of a, returns a scalar
b = np.sum(a)
print(f"b = np.sum(a) : {b}")
b = np.mean(a)
print(f"b = np.mean(a): {b}")
b = a**2
print(f"b = a**2 : {b}")
# Output
# a : [1 2 3 4]
# b = -a : [-1 -2 -3 -4]
# b = np.sum(a) : 10
# b = np.mean(a): 2.5
# b = a**2 : [ 1 4 9 16]
a = np.array([ 1, 2, 3, 4])
b = np.array([-1,-2, 3, 4])
print(f"Binary operators work element wise: {a + b}")
# Output
# Binary operators work element wise: [0 0 6 8]
a = np.array([1, 2, 3, 4])
# multiply a by a scalar
b = 5 * a
print(f"b = 5 * a : {b}")
# Output
# b = 5 * a : [ 5 10 15 20]
a = np.array([1, 2, 3, 4])
b = np.array([-1, 4, 3, 2])
c = np.dot(a, b)
print(f"NumPy 1-D np.dot(a, b) = {c}, np.dot(a, b).shape = {c.shape} ")
c = np.dot(b, a)
print(f"NumPy 1-D np.dot(b, a) = {c}, np.dot(a, b).shape = {c.shape} ")
# Output
# NumPy 1-D np.dot(a, b) = 24, np.dot(a, b).shape = ()
# NumPy 1-D np.dot(b, a) = 24, np.dot(a, b).shape = ()
创建矩阵
a = np.zeros((1, 5))
print(f"a shape = {a.shape}, a = {a}")
a = np.zeros((2, 1))
print(f"a shape = {a.shape}, a = {a}")
a = np.random.random_sample((1, 1))
print(f"a shape = {a.shape}, a = {a}")
# Output
# a shape = (1, 5), a = [[0. 0. 0. 0. 0.]]
# a shape = (2, 1), a = [[0.]
# [0.]]
# a shape = (1, 1), a = [[0.44236513]]
# NumPy routines which allocate memory and fill with user specified values
a = np.array([[5], [4], [3]]); print(f" a shape = {a.shape}, np.array: a = {a}")
a = np.array([[5], # One can also
[4], # separate values
[3]]); #into separate rows
print(f" a shape = {a.shape}, np.array: a = {a}")
# Output
# a shape = (3, 1), np.array: a = [[5]
# [4]
# [3]]
# a shape = (3, 1), np.array: a = [[5]
# [4]
# [3]]
矩阵操作
索引访问
#vector indexing operations on matrices
a = np.arange(6).reshape(-1, 2) #reshape is a convenient way to create matrices
print(f"a.shape: {a.shape}, \na= {a}")
#access an element
print(f"\na[2,0].shape: {a[2, 0].shape}, a[2,0] = {a[2, 0]}, type(a[2,0]) = {type(a[2, 0])} Accessing an element returns a scalar\n")
#access a row
print(f"a[2].shape: {a[2].shape}, a[2] = {a[2]}, type(a[2]) = {type(a[2])}")
# Output
# a.shape: (3, 2),
# a= [[0 1]
# [2 3]
# [4 5]]
#
# a[2,0].shape: (), a[2,0] = 4, type(a[2,0]) = <class 'numpy.int32'> Accessing an element returns a scalar
#
# a[2].shape: (2,), a[2] = [4 5], type(a[2]) = <class 'numpy.ndarray'>
切片访问
#vector 2-D slicing operations
a = np.arange(20).reshape(-1, 10)
print(f"a = \n{a}")
#access 5 consecutive elements (start:stop:step)
print("a[0, 2:7:1] = ", a[0, 2:7:1], ", a[0, 2:7:1].shape =", a[0, 2:7:1].shape, "a 1-D array")
#access 5 consecutive elements (start:stop:step) in two rows
print("a[:, 2:7:1] = \n", a[:, 2:7:1], ", a[:, 2:7:1].shape =", a[:, 2:7:1].shape, "a 2-D array")
# access all elements
print("a[:,:] = \n", a[:,:], ", a[:,:].shape =", a[:,:].shape)
# access all elements in one row (very common usage)
print("a[1,:] = ", a[1,:], ", a[1,:].shape =", a[1,:].shape, "a 1-D array")
# same as
print("a[1] = ", a[1], ", a[1].shape =", a[1].shape, "a 1-D array")
# Output
# a =
# [[ 0 1 2 3 4 5 6 7 8 9]
# [10 11 12 13 14 15 16 17 18 19]]
# a[0, 2:7:1] = [2 3 4 5 6] , a[0, 2:7:1].shape = (5,) a 1-D array
# a[:, 2:7:1] =
# [[ 2 3 4 5 6]
# [12 13 14 15 16]] , a[:, 2:7:1].shape = (2, 5) a 2-D array
# a[:,:] =
# [[ 0 1 2 3 4 5 6 7 8 9]
# [10 11 12 13 14 15 16 17 18 19]] , a[:,:].shape = (2, 10)
# a[1,:] = [10 11 12 13 14 15 16 17 18 19] , a[1,:].shape = (10,) a 1-D array
# a[1] = [10 11 12 13 14 15 16 17 18 19] , a[1].shape = (10,) a 1-D array