Python 中的几种矩阵乘法 np.dot, np.multiply, *【转】
本文转载自:https://www.geek-share.com/detail/2703505798.html
Python中的几种矩阵乘法
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))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
结果如下:
two_multi_res: [[22 28]
[49 64]]
one_result_res: 32
1
2
3
2. 对应元素相乘 element-wise product: 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))
1
2
3
4
5
6
7
8
9
10
11
12
13
结果如下:
element wise product: [[ 7 16 27]
[16 35 6]]
element wise product: [[ 7 16 27]
[16 35 6]]
---------------------
作者:cltdevelop
来源:CSDN
原文:https://www.geek-share.com/detail/2703505798.html?utm_source=copy
版权声明:本文为博主原创文章,转载请附上博文链接!
- Python 中的几种矩阵乘法 np.dot, np.multiply, *
- Python 中的几种矩阵乘法 np.dot, np.multiply, *
- Numpy中的几种矩阵乘法 np.dot, np.multiply, *
- python中np.multiply()、np.dot()和星号(*)三种乘法运算的区别
- 【Python】python中的乘法dot和对应分量相乘multiply
- Python中的几种矩阵乘法
- 【Numpy】 乘法 np.multiply np.dot
- python函数- exp、np.linalg.norm、np.multiply()、np.dot()、*、outer()、squeeze
- python numpy中*,np.dot(),np.multiply()区
- Python2.7学习笔记-矩阵乘法matrix vs. ndarray ; dot vs. inner,
- python中实现矩阵乘法
- numpy中矩阵乘法,星乘(*)和点乘(.dot)的区别
- Python numpy库中的array,list与矩阵的乘法,以及增添元素的方法
- python 多线程稀疏矩阵乘法
- 算法导论程序4--矩阵乘法的分治算法(Python)
- 基于Cuda的几种并行稀疏矩阵乘法方法(一)
- 分治策略之矩阵乘法的几种实现
- <Python> Numpy中的矩阵乘法问题
- Python 线性代数 矩阵乘法
- python tensorflow学习笔记(五)矩阵乘法运算