NumPy 矩阵类与数组类的乘法有何不同?
numpy 文档建议使用数组而不是矩阵来处理矩阵。然而,与octave(我最近才使用)不同,* 不执行矩阵乘法,您需要使用函数matrixmultipy()。我觉得这使得代码非常难以阅读。
有人同意我的观点并找到解决方案吗?
The numpy docs recommend using array instead of matrix for working with matrices. However, unlike octave (which I was using till recently), * doesn't perform matrix multiplication, you need to use the function matrixmultipy(). I feel this makes the code very unreadable.
Does anybody share my views, and has found a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
避免使用 Matrix 类的主要原因是 a) 它本质上是二维的,b) 与“普通”numpy 数组相比有额外的开销。如果您所做的只是线性代数,那么无论如何,请随意使用矩阵类......但就我个人而言,我发现它比它的价值更麻烦。
对于数组(Python 3.5 之前的版本),请使用
dot< /code>
而不是
matrixmultiply
。例如
,或者在较新版本的 numpy 中,只需使用
x.dot(y)
就我个人而言,我发现它比暗示矩阵乘法的
*
运算符更具可读性......对于数组在 Python 3.5 中,使用 x @ y。
The main reason to avoid using the
matrix
class is that a) it's inherently 2-dimensional, and b) there's additional overhead compared to a "normal" numpy array. If all you're doing is linear algebra, then by all means, feel free to use the matrix class... Personally I find it more trouble than it's worth, though.For arrays (prior to Python 3.5), use
dot
instead ofmatrixmultiply
.E.g.
Or in newer versions of numpy, simply use
x.dot(y)
Personally, I find it much more readable than the
*
operator implying matrix multiplication...For arrays in Python 3.5, use
x @ y
.NumPy 数组 操作与 NumPy 矩阵 操作需要了解的关键事项是:
NumPy 矩阵是子类< /em> NumPy 数组
NumPy 数组操作是逐元素(一旦考虑到广播)
NumPy 矩阵运算遵循线性代数的普通规则
一些代码片段来说明:
但是如果这两个 NumPy 矩阵转换为数组,则此操作将失败:
尽管使用 NP.dot 语法适用于数组;该运算的工作原理类似于矩阵乘法:
那么您是否需要 NumPy 矩阵?即,NumPy 数组是否足以进行线性代数计算(前提是您知道正确的语法,即 NP.dot)?
规则似乎是,如果参数(数组)的形状(mxn)与给定的线性代数运算兼容,那么就可以,否则,NumPy 会抛出异常。
我遇到的唯一例外(可能还有其他例外)是计算矩阵逆。
下面是我调用纯线性代数运算(实际上,来自 Numpy 的线性代数模块)并传入
数组的 NumPy 数组行列式的片段:
行列式 >特征向量/特征值对:
矩阵范数:
qr因式分解 :
矩阵秩:
矩阵条件:
反转虽然需要 NumPy 矩阵:
但是 Moore-Penrose 伪逆 似乎工作得很好
the key things to know for operations on NumPy arrays versus operations on NumPy matrices are:
NumPy matrix is a subclass of NumPy array
NumPy array operations are element-wise (once broadcasting is accounted for)
NumPy matrix operations follow the ordinary rules of linear algebra
some code snippets to illustrate:
but this operations fails if these two NumPy matrices are converted to arrays:
though using the NP.dot syntax works with arrays; this operations works like matrix multiplication:
so do you ever need a NumPy matrix? ie, will a NumPy array suffice for linear algebra computation (provided you know the correct syntax, ie, NP.dot)?
the rule seems to be that if the arguments (arrays) have shapes (m x n) compatible with the a given linear algebra operation, then you are ok, otherwise, NumPy throws.
the only exception i have come across (there are likely others) is calculating matrix inverse.
below are snippets in which i have called a pure linear algebra operation (in fact, from Numpy's Linear Algebra module) and passed in a NumPy array
determinant of an array:
eigenvectors/eigenvalue pairs:
matrix norm:
qr factorization:
matrix rank:
matrix condition:
inversion requires a NumPy matrix though:
but the Moore-Penrose pseudoinverse seems to works just fine
在 3.5 中,Python 终于有了矩阵乘法运算符。语法为
a @ b
。In 3.5, Python finally got a matrix multiplication operator. The syntax is
a @ b
.在某些情况下,点运算符在处理数组和处理矩阵时会给出不同的答案。例如,假设如下:
让我们将它们转换为矩阵:
现在,我们可以看到两种情况的不同输出:
There is a situation where the dot operator will give different answers when dealing with arrays as with dealing with matrices. For example, suppose the following:
Lets convert them into matrices:
Now, we can see a different output for the two cases:
参考http://docs.scipy.org/doc/scipy/reference/tutorial/linalg。 html
...,不鼓励使用 numpy.matrix 类,因为它不会添加任何 2D numpy.ndarray 无法完成的功能 对象,并可能导致混淆正在使用哪个类。例如,
scipy.linalg运算同样可以应用于numpy.matrix或二维numpy.ndarray对象。
Reference from http://docs.scipy.org/doc/scipy/reference/tutorial/linalg.html
..., the use of the numpy.matrix class is discouraged, since it adds nothing that cannot be accomplished with 2D numpy.ndarray objects, and may lead to a confusion of which class is being used. For example,
scipy.linalg operations can be applied equally to numpy.matrix or to 2D numpy.ndarray objects.
这个技巧可能就是您正在寻找的。它是一种简单的运算符重载。
然后,您可以使用类似于建议的 Infix 类的内容,如下所示:
This trick could be what you are looking for. It is a kind of simple operator overload.
You can then use something like the suggested Infix class like this:
相关引用来自 PEP 465 - 用于矩阵乘法的专用中缀运算符,如@petr-viktorin 提到的,澄清了 OP 遇到的问题:
@
中缀运算符的引入应该帮助统一和简化Python矩阵代码。A pertinent quote from PEP 465 - A dedicated infix operator for matrix multiplication , as mentioned by @petr-viktorin, clarifies the problem the OP was getting at:
The introduction of the
@
infix operator should help to unify and simplify python matrix code.函数 matmul (自 numpy 1.10.1 起)有效对于这两种类型都很好,并以 numpy 矩阵类的形式返回结果:
输出:
从 python 3.5 开始,早期提到,您也可以使用新的矩阵乘法运算符
@
like并得到与上面相同的结果。
Function matmul (since numpy 1.10.1) works fine for both types and return result as a numpy matrix class:
Output:
Since python 3.5 as mentioned early you also can use a new matrix multiplication operator
@
likeand get the same result as above.