它们各自的优点和缺点是什么?

据我所知,如果需要的话,任何一种都可以作为另一种的替代品,所以我是应该同时使用两种呢,还是应该坚持使用其中一种呢?

程序的风格会影响我的选择吗?我正在使用numpy做一些机器学习,所以确实有很多矩阵,但也有很多向量(数组)。


当前回答

Scipy.org建议使用数组:

*'array' or 'matrix'? Which should I use? - Short answer Use arrays. They support multidimensional array algebra that is supported in MATLAB They are the standard vector/matrix/tensor type of NumPy. Many NumPy functions 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. Until Python 3.5 the only disadvantage of using the array type was that you had to use dot instead of * to multiply (reduce) two tensors (scalar product, matrix vector multiplication etc.). Since Python 3.5 you can use the matrix multiplication @ operator. Given the above, we intend to deprecate matrix eventually.

其他回答

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

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

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

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

所以现在,当你有疑问的时候,你应该坚持使用ndarray。

Scipy.org建议使用数组:

*'array' or 'matrix'? Which should I use? - Short answer Use arrays. They support multidimensional array algebra that is supported in MATLAB They are the standard vector/matrix/tensor type of NumPy. Many NumPy functions 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. Until Python 3.5 the only disadvantage of using the array type was that you had to use dot instead of * to multiply (reduce) two tensors (scalar product, matrix vector multiplication etc.). Since Python 3.5 you can use the matrix multiplication @ operator. Given the above, we intend to deprecate matrix eventually.

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

对我来说,numpy ndarray与numpy矩阵或像matlab这样的矩阵语言相比,最大的实际区别之一是在约简操作中不保留维数。矩阵总是二维的,而数组的均值,例如,有一个维度少。

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

与矩阵

>>> 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]])

与数组

>>> 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]])

我还认为混合使用数组和矩阵会带来很多“愉快的”调试时间。 然而,scipy。稀疏矩阵总是矩阵的运算符,比如乘法。

根据官方文件,不再建议使用矩阵类,因为它将在未来被删除。

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

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

Numpy数组的矩阵运算:

我愿意不断更新这个答案 关于numpy数组的矩阵运算,如果一些用户有兴趣寻找关于矩阵和numpy的信息。

作为公认的答案,numpy-ref.pdf说:

类numpy。矩阵将在未来被删除。

现在需要做矩阵代数运算 使用Numpy数组。

a = np.array([[1,3],[-2,4]])
b = np.array([[3,-2],[5,6]]) 

矩阵乘法(中缀矩阵乘法)

a@b
array([[18, 16],
       [14, 28]])

置:

ab = a@b
ab.T       
array([[18, 14],
       [16, 28]])

  

矩阵的逆:

np.linalg.inv(ab)
array([[ 0.1       , -0.05714286],
       [-0.05      ,  0.06428571]])      

ab_i=np.linalg.inv(ab) 
ab@ab_i  # proof of inverse
array([[1., 0.],
       [0., 1.]]) # identity matrix 

矩阵的行列式。

np.linalg.det(ab)
279.9999999999999

求解线性方程组:

1.   x + y = 3,
    x + 2y = -8
b = np.array([3,-8])
a = np.array([[1,1], [1,2]])
x = np.linalg.solve(a,b)
x
array([ 14., -11.])
# Solution x=14, y=-11

特征值和特征向量:

a = np.array([[10,-18], [6,-11]])
np.linalg.eig(a)
(array([ 1., -2.]), array([[0.89442719, 0.83205029],
        [0.4472136 , 0.5547002 ]])