美文网首页
2018-05-25

2018-05-25

作者: 逃淘桃 | 来源:发表于2018-05-25 16:31 被阅读0次

python

1.python中数组和矩阵乘法及使用总结

  • 对数组的运算
import numpy as np
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 = np.array([[7, 8, 9],[4, 5, 6],[1, 2, 3]])
b = np.mat(b)
b
--->>
matrix([[7, 8, 9],
        [4, 5, 6],
        [1, 2, 3]])
a + b#矩阵的加减运算和数组运算一致,在对应位置相加减
--->>
matrix([[ 8, 10, 12],
        [ 8, 10, 12],
        [ 8, 10, 12]])
a * b#矩阵的乘用*即可表示
--->>
matrix([[ 18,  24,  30],
        [ 54,  69,  84],
        [ 90, 114, 138]])
b * a
--->>
matrix([[102, 126, 150],
        [ 66,  81,  96],
        [ 30,  36,  42]])
np.dot(b, a)# 使用dot与*的效果一样
--->>
matrix([[102, 126, 150],
        [ 66,  81,  96],
        [ 30,  36,  42]])
np.dot(a, b)
--->>
matrix([[ 18,  24,  30],
        [ 54,  69,  84],
        [ 90, 114, 138]])
c = np.array([1, 2, 3])#构建一个一行三列的数组
c
--->>
array([1, 2, 3])
a * c# 不符合矩阵运算规则
--->>
ValueError                                Traceback (most recent call last)
<ipython-input-19-fc01a05f2628> in <module>()
----> 1 a * c

/home/icrclsc/anaconda3/lib/python3.5/site-packages/numpy/matrixlib/defmatrix.py in __mul__(self, other)
    307         if isinstance(other, (N.ndarray, list, tuple)) :
    308             # This promotes 1-D vectors to row vectors
--> 309             return N.dot(self, asmatrix(other))
    310         if isscalar(other) or not hasattr(other, '__rmul__') :
    311             return N.dot(self, other)

ValueError: shapes (3,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)
c * a
--->>
matrix([[30, 36, 42]])
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
--->>
ValueError                                Traceback (most recent call last)
<ipython-input-25-608e57f1304c> in <module>()
----> 1 c * a

/home/icrclsc/anaconda3/lib/python3.5/site-packages/numpy/matrixlib/defmatrix.py in __rmul__(self, other)
    313 
    314     def __rmul__(self, other):
--> 315         return N.dot(other, self)
    316 
    317     def __imul__(self, other):

ValueError: shapes (3,1) and (3,3) not aligned: 1 (dim 1) != 3 (dim 0)
  • 矩阵求逆,转置,求迹
a.T# a 的转置
--->>
matrix([[1, 4, 7],
        [2, 5, 8],
        [3, 6, 9]])
a.H# a的共轭转置
--->>
matrix([[1, 4, 7],
        [2, 5, 8],
        [3, 6, 9]])
b = np.eye(3)
b
--->>
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
b = b * 3
b
--->>
array([[3., 0., 0.],
       [0., 3., 0.],
       [0., 0., 3.]])
b = np.mat(b)
b
matrix([[3., 0., 0.],
        [0., 3., 0.],
        [0., 0., 3.]])
b.I
matrix([[0.33333333, 0.        , 0.        ],
        [0.        , 0.33333333, 0.        ],
        [0.        , 0.        , 0.33333333]])
np.trace(b)
9.0

相关文章

网友评论

      本文标题:2018-05-25

      本文链接:https://www.haomeiwen.com/subject/jrcujftx.html