numpy中的matrix与array的区别(Matlab矩阵运算对比)

1. 定义(维基定义)

数组

数组(英语:Array),是由相同类型的元素(element)的集合所组成的资料结构,分配一块连续的内存来存储。利用元素的索引(index)可以计算出该元素对应的储存地址。

没有规定数组维度-->可以是任意的,eg:一维,二维,多维etc
矩阵

数学上,一个m×n的矩阵是一个由m行n列元素排列成的矩形阵列。

规定了矩阵维度:m x n--->二维的
numpy定义
numpy.ndarray

An array object represents a multidimensional, homogeneous array of fixed-size items. An associated data-type object describes the format of each element in the array .
一个表示多维,元素类型相同的确定维度的数组对象。有一个描述数组中元素类型的数据类型对象。

numpy.matrix

Returns a matrix from an array-like object, or from a string of data. A matrix is a specialized 2-D array that retains its 2-D nature through operations. It has certain special operators, such as * (matrix multiplication) and ** (matrix power).
矩阵是一个特定的2维的数组对象。有特定的操作,如:乘法,*矩阵乘方

2. 乘法运算类型及其定义

运算符号*
np.multiply(a,b)
np.dot(a, b)
np.matmul(a, b)

运算符号*
符号乘法,运算符重载;
(1) a,b 为1-D数组;逐元素相乘element-wise*

a=np.arange(4)#a=[0,1,2,3]
b=np.arange(4)+1 #b=[1,2,3,4]
c=a*b # c= [0,2,6,12]

(2)a,b为2-D数组;element-wise运算

a=np.arange(6).reshape(2,3)#a=[[0,1,2],[3,4,5]]
b=np.arange(6).reshape(3,2)#b=[[0,1],[2,3],[4,5]]
c=a*b#wrong 运行出错;不能进行广播
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (2,3) (3,2)

(3)a,b 为2-D矩阵;矩阵运算

a=np.mat(np.arange(6).reshape(2,3))#a=[[0,1,2],[3,4,5]]
b=np.mat((np.arage(6)+1).reshape(3,2))#b=[[0,1],[2,3],[4,5]]
c=a*b #c=[[13, 16],[40, 52]]

运算*:
如果输入为ndarray类型,进行逐元素乘法运算;维度不等时,进行广播;
如果输入为matrix类型,进行矩阵乘法运算。

np.multiply(a, b)
np.multiply(a, b)
Multiply arguments element-wise.逐元素相乘
参数:a,b: 数组类型
输出:ndarray 数组;a,b 逐元素相乘的结果;如果a和b都是标量,返回一个标量。

(1)标量:一个数,eg:6; 6.0; 666

a=1; b=2
c = np.multiply(a,b)
#c = 2 输出结果c为2

(2)a,b 1-D数组:

a=np.arange(4)#a=[0,1,2,3]
b=np.arange(4)+1 #b=[1,2,3,4]
c=np.multiply(a,b)#c=[0,2,6,12]

(3)a,b 2-D 数组:能广播,逐元素相乘;不能广播,报错。

a=np.arange(6).reshape(2,3)#a=[[0,1,2],[3,4,5]]
b=(np.arage(6)+1).reshape(3,2)#b=[[0,1],[2,3],[4,5]]
c=np.multiply(a,b)#wrong
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (2,3) (3,2)

(4)a,b 矩阵:报错;本质上,进行逐元素element-wise运算

a=np.mat(np.arange(6).reshape(2,3))#a=[[0,1,2],[3,4,5]]
b=np.mat((np.arage(6)+1).reshape(3,2))#b=[[0,1],[2,3],[4,5]]
c=np.multiply(a,b)#wrong
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (2,3) (3,2)

可以进行广播运算,正常运行(element-wise)

a=np.mat(np.arange(6).reshape(2,3))#a=[[0,1,2],[3,4,5]]
b=np.mat((np.arage(3)+1))#b=[[1,2,3]]
c=np.multiply(a,b)#c=matrix([[0,2,6],[3,8,15]])

np.dot(a,b)
np.dot(a,b)
Dot product of two arrays.数组a和数组b的点积。
点积是两个向量上的函数并返回一个标量的二元运算;是内积的一种特殊形式。
根据数组a和数组b的维度不同,运算过程也不相同:

(1)a, b为一维数组:返回a和b的点积(就是一个数):

a=np.arange(2) #a=[0,1]
b=np.arange(2)+2 #b=[2,3]
c=np.dot(a,b) #c=3 ;just a number

(2)a, b为二维数组:a和b做矩阵乘法运算:

a=np.arrange(6).reshape(2,3) #a=[[0,1,2],[3,4,5]]
b=np.arange(6).reshape(3,2) #b=[[0,1],[2,3],[4,5]]
c=np.dot(a,b) # c= [[10, 13], [28,40]]

仍然可以看做是点积运算,矩阵a中的行向量和矩阵b中列向量乘积,得到一个数,记录下来(这个过程是点积运算,10=00+12+2*4)。

(3)a,b 为矩阵形式:和二维数组相同,进行矩阵运算。

a=np.mat(np.arrange(6).reshape(2,3)) #a=[[0,1,2],[3,4,5]]
b=np.mat(np.arange(6).reshape(3,2) )#b=[[0,1],[2,3],[4,5]]
c=np.dot(a,b) # c= [[10, 13], [28,40]]

np.matmul(a, b)
np.matmul(a, b)
两个数组的矩阵积。
运算根据参数的维度大小,有不同的计算方法:

参数a,b都为二维矩阵2-D,进行矩阵运算;
第一个参数为1维1-D,用1填充参数a,然后进行矩阵运算,之后,删除添加的1;
第二个参数为1维1-D,用1填充参数b,进行矩阵运算,之后,删除添加的1;
如果参数a,b的维度大于2,将其视为存在于最后两个索引的矩阵的栈,并进行相应广播;

(1)a,b为2-D数组:

a=np.arrange(6).reshape(2,3) #a=[[0,1,2],[3,4,5]]
b=np.arange(6).reshape(3,2) #b=[[0,1],[2,3],[4,5]]
c=np.matmul(a,b) # c= [[10,13],[28,40]]

(2)a,b为matrix类型;和2-D数组相同

a=np.mat(np.arrange(6).reshape(2,3)) #a=[[0,1,2],[3,4,5]]
b=np.mat(np.arange(6).reshape(3,2))#b=[[0,1],[2,3],[4,5]]
c=np.matmul(a,b) # c= [[10,13],[28,40]]

小结
在这里插入图片描述

array只能通过dot()实现“矩阵乘法”,array的“*”运算实现的是两个纬度相同的“矩阵”的按位相乘。

而matrix则不同,可以直接使用"*"运算符实现“矩阵乘法”

在这里插入图片描述

注意,我们在数据处理中使用较多的是array。

import numpy as np
a = np.random.rand(3,4)
print('a shape: {}; a.T shape: {}'.format(np.shape(a),np.shape(a.T)) )
print('a and a.T 相乘: \n',np.dot(a, a.T)) # 进行矩阵乘法
b = np.mat(a) # 将 a 的array形式转换为 matrix
print('b.I: \n', b.I) # 逆矩阵
print('b*b.T: \n', b*b.T)

结果为:

a shape: (3, 4); a.T shape: (4, 3)
a and a.T 相乘: 
 [[1.06781647 0.30486271 0.45179314]
 [0.30486271 0.67382283 0.49058274]
 [0.45179314 0.49058274 0.79796573]]
b.I: 
 [[ 0.68774543  0.69230876 -0.80024159]
 [-0.17433347 -0.82191199  1.37088823]
 [ 0.80109553 -0.45486199  0.24199063]
 [-0.29892761  1.15387317  0.16119934]]
b*b.T: 
 [[1.06781647 0.30486271 0.45179314]
 [0.30486271 0.67382283 0.49058274]
 [0.45179314 0.49058274 0.79796573]]

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

在numpy中matrix的主要优势是:相对简单的乘法运算符号。例如,a和b是两个matrices,那么a*b,就是矩阵积。而不用np.dot()。

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 都可以通过objects后面加.T 得到其转置。但是 matrix objects 还可以在后面加 .H 得到共轭矩阵, 加 .I 得到逆矩阵。

相反的是在numpy里面arrays遵从逐个元素的运算,所以对于array:c 和d的c*d运算相当于matlab里面的c.*d运算

c=np.array([[4, 3], [2, 1]])
d=np.array([[1, 2], [3, 4]])
print(c*d)
# [[4 6]
#  [6 4]]
#而矩阵相乘,则需要numpy里面的dot命令 :
print(np.dot(c,d))
# [[13 20]
#  [ 5  8]]

但是python中矩阵没有MATLAB中的.*这个性质,对于matrix对应乘法得用np.multiply()

>>> np.multiply(a,b)
matrix([[ 4,  6],
                [6,  4]])   

当然 ** 运算符的作用也不一样 :

print(a**2)  #矩阵乘法
# [[22 15]
#  [10  7]]
print(c**2)  #对应乘法
# [[16  9]
#  [ 4  1]]

问题就出来了,如果一个程序里面既有matrix 又有array,会让人脑袋大。但是如果只用array,你不仅可以实现matrix所有的功能,还减少了编程和阅读的麻烦。

当然你可以通过下面的两条命令轻松的实现两者之间的转换:np.asmatrix和np.asarray

对我来说,numpy 中的array与numpy中的matrix,matlab中的matrix的最大的不同是,在做归约运算时,array的维数会发生变化,但matrix总是保持为2维。例如下面求平均值的运算

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

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

参考博客:

https://www.cnblogs.com/cymwill/p/7823148.html

https://blog.csdn.net/huahuazhu/article/details/86594444

https://www.jianshu.com/p/428c3f0a6f35

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何为xl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值