如何乘法numpy的矩阵不同VS数组类? [英] how does multiplication differ for NumPy Matrix vs Array classes?

查看:165
本文介绍了如何乘法numpy的矩阵不同VS数组类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该numpy的文档推荐使用数组,而不是矩阵与矩阵的工作。然而,与八度(我使用,直到最近这是),*不执行矩阵乘法,你需要使用的功能matrixmultipy()。我觉得这使得code非常不可读。

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?

推荐答案

主要的原因,以避免使用矩阵类是一个),这是本质上的2维和二)有相比于正常numpy的阵列的额外开销。如果你正在做的是线性代数,然后通过各种手段,随意使用矩阵类......我个人觉得它更多的麻烦比它的价值,虽然。

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.

有关阵列(前的Python 3.5),使用 而不是 MatrixMultiply函数

For arrays (prior to Python 3.5), use dot instead of matrixmultiply.

例如

import numpy as np
x = np.arange(9).reshape((3,3))
y = np.arange(3)

print np.dot(x,y)

还是在numpy的更新版本,只需使用 x.dot(Y)

就个人而言,我觉得它更具可读性比 * 运营商暗示矩阵乘法...

Personally, I find it much more readable than the * operator implying matrix multiplication...

有关在Python 3.5阵列,使用 X @是。

For arrays in Python 3.5, use x @ y.

这篇关于如何乘法numpy的矩阵不同VS数组类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆