zoukankan      html  css  js  c++  java
  • numpy的一些用法

    安装numpy

    windows安装pip即可,具体方法参考pip官网 http://pip-cn.readthedocs.io/en/latest/installing.html

    安装方法:pip install  numpy-1.14.3-cp27-none-win_amd64.whl

    功能介绍:

    • 提供数组的矢量化操作,所谓矢量化就是不用循环就能将运算符应用到数组中的每个元素中。
    • 提供数学函数应用到每个数组中元素
    • 提供线性代数,随机数生成,傅里叶变换等数学模块
    • ndarray:numpy库的心脏,多维数组,具有矢量运算能力,快速、节省空间在array中的数据类型是一致的

    ndarray:

      ndarray具有多维性。ndarray的元素可以通过索引的方式进行访问。在Numpy中,ndarray的维度称为axes。axes的大小称为rank。列如ndarray[1,2,1],它的维度为1,rank的值为1,因为只有一维。索引从0开始。

    print np.identity(3,int)  #单位矩阵

    结果:

    [[1 0 0]
     [0 1 0]
     [0 0 1]]

    零矩阵:

    print np.zeros((3,4)) #零矩阵
    print np.zeros(3)  #零矩阵

    结果:

    [[0. 0. 0. 0.]
     [0. 0. 0. 0.]
     [0. 0. 0. 0.]]
    [0. 0. 0.]

    全一矩阵:

    print np.ones((3,3))
    print np.ones(4)

    结果:

    [[1. 1. 1.]
     [1. 1. 1.]
     [1. 1. 1.]]
    [1. 1. 1. 1.]

    矩阵乘法:

    a=((1,2,3),(4,5,6),(7,8,9))
    a=np.array(a)
    print np.dot(2,a)

    结果:

    [[ 2  4  6]
     [ 8 10 12]
     [14 16 18]]

    矩阵大小:

    a=((1,2,3),(4,5,6),(7,8,9))
    a=np.array(a)
    print a.ndim

    结果:

    2

    行求和,列求和

    a=((1,2,3),(4,5,6),(7,8,9))
    a=np.array(a)
    print np.sum(a,axis=1)
    print np.sum(a,axis=0)
    #axis=1表示矩阵a的行求和,axis=0表示在列求和

    结果:

    [ 6 15 24]
    [12 15 18]

    转置矩阵:

    a=((1,2,3),(4,5,6),(7,8,9))
    a=np.array(a)
    print a.T

    结果:

    [[1 4 7]
     [2 5 8]
     [3 6 9]]

    其他的一些:

    a=((1,2,3),(4,5,6),(7,8,9))
    a=np.array(a)
    print np.random.random((3,3)) #random模块的random函数,生成随机数
    print np.mean(a)  #求平均数
    print np.max(a)  #求最大值
    print  np.min(a)
    print np.std(a)  #求标准差
    print np.arange(0,20,step=2)  #arange可以指定起点,终点,步长进行数组创建
    print np.linspace(0, 20, 10) #等同于下面的这个
    print np.linspace(start=0, stop=20, num=10)
    #直接指定开始,结束然后指定个数进行创建。
    print np.random.normal(10,100,size=10) #产生服从高斯分布的随机数,三个参数分别是平均值,方差,个数

    结果:

    [[0.18149469 0.82166642 0.89837593]
     [0.07947753 0.65715104 0.23933089]
     [0.34254456 0.19185617 0.17856812]]
    5.0
    9
    1
    2.581988897471611
    [ 0  2  4  6  8 10 12 14 16 18]
    [ 0.          2.22222222  4.44444444  6.66666667  8.88888889 11.11111111
     13.33333333 15.55555556 17.77777778 20.        ]
    [ 0.          2.22222222  4.44444444  6.66666667  8.88888889 11.11111111
     13.33333333 15.55555556 17.77777778 20.        ]
    [  36.25896713   75.73646837  -90.97435221  -87.55378736  192.75253223
      -59.32404814  256.30659631 -161.95343956    5.39389542  -62.17649294]
  • 相关阅读:
    如何使用idea创建一个java项目
    IntelliJ IDEA 下载安装配置教程
    使用cmd命令输出Hello word
    用js引入css,减少http请求次数,提高响应速度,
    mysql order by limit出现数据丢失问题
    被ASP.NET GridView checkbox选择逼疯的朋友们,请放下你手中的割腕刀
    分发计数器
    mysql 操作
    code site 例子
    kjj
  • 原文地址:https://www.cnblogs.com/catxjd/p/8985272.html
Copyright © 2011-2022 走看看