Python numpy 数组和矩阵之间有什么区别?我应该使用哪一种?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4151128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:29:04  来源:igfitidea点击:

What are the differences between numpy arrays and matrices? Which one should I use?

pythonarraysmatrixnumpy

提问by levesque

What are the advantages and disadvantages of each?

各自的优缺点是什么?

From what I've seen, either one can work as a replacement for the other if need be, so should I bother using both or should I stick to just one of them?

从我所看到的,如果需要,任何一个都可以替代另一个,那么我应该费心使用两者还是只坚持使用其中一个?

Will the style of the program influence my choice? I am doing some machine learning using numpy, so there are indeed lots of matrices, but also lots of vectors (arrays).

节目的风格会影响我的选择吗?我正在使用 numpy 进行一些机器学习,因此确实有很多矩阵,但也有很多向量(数组)。

采纳答案by Aks

As per the official documents, it's not anymore advisable to use matrix class since it will be removed in the future.

根据官方文档,不再建议使用矩阵类,因为将来会删除它。

https://numpy.org/doc/stable/reference/generated/numpy.matrix.html

https://numpy.org/doc/stable/reference/generated/numpy.matrix.html

As other answers already state that you can achieve all the operations with NumPy arrays.

正如其他答案已经指出的那样,您可以使用 NumPy 数组实现所有操作。

回答by unutbu

Numpy matricesare strictly 2-dimensional, while numpy arrays(ndarrays) are N-dimensional. Matrix objects are a subclass of ndarray, so they inherit all the attributes and methods of ndarrays.

Numpy矩阵是严格的二维矩阵,而 numpy数组(ndarrays) 是 N 维的。Matrix 对象是 ndarray 的子类,因此它们继承了 ndarrays 的所有属性和方法。

The main advantage of numpy matrices is that they provide a convenient notation for matrix multiplication: if a and b are matrices, then a*bis their matrix product.

numpy 矩阵的主要优点是它们为矩阵乘法提供了一种方便的表示法:如果 a 和 b 是矩阵,那么a*b它们的矩阵乘积就是。

import numpy as np

a = np.mat('4 3; 2 1')
b = np.mat('1 2; 3 4')
print(a)
# [[4 3]
#  [2 1]]
print(b)
# [[1 2]
#  [3 4]]
print(a*b)
# [[13 20]
#  [ 5  8]]

On the other hand, as of Python 3.5, NumPy supports infix matrix multiplication using the @operator, so you can achieve the same convenience of matrix multiplication with ndarrays in Python >= 3.5.

另一方面,从 Python 3.5 开始,NumPy 支持使用@运算符的中缀矩阵乘法,因此您可以在 Python >= 3.5 中使用 ndarrays 实现与矩阵乘法相同的便利。

import numpy as np

a = np.array([[4, 3], [2, 1]])
b = np.array([[1, 2], [3, 4]])
print(a@b)
# [[13 20]
#  [ 5  8]]

Both matrix objects and ndarrays have .Tto return the transpose, but matrix objects also have .Hfor the conjugate transpose, and .Ifor the inverse.

矩阵对象和 ndarrays 都.T必须返回转置,但矩阵对象也有.H共轭转置和.I逆。

In contrast, numpy arrays consistently abide by the rule that operations are applied element-wise (except for the new @operator). Thus, if aand bare numpy arrays, then a*bis the array formed by multiplying the components element-wise:

相比之下,numpy 数组始终遵守按元素应用操作的规则(除了 new@运算符)。因此,如果ab是 numpy 数组,则a*b是通过将组件元素相乘形成的数组:

c = np.array([[4, 3], [2, 1]])
d = np.array([[1, 2], [3, 4]])
print(c*d)
# [[4 6]
#  [6 4]]

To obtain the result of matrix multiplication, you use np.dot(or @in Python >= 3.5, as shown above):

要获得矩阵乘法的结果,请使用np.dot(或@在 Python >= 3.5 中,如上所示):

print(np.dot(c,d))
# [[13 20]
#  [ 5  8]]

The **operator also behaves differently:

**运营商还表现不同:

print(a**2)
# [[22 15]
#  [10  7]]
print(c**2)
# [[16  9]
#  [ 4  1]]

Since ais a matrix, a**2returns the matrix product a*a. Since cis an ndarray, c**2returns an ndarray with each component squared element-wise.

由于a是一个矩阵,a**2返回矩阵 product a*a。由于c是一个 ndarray,c**2返回一个 ndarray,每个分量按元素平方。

There are other technical differences between matrix objects and ndarrays (having to do with np.ravel, item selection and sequence behavior).

矩阵对象和 ndarrays 之间还有其他技术差异(与np.ravel、项目选择和序列行为有关)。

The main advantage of numpy arrays is that they are more general than 2-dimensional matrices. What happens when you want a 3-dimensional array? Then you have to use an ndarray, not a matrix object. Thus, learning to use matrix objects is more work -- you have to learn matrix object operations, and ndarray operations.

numpy 数组的主要优点是它们比二维矩阵更通用。当你想要一个 3 维数组时会发生什么?然后你必须使用一个 ndarray,而不是一个矩阵对象。因此,学习使用矩阵对象需要更多的工作——您必须学习矩阵对象操作和 ndarray 操作。

Writing a program that mixes both matrices and arrays makes your life difficult because you have to keep track of what type of object your variables are, lest multiplication return something you don't expect.

编写一个混合矩阵和数组的程序会让你的生活变得困难,因为你必须跟踪你的变量是什么类型的对象,以免乘法返回你不期望的东西。

In contrast, if you stick solely with ndarrays, then you can do everything matrix objects can do, and more, except with slightly different functions/notation.

相比之下,如果你只坚持使用 ndarrays,那么你可以做矩阵对象可以做的一切,甚至更多,除了稍微不同的函数/符号。

If you are willing to give up the visual appeal of NumPy matrix product notation (which can be achieved almost as elegantly with ndarrays in Python >= 3.5), then I think NumPy arrays are definitely the way to go.

如果您愿意放弃 NumPy 矩阵乘积符号的视觉吸引力(使用 Python >= 3.5 中的 ndarrays 几乎可以优雅地实现这一点),那么我认为 NumPy 数组绝对是要走的路。

PS. Of course, you really don't have to choose one at the expense of the other, since np.asmatrixand np.asarrayallow you to convert one to the other (as long as the array is 2-dimensional).

附注。当然,您真的不必以牺牲另一个为代价来选择一个,因为np.asmatrix并且np.asarray允许您将一个转换为另一个(只要数组是二维的)。



There is a synopsis of the differences between NumPy arraysvs NumPy matrixes here.

还有就是与NumPy之间的差异大纲arraysVS NumPy的matrixES这里

回答by Josef

Just to add one case to unutbu's list.

只是在 unutbu 的列表中添加一个案例。

One of the biggest practical differences for me of numpy ndarrays compared to numpy matrices or matrix languages like matlab, is that the dimension is not preserved in reduce operations. Matrices are always 2d, while the mean of an array, for example, has one dimension less.

与 numpy 矩阵或矩阵语言(如 matlab)相比,numpy ndarrays 的最大实际差异之一是在 reduce 操作中不保留维度。矩阵总是二维的,而数组的均值,例如,少一维。

For example demean rows of a matrix or array:

例如贬低矩阵或数组的行:

with matrix

带矩阵

>>> m = np.mat([[1,2],[2,3]])
>>> m
matrix([[1, 2],
        [2, 3]])
>>> mm = m.mean(1)
>>> mm
matrix([[ 1.5],
        [ 2.5]])
>>> mm.shape
(2, 1)
>>> m - mm
matrix([[-0.5,  0.5],
        [-0.5,  0.5]])

with array

带阵列

>>> a = np.array([[1,2],[2,3]])
>>> a
array([[1, 2],
       [2, 3]])
>>> am = a.mean(1)
>>> am.shape
(2,)
>>> am
array([ 1.5,  2.5])
>>> a - am #wrong
array([[-0.5, -0.5],
       [ 0.5,  0.5]])
>>> a - am[:, np.newaxis]  #right
array([[-0.5,  0.5],
       [-0.5,  0.5]])

I also think that mixing arrays and matrices gives rise to many "happy" debugging hours. However, scipy.sparse matrices are always matrices in terms of operators like multiplication.

我还认为混合数组和矩阵会带来许多“快乐”的调试时间。但是, scipy.sparse 矩阵始终是乘法等运算符的矩阵。

回答by atomh33ls

Scipy.org recommends that you use arrays:

Scipy.org 建议您使用数组:

*'array' or 'matrix'? Which should I use? - Short answer

Use arrays.

  • They are the standard vector/matrix/tensor type of numpy. Many numpy function return arrays, not matrices.

  • There is a clear distinction between element-wise operations and linear algebra operations.

  • You can have standard vectors or row/column vectors if you like.

The only disadvantage of using the array type is that you will have to use dotinstead of *to multiply (reduce) two tensors (scalar product, matrix vector multiplication etc.).

*“数组”还是“矩阵”?我应该使用哪个?- 简答

使用数组。

  • 它们是 numpy 的标准向量/矩阵/张量类型。许多 numpy 函数返回数组,而不是矩阵。

  • 逐元素运算和线性代数运算之间有明显的区别。

  • 如果您愿意,您可以使用标准向量或行/列向量。

使用数组类型的唯一缺点是您必须使用dot而不是*乘以(减少)两个张量(标量积、矩阵向量乘法等)。

回答by Peque

As others have mentioned, perhaps the main advantage of matrixwas that it provided a convenient notation for matrix multiplication.

正如其他人所提到的,也许 的主要优点matrix是它为矩阵乘法提供了方便的表示法。

However, in Python 3.5 there is finally a dedicated infix operator for matrix multiplication: @.

然而,在 Python 3.5 中,终于有一个专门用于矩阵乘法的中缀运算符: @

With recent NumPy versions, it can be used with ndarrays:

在最近的 NumPy 版本中,它可以与ndarrays一起使用:

A = numpy.ones((1, 3))
B = numpy.ones((3, 3))
A @ B

So nowadays, even more, when in doubt, you should stick to ndarray.

所以现在,更重要的是,当有疑问时,你应该坚持使用ndarray.