numpy矩阵乘法

NumPy matrix multiplication can be done by the following three methods.

NumPy矩阵乘法可以通过以下三种方法完成。

  1. multiply(): element-wise matrix multiplication.multiple():逐元素矩阵乘法。
  2. matmul(): matrix product of two arrays.matmul():两个数组的矩阵乘积。
  3. dot(): dot product of two arrays.dot():两个数组的点积。

1. NumPy矩阵乘法元素明智 (1. NumPy Matrix Multiplication Element Wise)

If you want element-wise matrix multiplication, you can use multiply() function.

如果要逐元素矩阵相乘,可以使用multiple()函数。

import numpy as nparr1 = np.array([[1, 2],[3, 4]])
arr2 = np.array([[5, 6],[7, 8]])arr_result = np.multiply(arr1, arr2)print(arr_result)

Output:

输出:

[[ 5 12][21 32]]

The below image shows the multiplication operation performed to get the result matrix.

下图显示了为获得结果矩阵而执行的乘法运算。

Numpy Matrix multiply()

numpy矩阵乘法()

2.两个NumPy阵列的矩阵乘积 (2. Matrix Product of Two NumPy Arrays)

If you want the matrix product of two arrays, use matmul() function.

如果要两个数组的矩阵乘积,请使用matmul()函数。

import numpy as nparr1 = np.array([[1, 2],[3, 4]])
arr2 = np.array([[5, 6],[7, 8]])arr_result = np.matmul(arr1, arr2)print(f'Matrix Product of arr1 and arr2 is:\n{arr_result}')arr_result = np.matmul(arr2, arr1)print(f'Matrix Product of arr2 and arr1 is:\n{arr_result}')

Output:

输出:

Matrix Product of arr1 and arr2 is:
[[19 22][43 50]]
Matrix Product of arr2 and arr1 is:
[[23 34][31 46]]

The below diagram explains the matrix product operations for every index in the result array. For simplicity, take the row from the first array and the column from the second array for each index. Then multiply the corresponding elements and then add them to reach the matrix product value.

下图说明了结果数组中每个索引的矩阵乘积运算。 为简单起见,为每个索引取第一个数组的行和第二个数组的列。 然后乘以相应的元素,然后将它们相加以达到矩阵乘积值。

Numpy Matrix Product

块状矩阵产品

The matrix product of two arrays depends on the argument position. So matmul(A, B) might be different from matmul(B, A).

两个数组的矩阵乘积取决于参数位置。 因此,matmul(A,B)可能与matmul(B,A)不同。

3.两个NumPy阵列的点积 (3. Dot Product of Two NumPy Arrays)

The numpy dot() function returns the dot product of two arrays. The result is the same as the matmul() function for one-dimensional and two-dimensional arrays.

numpy dot()函数返回两个数组的点积。 结果与一维和二维数组的matmul()函数相同。

import numpy as nparr1 = np.array([[1, 2],[3, 4]])
arr2 = np.array([[5, 6],[7, 8]])arr_result = np.dot(arr1, arr2)print(f'Dot Product of arr1 and arr2 is:\n{arr_result}')arr_result = np.dot(arr2, arr1)print(f'Dot Product of arr2 and arr1 is:\n{arr_result}')arr_result = np.dot([1, 2], [5, 6])
print(f'Dot Product of two 1-D arrays is:\n{arr_result}')

Output:

输出:

Dot Product of arr1 and arr2 is:
[[19 22][43 50]]
Dot Product of arr2 and arr1 is:
[[23 34][31 46]]
Dot Product of two 1-D arrays is:
17
  • numpy.square()numpy.square()
  • NumPy sqrt() – Square Root of Matrix ElementsNumPy sqrt()–矩阵元素的平方根
  • Python NumPy TutorialPython NumPy教程

参考资料 (References)

  • numpy matmul()numpy matmul()
  • numpy multiply()numpy的multiple()

翻译自: https://www.journaldev.com/32966/numpy-matrix-multiplication

numpy矩阵乘法

numpy矩阵乘法_NumPy矩阵乘法相关推荐

  1. python 矩阵库_NumPy 矩阵库(Matrix)

    NumPy 中包含了一个矩阵库 numpy.matlib,该模块中的函数返回的是一个矩阵,而不是 ndarray 对象. 由 m × n 个数aij排成的 m 行 n 列的数表称为 m 行 n 列的矩 ...

  2. python的常见矩阵除法_Numpy矩阵除法返回所有零

    我对下面的矩阵有个除法错误.我想用行和的10×1向量除以10×10matrix.在[[5731, 3, 20, 8, 12, 54, 46, 8, 39, 2], [ 2, 6472, 47, 24, ...

  3. numpy 矩阵乘法_NumPy 运算规则总结

    1. 问题 ndarray 是 NumPy 的基础元素,NumPy 又主要是用来进行矩阵运算的.那么具体来说,ndarray 是如何进行普通矩阵运算的呢? 2. 分析 首先,在矩阵用 +-*/ 这些常 ...

  4. python三维矩阵乘法_tensorflow 二维矩阵乘以三维矩阵,高维矩阵相乘

    最近看论文时看到了一个让我费解的操作. 二维矩阵 * 三维矩阵 * 二维矩阵 得到了一个二维矩阵. 即: (n * c1) x (m * c1 * c2) x (n * c2) 得 n * m 实现主 ...

  5. 【Android 应用开发】Paint 滤镜原理 之 颜色矩阵 ( 颜色模式 | 颜色通道 | 颜色矩阵 | 矩阵运算 | 矩阵乘法 | 矩阵加法 | 颜色矩阵深入解析 )

    文章目录 颜色模式 颜色通道 Android 中的颜色矩阵 矩阵乘法运算 滤镜中的矩阵乘法运算 矩阵加法运算 滤镜中的矩阵乘法运算 滤镜运算原理 ( 总结 ) 实际滤镜理论示例 颜色模式 颜色模式 : ...

  6. 疯子的算法总结(五) 矩阵乘法 (矩阵快速幂)

    学过线性代数的都知道矩阵的乘法,矩阵乘法条件第为一个矩阵的行数等与第二个矩阵的列数,乘法为第一个矩阵的第一行乘以第二个矩阵的第一列的对应元素的和作为结果矩阵的第一行第一列的元素.(详解参见线性代数) ...

  7. Vijos 1603 ----迷宫(矩阵乘法,矩阵快速幂)

    描述 在某个神秘的星球上有一个游乐园 游乐园里有一个奇怪的迷宫,迷宫内有n个点,每个点之间都可能会有一条有向边(可能会有自环) 现在游乐园主有个问题想请你帮忙: 问:从s点走到f点,恰好走过m条边(边 ...

  8. 矩阵问题入门(矩阵乘法and矩阵快速幂)acm寒假集训日记22/1/15

    今天凌晨3点才睡,没想到通过看小说抑制玩游戏,反而看小说的时间更长. u1s1:那小说太刺激了,晚上看很有感觉,风吹草动我就会猛地看过去(类似茄子说柜子动了,哈哈),真TM(语气词)练胆量!!!..Q ...

  9. lu分解法matlab_MIT 18.065—机器学习中的矩阵方法02 矩阵乘法与矩阵分解

    数据分析.信号处理和机器学习中的矩阵方法 第02讲 矩阵乘法与矩阵分解 新MIT 线性代数|机器学习(中英机翻字幕)18.065 by Gilbert Strang_哔哩哔哩 (゜-゜)つロ 干杯~- ...

最新文章

  1. 笔记:2016-06-03
  2. 亿科影视管理系统1.2.0版以及1.0版本均有后门
  3. java通过异常处理错误_java 通过异常处理错误
  4. 粒子群matlab工具箱,科学网—PSO粒子群优化算法Matlab工具箱 - 白途思的博文
  5. 获取客户端的IP地址
  6. 基于HT for Web的Web SCADA工控移动应用
  7. NYOJ 7-街区最短路径问题(曼哈顿距离)
  8. cad隐藏图层命令快捷键_这10个cad图层管理命令,让你画图速度飞起来!
  9. 3D点云目标检测综述
  10. 国家铁路局招聘面试题汇集
  11. 一百个超级值得收藏网站,确定看见不点开看看嘛
  12. ibm服务器卡在开机界面_使用HTTPS配置IBM Integration Bus Web用户界面
  13. 一位北大才子的光棍节情诗
  14. 计算机网络 方面应用研究,计算机网络技术应用研究
  15. 从数字化转变为数字化敏捷
  16. POI生成word文档,包括标题,段落,表格,统计图(非图片格式)
  17. AtCoder Beginner Contest 172 总结
  18. pdf添加书签操作介绍
  19. python创建_python 创建txt并写入Python基础1 Hello World!
  20. MSSQL数据库安全实验

热门文章

  1. AE “每用户订阅上的所有人SID 不存在”
  2. 图片随鼠标滚动而任意浮动
  3. Linux 文件内容替换命令
  4. MapReduce的map流程
  5. [转载] python字典查询功能_Python中的字典功能
  6. Jrebel激活方法
  7. Python返回多个值
  8. 深入理解JVM之JVM内存区域与内存分配
  9. 大小端判断及相互转化
  10. Global GUI map for automation with VS.NET