zoukankan      html  css  js  c++  java
  • numpy库的基本使用

    1.概述

    2.基本操作

    2.1  numpy.tile(A, reps)

    tile共有2个参数,A指待输入数组,reps则决定A重复的次数。整个函数用于重复数组A来构建新的数组。

    假设reps的维度为d,那么新数组的维度为max(d,A.ndim)。下面分三种情况进行讨论:

    (1)A.ndim < d

    则向A中添加新轴扩充A的维度。维度大小可以从shape中看出,一般通过向shape对应的元组中添加1完成对A维度的扩充。扩充完成后,则可根据reps的值对A中相应维度的值进行重复。

    例如,一维数组shape为(3,),扩充至2维则shape值为(1,3),扩充至3维则shape值为(1,1,3)

    (2)A.ndim > d

    将reps扩充至与A相同的维度。扩充方法同上,也是向shape对应元组中添1,然后再进行重复。

    例如,4维数组A的shape为(2,3,4,5),而reps为(2,2)只有2维,那么就要对reps添维进行扩充,得到(1,1,2,2)

    (3)A.ndim = d

     不需要扩充,直接按reps的值对相应维度的值进行重复。

    >>>from numpy import *
    >>> a = array([1,2,3])
    >>>print a.shape
    (3.)
    >>>print a.ndim
    1
    >>>b = tile(a,2)
    >>>print b
    [1 2 3 1 2 3]
    >>>print b.shape
    (6,)
    >>>print b.ndim
    1
    >>>c = tile(a,(2,3))
    >>>print c
    [[1 2 3 1 2 3 1 2 3]
     [1 2 3 1 2 3 1 2 3]]
    >>>print c.shape
    (2,9)
    >>>print c.ndim
    2
     

    由此可以看出,得到的新数组的维度由d和A.ndim的大小决定,shape值由扩充后的A和reps相应维度值的乘积得到。

    2.2 numpy.cov(data)

    求矩阵的协方差

    2.3   np.random.random(size = (5,10))

    创建随机数矩阵

  • 相关阅读:
    BEC listen and translation exercise 44
    中译英12
    BEC listen and translation exercise 43
    中译英11
    BEC listen and translation exercise 42
    中译英10
    BEC listen and translation exercise 41
    中译英9
    BEC listen and translation exercise 40
    中译英8
  • 原文地址:https://www.cnblogs.com/guo-zhi-wei/p/10482204.html
Copyright © 2011-2022 走看看