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 routines which allocate memory and fill arrays with valuea=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 argumenta=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 valuesa=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 vectorsa=np.arange(10)
print(a)
# [0 1 2 3 4 5 6 7 8 9]#access an elementprint(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 endprint(f"a[-1] = {a[-1]}")
# a[-1] = 9
切片访问
#vector slicing operationsa=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 abovec=a[3:]; print("a[3:] = ", c)
# access all elements below index 3c=a[:3]; print("a[:3] = ", c)
# access all elementsc=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 ab=-aprint(f"b = -a : {b}")
# sum all elements of a, returns a scalarb=np.sum(a)
print(f"b = np.sum(a) : {b}")
b=np.mean(a)
print(f"b = np.mean(a): {b}")
b=a**2print(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 scalarb=5*aprint(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 valuesa=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 rowsprint(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 matricesa=np.arange(6).reshape(-1, 2) #reshape is a convenient way to create matricesprint(f"a.shape: {a.shape}, \na= {a}")
#access an elementprint(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 rowprint(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'>
NumPy 学习笔记
相关资源
创建 Vector
使用长度创建 Vector
使用元素创建 Vector
Vector 操作
索引访问
切片访问
对一整个 Vector 操作
创建矩阵
矩阵操作
索引访问
切片访问
The text was updated successfully, but these errors were encountered: