python中数组和矩阵乘法及使用总结(推荐)

yipeiwu_com5年前Python基础

Matrix是Array的一个小的分支,包含于Array。所以matrix 拥有array的所有特性。

但在数组乘和矩阵乘时,两者各有不同,如果a和b是两个matrices,那么a*b,就是矩阵积

如果a,b是数组的话,则a*b是数组的运算

1.对数组的操作

>>> import numpy as np
>>> a=np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> a
array([[1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]])
>>> b=a.copy()
>>> b
array([[1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]])
>>> a+b#多维数组的加减,按对应位置操作
array([[ 2, 4, 6],
    [ 8, 10, 12],
    [14, 16, 18]])
>>> a*3#多维数组乘常数,则对数组中每一个元素乘该常数
array([[ 3, 6, 9],
    [12, 15, 18],
    [21, 24, 27]])
>>> np.dot(a,b)#数组的点乘运算通过np.dot(a,b)来实现,相当于矩阵乘
array([[ 30, 36, 42],
    [ 66, 81, 96],
    [102, 126, 150]])
>>> c=np.array([1,2,3])#构造一行三列的数组
>>> c
array([1, 2, 3])
>>> c*a#c为一行三列,放于数组a之前,则对数组a中每行对应位置相乘
array([[ 1, 4, 9],
    [ 4, 10, 18],
    [ 7, 16, 27]])
>>> a*c#c为一行三列,放于数组a之后,依旧是对数组a中每行对应位置相乘
array([[ 1, 4, 9],
    [ 4, 10, 18],
    [ 7, 16, 27]])
>>> #如果想要矩阵运算,则需要np.dot()函数
>>> np.dot(c,a)#c为一行三列,放于数组a之前,按正常矩阵方式运算
array([30, 36, 42])
>>> np.dot(a,c)#c为一行三列,放于数组a之后,相当于矩阵a乘以3行一列的c矩阵,返回结果值不变,格式为1行3列
array([14, 32, 50])
>>> #将c改为多行一列的形式
>>> d=c.reshape(3,1)
>>> d
array([[1],
    [2],
    [3]])
>>> #
>>> np.dot(a,d)#值与np.dot(a,c)一致,但格式以改变为3行1列
array([[14],
    [32],
    [50]])
 
>>> a*a#数组使用*的运算其结果属于数组运算,对应位置元素之间的运算
array([[ 1, 4, 9],
    [16, 25, 36],
    [49, 64, 81]])
>>> #但是不能更改a,d点乘的位置,不符合矩阵运算格式
>>> np.dot(d,a)
Traceback (most recent call last):
 File "<pyshell#28>", line 1, in <module>
  np.dot(d,a)
ValueError: shapes (3,1) and (3,3) not aligned: 1 (dim 1) != 3 (dim 0)

对于数组的转置,求逆,求迹运算请参考上篇文章

2.对矩阵的操作

>>> a=np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> a=np.mat(a)
>>> a
matrix([[1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]])
>>> b=a
>>> b
matrix([[1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]])
>>> a+b#矩阵的加减运算和数组运算一致
matrix([[ 2, 4, 6],
    [ 8, 10, 12],
    [14, 16, 18]])
>>> a-b
matrix([[0, 0, 0],
    [0, 0, 0],
    [0, 0, 0]])
>>> a*b#矩阵的乘用*即可表示
matrix([[ 30, 36, 42],
    [ 66, 81, 96],
    [102, 126, 150]])
>>> np.dot(a,b)#与*一致
matrix([[ 30, 36, 42],
    [ 66, 81, 96],
    [102, 126, 150]])
>>> b*a
matrix([[ 30, 36, 42],
    [ 66, 81, 96],
    [102, 126, 150]])
>>> np.dot(b,a)
matrix([[ 30, 36, 42],
    [ 66, 81, 96],
    [102, 126, 150]])
>>> c=np.array([1,2,3])#构造一行三列数组
>>> c
array([1, 2, 3])
>>> c*a#矩阵运算
matrix([[30, 36, 42]])
>>> a*c#不合矩阵规则
Traceback (most recent call last):
 File "<pyshell#63>", line 1, in <module>
  a*c
 File "F:\python3\anzhuang\lib\site-packages\numpy\matrixlib\defmatrix.py", line 309, in __mul__
  return N.dot(self, asmatrix(other))
ValueError: shapes (3,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)
>>> np.dot(c,a)#和矩阵运算一致
matrix([[30, 36, 42]])
>>> np.dot(a,c)#自动将a转换成3行1列参与运算,返回结果格式已经变为1行3列而非3行一列的矩阵
matrix([[14, 32, 50]])
>>> c=c.reshape(3,1)
>>> c
array([[1],
    [2],
    [3]])
>>> a*c#和矩阵运算一致
matrix([[14],
    [32],
    [50]])
>>> c*a#不合矩阵运算格式
Traceback (most recent call last):
 File "<pyshell#71>", line 1, in <module>
  c*a 
ValueError: shapes (3,1) and (3,3) not aligned: 1 (dim 1) != 3 (dim 0)

矩阵运算的另一个好处就是方便于求转置,求逆,求迹

>>> a
matrix([[1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]])
>>> a.T
matrix([[1, 4, 7],
    [2, 5, 8],
    [3, 6, 9]])
>>> a.H#共轭转置
matrix([[1, 4, 7],
    [2, 5, 8],
    [3, 6, 9]])
>>> b=np.eye(3)*3
>>> b
array([[3., 0., 0.],
    [0., 3., 0.],
    [0., 0., 3.]])
>>> b=np.mat(b)
>>> b.I#求逆运算
matrix([[0.33333333, 0.    , 0.    ],
    [0.    , 0.33333333, 0.    ],
    [0.    , 0.    , 0.33333333]])
>>> np.trace(b)#求迹运算
9.0

以上所述是小编给大家介绍的python中数组和矩阵乘法及使用总结详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

基于python if 判断选择结构的实例详解

代码执行结构为顺序结构、选择结构、循环结构。 python判断选择结构【if】 if 判断条件 #进行判断条件满足之后执行下方语句 执行语句 elif 判断条件 #在不满足上面所有...

使用python实现男神女神颜值打分系统(推荐)

使用python实现男神女神颜值打分系统(推荐)

先给大家展示效果图,感觉不错,请参考实现代码。 具体代码如下所示: #!/usr/bin/env python # -*- coding:utf-8 -*- """ pip in...

python二叉树遍历的实现方法

复制代码 代码如下:#!/usr/bin/python# -*- coding: utf-8 -*- class TreeNode(object):   ...

使用rst2pdf实现将sphinx生成PDF

使用rst2pdf实现将sphinx生成PDF

当初项目文档是用sphinx写的,一套rst下来make html得到一整个漂亮的在线文档。现在想要将文档导出为离线的handbook pdf,于是找到了rst2pdf这个项目,作为sp...

pyqt5的QComboBox 使用模板的具体方法

pyqt5的QComboBox 使用模板的具体方法

QComboBox 的常规使用方法,在这个使用模板里,基本都有了。 QComboBox小部件是一个组合的按钮和弹出列表。 QComboBox提供了一种向用户呈现选项列表的方式,其占用最小...