标签:pow sum cpm 1.5 sub log 运算 生成 规则
1.矩阵的初始化
(1)创建一个 3*5的全0矩阵和全1矩阵
import numpy as np myzero = np.zeros([3,5]) print myzero myones = np.ones([3,5]) print myones
(2)生成随机矩阵
myrand = np.random.rand(3,4) print myrand
(3)单位阵
myeye = np.eye(3) print myeye
2. 矩阵的元素运算
矩阵的元素运算是指矩阵在元素级别的加、减、乘、除运算。
from numpy import * # 导入 NumPy包
(1)元素的加和相减
条件:矩阵的行数和列数必须相同。
数学公式:(A±B)i?j = Ai,j ± Bi,j 。
myones = ones([3,3]) myeye = eye(3) print myones print myones-myeye
(2)矩阵数乘:一个数乘一个矩阵
数学公式:(cA)i,j = c * Ai,j。
mymatrix = mat([1,2,3],[4,5,6],[7,8,9]) a = 10 print a * mymatrix
(3)矩阵的所有元素求和:
数学公式: 打不出来
mymatrix = mat([1,2,3],[4,5,6],[7,8,9]) print sum(mymatrix)
(4)矩阵各元素的积:矩阵的店乘同维对应元素的相乘。当矩阵的维度不同时,会根据一定的广播规则将维度扩充到一致的形式。
mymatrix = mat([1,2,3],[4,5,6],[7,8,9]) mymatrix2 = 1.5*ones([3,3]) print multiply(mymatrix,mymatrix2)
(5)矩阵各元素的n次幂: n = 2
mylist = mat([[1,2,3],[4,5,6],[7,8,9]]) print power(mylist,2)
3、矩阵的乘法:矩阵乘矩阵
from numpy import * mymatrix = mat([[1,2,3],[4,5,6],[7,8,9]]) mymatrix2 = mat([[1],[2],[3]]) print mymatrix*mymatrix2
4、矩阵的转置
from numpy import * mymatrix =- mat([[1,2,3],[4,5,6],[7,8,9]]) print mymatrix.T mymatrix.transpose() print mymatrix
5、矩阵的其他操作:行列数、切片、复制、比较
from numpy import * mymatix = mat([[1,2,3],[4,5,6],[7,8,9]]) [m,n] = shape(mymatrix) print "矩阵的行数和列数:", m , n myscl1 = mymatrix[0] print "按行切片:" , myscl1 myscl2 = muymatrix.T[0] print "按列切片:" , myscl2 mycpmat = mymatrix.copy() print "复制矩阵:\n " , mycpmat print "矩阵元素的比较:\n",mymatrix < mymatrix.T
标签:pow sum cpm 1.5 sub log 运算 生成 规则
原文地址:http://www.cnblogs.com/jackchen001/p/6624800.html