本文转载自:https://blog.csdn.net/u012609509/article/details/70230204

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://blog.csdn.net/u012609509/article/details/70230204?utm_source=copy
版权声明:本文为博主原创文章,转载请附上博文链接!

最新文章

  1. 为模版设计师而生的Twig(上)-Twig使用指南
  2. linq to sql 扩展方法
  3. VPS选购及辨别vps虚拟化技术
  4. Linux学习之Shell编程基础
  5. vs2010如何打开vs2013
  6. CI-持续集成(2)-软件工业“流水线”技术实现(转)
  7. centos7 crontab笔记
  8. 淘淘商城_day02_课堂笔记
  9. dplyr 数据操作 数据排序 (arrange)
  10. 使用JavaEE的ServerAuthModule模块和web.xml进行相应配置,实现对用户的权限控制
  11. LeetCode 695. Max Area of Island (岛的最大区域)
  12. arcgis api 3.x for js 入门开发系列八聚合效果(附源码下载)
  13. GET和POST传输方式
  14. Java语法基础常见疑惑解答8,16,17,21图片补充
  15. POJ 1733 Parity game 【带权并查集】+【离散化】
  16. 创建 .m2 文件夹
  17. iOS : 判断运行设备类型是否是iPad
  18. LeetCode--112--路径总和
  19. jQuery .tmpl(), .template()学习资料小结
  20. C#两个引用类的属性、方法 各位早安

热门文章

  1. 1067 - Combinations---LightOj(Lucas求组合数)
  2. 洛谷P2279 消防局的设立 [HNOI2003] 贪心
  3. zookeeper 版本不一致导致不断重连
  4. .NET基于分页控件实现真分页功能
  5. Loadrunner 手动关联技术
  6. 【Python】【Web.py】python调用html【问题:echart图标调用html上未显示】
  7. 全局ID的重要性
  8. GOLANG错误处理最佳方案errors wrap, Defer, Panic, and Recover
  9. visio 的使用方法
  10. Postman使用js获取日期