zoukankan      html  css  js  c++  java
  • numpy库

    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))
    >>> randMat
    matrix([[ 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----》矩阵*矩阵的逆=单位矩阵
    >>> myEye
    matrix([[ 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 operator
    
    print 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)
    4
    4
    (2, 4)
    2
    4

    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;
    欢迎关注我的公众号:小秋的博客 CSDN博客:https://blog.csdn.net/xiaoqiu_cr github:https://github.com/crr121 联系邮箱:rongchen633@gmail.com 有什么问题可以给我留言噢~
  • 相关阅读:
    标签切换JS代码
    use selenium in scrapy webdriver
    scrapy yield Request
    爱拼图游戏android源码完整版
    ios球体弹跳游戏源码
    android版猜拳游戏源码分享
    萝卜家园32位和64位Win7系统Ghost版
    Android开发教程大全介绍
    怎样可以让你抓住数字教育产品新机遇
    Android版年年有鱼游戏源码
  • 原文地址:https://www.cnblogs.com/flyingcr/p/10327084.html
Copyright © 2011-2022 走看看