python中array和matrix的区别

二者的区别主要在于在做乘法运算的时候,一个是矩阵乘,一个是数组乘,这里和MATLAb很相似。
调用的时候需要用numpy.array


Numpy matrices必须是2维的,但是numpy arrays (ndarrays) 可以是多维的(1D,2D,3D····ND). Matrix是Array的一个小的分支,包含于Array。所以matrix 拥有array的所有特性。

在numpy中matrix的主要优势是:相对简单的乘法运算符号。例如,a和b是两个matrices,那么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]]

matrix 和 array 都可以通过在have.Tto return the transpose, but matrix objects also have.Hfor the conjugate transpose, and.Ifor the inverse.

In contrast, numpy arrays consistently abide by the rule that operations are applied element-wise. Thus, if a and b are numpy arrays, then a*b is the array formed by multiplying the components element-wise:

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 :

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

Sinceais a matrix,a**2returns the matrix producta*a. Sincecis an ndarray,c**2returns an ndarray with each component squared element-wise.

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

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.

Writing a program that uses 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.

If you are willing to give up the visual appeal of numpy matrix product notation, then I think numpy arrays are definitely the way to go.

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

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.

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.

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值