1、创建数组
(1)创建数组
array_row1 = np.array([1, 2, 3]) # 一维数组,1*3
print("array_row1",array_row1.shape,array_row1)
结果为:array_row1 (3,) [1 2 3]
因为数组只有一个维度,所以第二为空。以下创建方式报错
array_col1 = np.array([1], [1], [1]) # 3*1
print("array_col1",array_col1.shape,array_col1)
(2)矩阵与数组相乘
矩阵与数组相乘不会报错,但是会导致结果错误,结果均为数组。
result1 (3,) [70 80 90]
result2 (3,) [70 80 90]
d1 = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]])
S1 = np.array([1, 2, 3, 4])
result1 = d1.T @ S1.T
result2 = d1.T @ S1
print("result1",result1.shape,result1)
print("result2",result2.shape,result2)
2、创建矩阵
(1)创建矩阵
array_row2 = np.array([[1], [2], [3]]) # 3*1
print("array_row2",array_row2.shape,array_row2)
array_col2 = np.array([[1, 2, 3]]) # 1*3
print("array_col2",array_col2.shape,array_col2)
分别创建行和列,上述代码结果为
array_row2 (3, 1) [[1]
[2]
[3]]
array_col2 (1, 3) [[1 2 3]]
(2)矩阵相乘
将数组转为矩阵,S2 = S1.reshape(1, 4)
S2 = S1.reshape(1, 4)
result3 = d1.T @ S2.T
print("result3",result3.shape,result3)
结果为:
result3 (3, 1) [[70]
[80]
[90]]