一、数学基础
二、numpy
一、数学基础
关键字:求导、偏导、梯度、复合函数求导链式法则
矩阵的转置、矩阵加减、矩阵乘法【矩阵乘法不满足交换律】
二、numpy
(1)转置和加、乘
#coding:utf-8import numpy as npprint np.__version__# 1- create narrayarray = np.array([1,2,3],dtype=np.uint8)print "array:",array# 2- zerosmat1 = np.zeros((2,3)) #(2,3) tupleprint "mat1:",mat1# 3- 高维矩阵mat2 = np.zeros((1,2,3,4))print mat2.shapeprint mat2.size# 4- 标准矩阵运算# (1) 标量与矩阵相乘scalar = 2mat = np.ones((2,3))mat3 = scalar * matprint "mat3:",mat3# (2) 矩阵转置 mat.Tmat = np.zeros((2,3))tmat = mat.Tprint mat.shape,tmat.shapemat4 = np.array((1,2,3))print "mat4:",mat4tmat4 = mat4.Tprint mat4.shape,tmat4.shape# (3) 矩阵的加法print "add--------------------"mat1 = np.array([[1,2],[3,4]])mat2 = np.zeros((2,2))mat3 = mat1 + mat2print "mat3:",mat3# (4) 矩阵的乘法print "multi------------------"mat1 = np.array([[1,2],[3,4]])mat2 = np.ones((2,2))mat3 = mat1.dot(mat2)print "mat3:",mat3
(2)扩展运算
#coding:utf-8import numpy as np# 扩展运算# (1) 对应元素相乘mat1 = np.array([[1,2],[3,4]])mat = mat1 * mat1print "mat:",mat# (2) 标量与矩阵相加scalar = 2mat1 = np.array([[1,2],[3,4]])mat = scalar + mat1print "mat:",mat# (3) 高位矩阵维度改变mat1 = np.zeros((1,2,3))mat = mat1.transpose(0,2,1) # 维度调换print mat1.shape,mat.shape# (4) broad cast 拓宽操作。先对原来的向量拓宽,再做加法mat1 = np.ones((3,4))vec = np.array([[1],[2],[3]])print mat1+vec
(2)杂项运算
#coding:utf-8import numpy as np# 杂项操作# (1) 生成随机数rannum = np.random.randn(2,3)print "rannum:",rannum# 生成一个指定尺寸的矩阵# 矩阵中的所有数字符合正态分布(normal distribution)met = [1,2,3]np.random.shuffle(met)print "shuffle_met:",met# (2) 对矩阵元素求和print np.sum(rannum)# (3) numpy axisprint np.sum(rannum,axis = 0) # 列print np.sum(rannum,axis = 1) # 行# (4) e的指数print np.exp(rannum)# (5) 求最大下标a=[3,1,2,3,545,23,23,243]print np.argmax(a)