混合numpy矩阵和数组的危险 [英] Danger of mixing numpy matrix and array

查看:62
本文介绍了混合numpy矩阵和数组的危险的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究的科学/工程应用程序具有很多线性代数矩阵乘法,因此我使用Numpy矩阵.但是,python中有许多函数可以互换地接受矩阵或数组类型.很好,不是吗?好吧,不是真的.让我用一个例子来演示这个问题:

The science/engineering application I'm working on has lots of linear algebra matrix multiplications, therefore I use Numpy matrices. However, there are many functions in python that interchangeably accept matrix or array types. Nice, no? Well, not really. Let me demonstrate the problem with an example:

from scipy.linalg import expm
from numpy import matrix

# Setup input variable as matrix
A = matrix([[ 0, -1.0,  0,  0],
            [ 0,  0,  0,  1.0],
            [ 0,  0,  0,  0],
            [ 0,  0,  1.0,  0]])

# Do some computation with that input
B = expm(A)

b1 = B[0:2, 2:4]
b2 = B[2:4, 2:4].T

# Compute and Print the desired output
print "The innocent but wrong answer:"
print b2 * b1

print "The answer I should get:"
print matrix(b2) * matrix(b1)

运行时您会得到:

The innocent but wrong answer:
[[-0.16666667 -0.5       ]
 [ 0.          1.        ]]
The answer I should get, since I expected everything to still be matrices:
[[ 0.33333333  0.5       ]
 [ 0.5         1.        ]]

关于如何避免此类混淆的任何提示或建议?始终将变量包装在matrix()调用中以确保它们仍然是矩阵,这确实很麻烦.在这方面似乎没有标准,因此可能导致难以检测的错误.

Any tips or advice on how to avoid this sort of a mix up? Its really messy to keep wrapping variables in matrix() calls to ensure they still are matrices. It seems there is no standard in this regard, and so it can lead to bugs that are hard to detect.

推荐答案

出于以下原因,我倾向于在numpy中使用array代替matrix:

I tend to use array instead of matrix in numpy for a few reasons:

  1. matrix严格为2D,而您可以具有任意尺寸的numpy array.
  2. 除了一些区别之外,arraymatrix操作对于可互换 Matlab用户.
  3. 如果始终使用array,则可以使用numpy.dot()(或在Python 3.5中使用新的@二进制运算符)进行矩阵乘法.这样可以避免不确定*在代码中实际做什么的问题.而且,当您遇到乘法错误时,由于您确定要执行哪种乘法,就可以更轻松地发现问题.
  1. matrix is strictly 2D whereas you can have a numpy array of any dimension.
  2. Aside from a few differences, array and matrix operations are pretty much interchangeable for a Matlab user.
  3. If you use array consistently, then you would use numpy.dot() (or in Python 3.5 the new @ binary operator) for matrix multiplication. This will prevent the problem of not sure what * actually does in your code. And when you encounter a multiplication error, you can find the problem easier since you are certain of what kind of multiplication you are trying to perform.

因此,我建议您尝试坚持使用numpy.array,但也要记住arraymatrix之间的区别.

So I would suggest you try to stick to numpy.array, but also keep in mind the differences between array and matrix.

最后,我发现在 bpython 上使用numpy/scipy很高兴.自动提示可以帮助您以比通常需要查阅numpy/scipy文档的速度更快的速度来学习要使用的功能的属性.

Lastly, I found it a joy to work with numpy/scipy on bpython. The auto-prompt helps you to learn the properties of the function you are trying to use at a much faster pace than having to consult the numpy/scipy doc constantly.

修改: arraymatrix之间的区别也许可以在这里得到最好的回答: '矩阵'?我应该使用哪个?

The difference between array vs matrix is perhaps best answered here: 'array' or 'matrix'? Which should I use?

这篇关于混合numpy矩阵和数组的危险的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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