python/numpy中的矩阵乘法(np.dot, np.multiply, *)

1. np.dot(A, B)
对于二维矩阵,和线性代数中矩阵乘法的定义相同,计算矩阵的乘积;
对于一维矩阵,计算两者的内积。

示例代码

import numpy as np

# 2-D array: 2 x 3
two_dim_matrix_one = np.array([[1, 2, 3], [4, 5, 6]])
# 2-D array: 3 x 2
two_dim_matrix_two = np.array([[1, 2], [3, 4], [5, 6]])

two_multi_res = np.dot(two_dim_matrix_one, two_dim_matrix_two)
print('two_multi_res: %s' %(two_multi_res))

# 1-D array
one_dim_vec_one = np.array([1, 2, 3])
one_dim_vec_two = np.array([4, 5, 6])
one_result_res = np.dot(one_dim_vec_one, one_dim_vec_two)
print('one_result_res: %s' %(one_result_res))

结果如下:

two_multi_res: [[22 28]
 [49 64]]
one_result_res: 32

2. np.multiply()或 *
实现对应元素相乘(element-wise product)

示例代码

import numpy as np

# 2-D array: 2 x 3
two_dim_matrix_one = np.array([[1, 2, 3], [4, 5, 6]])
another_two_dim_matrix_one = np.array([[7, 8, 9], [4, 7, 1]])

# 对应元素相乘 element-wise product
element_wise = two_dim_matrix_one * another_two_dim_matrix_one
print('element wise product: %s' %(element_wise))

# 对应元素相乘 element-wise product
element_wise_2 = np.multiply(two_dim_matrix_one, another_two_dim_matrix_one)
print('element wise product: %s' % (element_wise_2))

结果如下:

element wise product: [[ 7 16 27]
 [16 35  6]]
element wise product: [[ 7 16 27]
 [16 35  6]]

猜你喜欢

转载自blog.csdn.net/weixin_41278720/article/details/80101322