Numpy中的几种矩阵乘法 np.dot, np.multiply, *
2018-03-06 20:03
5161 查看
[b]使用array时,运算符multiply、 * 用于分别计算两个数的相乘(1),函数 dot() 同线性代数中矩阵乘法的定义(2)(对于秩为1的数组,执行对应位置相乘,然后再相加;对于秩不为1的二维数组,执行矩阵乘法运算;)[/b]
[b]使用matrix时,运算符 * 、 dot() 用于(2),函数 multiply() 用于(1).[/b]
1. 同线性代数中矩阵乘法的定义: np.dot()
np.dot(A, B):对于二维矩阵,计算真正意义上的矩阵乘积,同线性代数中矩阵乘法的定义。对于一维矩阵,计算两者的内积。见如下Python代码: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: 322. 对应元素相乘: np.multiply(), 或 *
在Python中,实现对应元素相乘,有2种方式,一个是np.multiply(),另外一个是*。见如下Python代码: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]]
[b]使用matrix时,运算符 * 、 dot() 用于(2),函数 multiply() 用于(1).[/b]
1. 同线性代数中矩阵乘法的定义: np.dot()
np.dot(A, B):对于二维矩阵,计算真正意义上的矩阵乘积,同线性代数中矩阵乘法的定义。对于一维矩阵,计算两者的内积。见如下Python代码: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: 322. 对应元素相乘: np.multiply(), 或 *
在Python中,实现对应元素相乘,有2种方式,一个是np.multiply(),另外一个是*。见如下Python代码: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]]
相关文章推荐
- Python 中的几种矩阵乘法 np.dot, np.multiply, *
- Python 中的几种矩阵乘法 np.dot, np.multiply, *
- Python 中的几种矩阵乘法 np.dot, np.multiply, *【转】
- 【Numpy】 乘法 np.multiply np.dot
- NumPy中的乘法运算符 * 指示按元素计算,矩阵乘法可以使用 dot 函数或创建矩阵对象实现
- Numpy np.dot() vs np.multiply() vs *
- python中np.multiply()、np.dot()和星号(*)三种乘法运算的区别
- numpy中矩阵乘法,星乘(*)和点乘(.dot)的区别
- python numpy中*,np.dot(),np.multiply()区
- Python numpy 矩阵特殊加、乘法与循环优化
- Python中的几种矩阵乘法
- <Python> Numpy中的矩阵乘法问题
- numpy中的矩阵乘法
- Python2.7学习笔记-矩阵乘法matrix vs. ndarray ; dot vs. inner,
- numpy矩阵与向量类型的向量乘法
- matlab 和 numpy 矩阵乘法异同
- C语言 矩阵的几种乘法
- Python numpy 转置、逆、去掉一列、按列取出、矩阵拼接、矩阵排序、矩阵相等、np.where,一维转二维
- 线性代数笔记-矩阵乘法的几种求法
- numpy中dot、multiply和asterisk的区别和联系。