numpy库

来源:互联网 发布:用在淘宝上的比价神器' 编辑:程序博客网 时间:2024/06/08 04:12

1、创建随机矩阵

>>> from numpy import *>>> random.rand(4,4)array([[ 0.1801566 ,  0.02580119,  0.02685281,  0.52768083],       [ 0.45411008,  0.7831068 ,  0.21375316,  0.5834276 ],       [ 0.14515285,  0.56107743,  0.70939684,  0.38059215],       [ 0.38190561,  0.90318702,  0.36228335,  0.41596983]])>>> random.rand(1,5)array([[ 0.44651467,  0.62156783,  0.26844721,  0.0429569 ,  0.94402983]])>>> mat(random.rand(2,2))-----》转换为矩阵matrix([[ 0.70740332,  0.74586087],        [ 0.63854925,  0.08342785]])>>> randMat = mat(random.rand(4,5))>>> randMatmatrix([[ 0.06601757,  0.01608014,  0.42190585,  0.01011915,  0.31645594],        [ 0.65403083,  0.59347221,  0.19135569,  0.46777104,  0.66128351],        [ 0.35348795,  0.82899244,  0.56450194,  0.90785287,  0.79258663],        [ 0.71413199,  0.62240897,  0.17560023,  0.51640713,  0.51107191]])>>> randMat.I-----》求矩阵的逆matrix([[ 0.99798378, -1.76149543, -1.19227532,  3.46560494],        [-0.85138719, -0.19618883,  0.49031565,  0.21072417],        [ 2.50529117, -4.01838827,  0.18836154,  3.3826735 ],        [-0.89262193, -1.69025782,  1.0541609 ,  0.95941787],        [-0.31650047,  5.78889385, -0.06102358, -5.27421759]])>>>>>> IrandMat = randMat.I>>> myEye = IrandMat * randMat----》矩阵*矩阵的逆=单位矩阵>>> myEyematrix([[ 0.96725654,  0.13928549,  0.01950231, -0.1066253 , -0.02283814],        [ 0.13928549,  0.40750154, -0.08295973,  0.45356717,  0.09714986],        [ 0.01950231, -0.08295973,  0.98838424,  0.06350702,  0.01360261],        [-0.1066253 ,  0.45356717,  0.06350702,  0.65278699, -0.07436979],        [-0.02283814,  0.09714986,  0.01360261, -0.07436979,  0.98407068]])>>> myEye - eye(4)Traceback (most recent call last):  File "<stdin>", line 1, in <module>ValueError: operands could not be broadcast together with shapes (5,5) (4,4)>>> eye(4)array([[ 1.,  0.,  0.,  0.],       [ 0.,  1.,  0.,  0.],       [ 0.,  0.,  1.,  0.],       [ 0.,  0.,  0.,  1.]])>>> myEye - eye(4)Traceback (most recent call last):  File "<stdin>", line 1, in <module>ValueError: operands could not be broadcast together with shapes (5,5) (4,4)>>> myEye - eye(5)-----》减去5*5的单位矩阵,计算误差matrix([[-0.03274346,  0.13928549,  0.01950231, -0.1066253 , -0.02283814],        [ 0.13928549, -0.59249846, -0.08295973,  0.45356717,  0.09714986],        [ 0.01950231, -0.08295973, -0.01161576,  0.06350702,  0.01360261],        [-0.1066253 ,  0.45356717,  0.06350702, -0.34721301, -0.07436979],        [-0.02283814,  0.09714986,  0.01360261, -0.07436979, -0.01592932]])

2、shape()—>针对矩阵

from numpy import *import operatorprint eye(4);print eye(4).shape;print eye(4).shape[0];#行数print eye(4).shape[1];#列数a = [[1,2,4,5],[2,4,5,5]];b = mat(a);print b.shape;print b.shape[0];print b.shape[1];[[ 1.  0.  0.  0.] [ 0.  1.  0.  0.] [ 0.  0.  1.  0.] [ 0.  0.  0.  1.]](4, 4)44(2, 4)24

3、矩阵的重复

c = [1,2];print tile(c,3);#列重复3次print tile(c,(2,3));#行重复2次 列重复3次

4、sum()

b = [[1,2,4,5],[2,4,5,5]];#b = mat(a);result = np.sum(b,axis=1);#一个矩阵的每一行向量相加,result2 = np.sum(b,axis=0);#矩阵的所有行向量相加,print result;print result2;
原创粉丝点击