2018-05-25

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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 算法导论,线性时间,最大子数组和。这个思想,必须先要理解清楚,而后才能 写代码 。参考资料,感谢作者。https:...
    木马音响积木阅读 412评论 3 1
  • 说些关于偏振的事 偏振的分类 完全偏振可以使用X和Y方向的两个震动表示,通常的形式有椭圆偏振,圆偏振,线偏振。使用...
    易碌物阅读 3,296评论 0 0
  • 我的学生时代,跟大多数同学一样,拥有大把的时间,他们大多把时间花在玩,逛街,谈恋爱上,我把时间花在学习上。学习啥呢...
    古尚寒风阅读 355评论 0 0
  • 夏夜并不算寂静,躺在被蚊帐包围的床上,宋稳稳听着宿舍楼外的生物的响声。对,她就是这么无聊,不是学生会的,不是班委中...
    笛安渔阅读 211评论 0 0
  • 一、字体与兼容性 1. TureTpe(.ttf)格式:.ttf字体是Windows和Mac的最常见的字体,是一种...
    妙言Lisa阅读 3,907评论 0 2