Python 之NumPy

发布时间:2019-08-13 07:40:02编辑:auto阅读(1953)

    NumPy的主要对象是同质的多维数组。它是一个有明确索引的相同类型的元素组成的表。在NumPy中维度称之为轴,轴数称之为列。

    举个例子:

    例一:

    [ 1, 2, 1 ]

    这是一个一维数组,因为它只有一个轴,这个轴的长度是3.

    列二:

    [[ 1., 0., 0.],[ 0., 1., 2.]]

    这是一个二维数组,第一个维度的长度是2,第二个维度的长度是3.

    NumPy中的array类被称之为ndarray,但是他的别名array更有名。特别需要注意的是NumPy.array和Python 标准库里的arry.array不一样。array.array只提供了比较少的方法。而NumPy中array包含以下重要属性:

    ndarray.ndim

        数组的维度数量

        返回类型: number

    ndarray.shape

        数组的各个维度,这是一个存储了数组各个维度大小的int类型元祖。是一个n行m列矩阵,shape(n,m),这个shape的长度就是数组的维度数量。(如(2,3)表示2x3)

        返回类型:tuple

    ndarray.size

        数组中所有元素的数量

       返回类型:number

    ndarray.dtype

        数组中元素的类型。你可以使用python的types来创建和指定dtype’s,除此之外,numpy有自己的types,如:float64

    返回类型:dtype对象

    ndarray.itemsize

        数组中每个元素的字节数。例如:float64类型的数组元素的itemize是8(64/8)

    ndarray.data

        不常用,访问数组元素使用索引更便利



    创建数组

    #使用array方法创建数组,array的参数可以是python的list和tuple
    >>> import numpy as np
    >>> a = np.array([2,3,4])
    >>> a
    array([2, 3, 4])
    >>> a.dtype
    dtype('int64')
    >>> b = np.array([1.2, 3.5, 5.1])
    >>> b.dtype
    dtype('float64')
    #array可以将序列中的序列转化为2维数组
    >>> b = np.array([(1.5,2,3), (4,5,6)])
    >>> b
    array([[ 1.5,  2. ,  3. ],       [ 4. ,  5. ,  6. ]])
    数组的类型可以在创建时被显示的指定
    >>> c = np.array( [ [1,2], [3,4] ], dtype=complex )
    >>> c
    array([[ 1.+0.j,  2.+0.j],       [ 3.+0.j,  4.+0.j]])

    在创建数组的时候通常不知道数组的数据,但是知道数组的大小。所以numpy提供了几种方式类初始化数组内容。

    zeros创建一个所有元素都是0的数组

    >>> np.zeros( (3,4) )
    array([[ 0.,  0.,  0.,  0.],       
        [ 0.,  0.,  0.,  0.],       
        [ 0.,  0.,  0.,  0.]])

    ones创建一个所有元素都是1的数组

    >>> np.ones( (2,3,4), dtype=np.int16 )  # dtype can also be specified
    array([[[ 1, 1, 1, 1],
            [ 1, 1, 1, 1],
            [ 1, 1, 1, 1]],
            [[ 1, 1, 1, 1],
            [ 1, 1, 1, 1],        
            [ 1, 1, 1, 1]]], dtype=int16)

    empty的内容使用随机数填充

    >>> np.empty( (2,3) )          # uninitialized, output may vary
    array([[  3.73603959e-262,   6.02658058e-154,   6.55490914e-260],      
     [  5.30498948e-313,   3.14673309e-307,   1.00000000e+000]])

    连续的数字使用range(start, end, step)生成数组

    >>> np.arange( 10, 30, 5 )
    array([10, 15, 20, 25])
    >>> np.arange( 0, 2, 0.3 )      # it accepts float arguments
    array([ 0. ,  0.3,  0.6,  0.9,  1.2,  1.5,  1.8])

    由于浮点型涉及到字符精度,所以不拿呢个使用range,而是使用linspace替代

    >>> from numpy import pi
    >>> np.linspace( 0, 2, 9 )      # 9 numbers from 0 to 2
    array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ,  1.25,  1.5 ,  1.75,  2.  ])
    >>> x = np.linspace( 0, 2*pi, 100 )    # useful to evaluate function at lots of points
    >>> f = np.sin(x)

    reshape输出队列的格式

    >>> a = np.arange(6)                 # 默认是一维数组
    array>>> print(a)[0 1 2 3 4 5]
    >>> b = np.arange(12).reshape(4,3)           # 2d array四行三列的二维数组
    >>> print(b)
    [[ 0  1  2] 
    [ 3  4  5] 
    [ 6  7  8] 
    [ 9 10 11]]

    下面是数组的基本操作

    >>> a = np.array( [20,30,40,50] )
    >>> b = np.arange( 4 )
    >>> b #输b的内容,便于观察c的结果
    array([0, 1, 2, 3])
    >>> c = a-b #数组相减
    >>> c
    array([20, 29, 38, 47])
    >>> b**2   #b的二次幂
    array([0, 1, 4, 9])
    >>> 10*np.sin(a) #数组a中每个值的正玄值的10倍
    array([ 9.12945251, -9.88031624,  7.4511316 , -2.62374854])
    >>> a<35 #数组a中的数值是否比35小
    array([ True, True, False, False], dtype=bool)

    特别需要注意的是数组的乘法和数组的矩阵计算方式不同。* 的计算方式是对应位置的数字相乘。而矩阵的算法,详见链接

    >>> A = np.array( [[1,1]
    ,...             [0,1]] )
    >>> B = np.array( [[2,0],
    ...             [3,4]] )
    >>> A*B                         # elementwise product
    array([[2, 0],       [0, 4]])
    >>> A.dot(B)                    # matrix product
    array([[5, 4],       [3, 4]])
    >>> np.dot(A, B)                # another matrix product
    array([[5, 4],       [3, 4]])

    使用+=,*=来修改当前的数组,比创建数组更节省内存

    >>> a = np.ones((2,3), dtype=int)
    >>> b = np.random.random((2,3))
    >>> a *= 3
    >>> aarray([[3, 3, 3],       [3, 3, 3]])
    >>> b += a
    >>> b
    array([[ 3.417022  ,  3.72032449,  3.00011437],       
    [ 3.30233257,  3.14675589,  3.09233859]])
    >>> a += b  #b无法自动转换成int类型
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: Cannot cast ufunc add output from dtype('float64') to dtype('int16') with casting rule 'same_kind'

    数据类型不同的俩个或多个数组进行操作,结果会向上趋于精度更高的一方

    >>> a = np.ones(3, dtype=np.int32)
    >>> b = np.linspace(0,pi,3)
    >>> b.dtype.name
    'float64'
    >>> c = a+b
    >>> carray([ 1.        ,  2.57079633,  4.14159265])
    >>> c.dtype.name   #b的精度更高,所以结果c和b是同一类型
    'float64'
    >>> d = np.exp(c*1j)
    >>> darray([ 0.54030231+0.84147098j, -0.84147098+0.54030231j,       -0.54030231-0.84147098j])
    >>> d.dtype.name
    'complex128'

    ndarray还提供了很多的数学操作如:random,sum,max,min等

    >>> a = np.random.random((2,3))
    >>> a
    array([[ 0.18626021,  0.34556073,  0.39676747],       [ 0.53881673,  0.41919451,  0.6852195 ]])
    >>> a.sum()
    2.5718191614547998
    >>> a.min()
    0.1862602113776709
    >>> a.max()
    0.6852195003967595

    你可以指定多维数组的特定维度进行计算

    >>> b = np.arange(12).reshape(3,4)
    >>> b
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11]])
    >>>>>> b.sum(axis=0)  # sum of each column
    array([12, 15, 18, 21])
    >>>>>> b.min(axis=1)      # min of each row
    array([0, 4, 8])
    >>>>>> b.cumsum(axis=1)     # cumulative sum along each row
    array([[ 0,  1,  3,  6],       [ 4,  9, 15, 22],       [ 8, 17, 27, 38]])

    numpy提供很多的算术方法如:sin,cos,exp

    >>> B = np.arange(3)
    >>> Barray([0, 1, 2])
    >>> np.exp(B)
    array([ 1.        ,  2.71828183,  7.3890561 ])
    >>> np.sqrt(B)array([ 0.        ,  1.        ,  1.41421356])
    >>> C = np.array([2., -1., 4.])
    >>> np.add(B, C)
    array([ 2.,  0.,  6.])

    数组的索引,切片和迭代

    >>> a = np.arange(10)**3
    >>> a
    array([  0,   1,   8,  27,  64, 125, 216, 343, 512, 729])
    >>> a[2]
    8
    >>> a[2:5]
    array([ 8, 27, 64])
    >>> a[:6:2] = -1000    # equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element to -1000
    >>> a
    array([-1000,     1, -1000,    27, -1000,   125,   216,   343,   512,   729])
    >>> a[ : :-1]                                 # reversed a
    array([  729,   512,   343,   216,   125, -1000,    27, -1000,     1, -1000])
    >>> for i in a:
    ...     print(i**(1/3.))
    ...nan
    1.0
    nan
    3.0
    nan
    5.0
    6.0
    7.0
    8.0
    9.0

    多维数组的每个维度都有一个索引

    >>> def f(x,y):
    ...     return 10*x+y
    ...
    >>> b = np.fromfunction(f,(5,4),dtype=int)
    >>> b
    array([[ 0,  1,  2,  3],       [10, 11, 12, 13],       [20, 21, 22, 23],       [30, 31, 32, 33],       [40, 41, 42, 43]])
    >>> b[2,3]
    23
    >>> b[0:5, 1]                       # each row in the second column of b
    array([ 1, 11, 21, 31, 41])
    >>> b[ : ,1]                        # equivalent to the previous example
    array([ 1, 11, 21, 31, 41])
    >>> b[1:3, : ]                      # each column in the second and third row of b
    array([[10, 11, 12, 13],       [20, 21, 22, 23]])

    如果维数太多的话可以使用...来替代。

    >>>c = np.array( [[[  0,  1,  2],
                    [ 10, 12, 13]],
                   [[100,101,102],
                   [110,112,113]]])
    >>> c.shape  #共有三个维度,0维2个元素,1维2个元素,2维3个元素
    (2, 2, 3) 
    >>> c[1,...]   #0维索引为1的元素,类似于list的get(1)
    array([[100, 101, 102],       [110, 112, 113]]) 
    >>>c[...,2]   #前面的...表示0维和1维,最后的数字表是2维的索引为2的元素
    array([[  2,  13],       [102, 113]])

    迭代数组元素

    >>> for row in b: #迭代数组输出维度维0的元素信息
            print(b)
    >>> for element in b.flat: #输出数组的每一个元素
             print(element)

    改变数组的形状

    >>> a = np.floor(10*np.random.random((3,4)))
    >>> a
    array([[ 2.,  8.,  0.,  6.], [ 4.,  5.,  1.,  1.],[ 8.,  9.,  3.,  6.]])
    >>> a.shape
    (3, 4)
    >>> a.ravel() #扁平话处理,及只有一个维度
    array([ 2.,  8.,  0.,  6.,  4.,  5.,  1.,  1.,  8.,  9.,  3.,  6.])
    >>> a.reshape(6,2) #改变形状为6行2列形式
    array([[ 2.,  8.],
           [ 0.,  6.],       
           [ 4.,  5.],       
           [ 1.,  1.],       
           [ 8.,  9.],       
           [ 3.,  6.]])
    >>> a.T #换位,即行变成列,列变成行
    array([[ 2.,  4.,  8.],
           [ 8.,  5.,  9.],       
           [ 0.,  1.,  3.],       
           [ 6.,  1.,  6.]])
    >>> a.T.shape
    (4, 3)
    >>> a.shape
    (3, 4)
    reshape改变了数组的形状,新生乘了一个数组,但是resize改变个数组的形状,改变的是自身的数据
    >>> a
    array([[ 2.,  8.,  0.,  6.],       [ 4.,  5.,  1.,  1.],       [ 8.,  9.,  3.,  6.]])
    >>> a.resize((2,6))
    >>> a
    array([[ 2.,  8.,  0.,  6.,  4.,  5.],       [ 1.,  1.,  8.,  9.,  3.,  6.]])
    合并数组:横向合并和纵向合并
    >>> a = np.floor(10*np.random.random((2,2)))
    >>> a
    array([[ 8.,  8.],       [ 0.,  0.]])
    >>> b = np.floor(10*np.random.random((2,2)))
    >>> b
    array([[ 1.,  8.],       [ 0.,  4.]])
    >>> np.vstack((a,b)) #纵向合并
    array([[ 8.,  8.],       [ 0.,  0.],       [ 1.,  8.],       [ 0.,  4.]])
    >>> np.hstack((a,b)) #横向合并
    array([[ 8.,  8.,  1.,  8.],       [ 0.,  0.,  0.,  4.]])
    
    
    
    
    column_stack合并数组,值合并第一维度
    >>> from numpy import newaxis
    >>> np.column_stack((a,b))   # With 2D arrays
    array([[ 8.,  8.,  1.,  8.],       [ 0.,  0.,  0.,  4.]])
    
    >>> a = np.array([4.,2.])
    >>> b = np.array([2.,8.])
    >>> a[:,newaxis]  # This allows to have a 2D columns vector
    array([[ 4.],       [ 2.]])
    
    >>> np.column_stack((a[:,newaxis],b[:,newaxis])) #合并2维数组
    array([[ 4.,  2.],       [ 2.,  8.]])
    
    >>> np.vstack((a[:,newaxis],b[:,newaxis]))
    array([[ 4.],
           [ 2.],       
           [ 2.],       
           [ 8.]])
    hsplit水平方向切割数组
    >>> a = np.floor(10*np.random.random((2,12)))
    >>> a
    array([[ 9.,  5.,  6.,  3.,  6.,  8.,  0.,  7.,  9.,  7.,  2.,  7.],
           [ 1.,  4.,  9.,  2.,  2.,  1.,  0.,  6.,  2.,  2.,  4.,  0.]])
    >>> np.hsplit(a,3)   # Split a into 3 切割成3个数组
    [array([[ 9.,  5.,  6.,  3.],
           [ 1.,  4.,  9.,  2.]]), 
           array([[ 6.,  8.,  0.,  7.],       
           [ 2.,  1.,  0.,  6.]]), 
           array([[ 9.,  7.,  2.,  7.],       
           [ 2.,  2.,  4.,  0.]])]
    array_split可以自定义切割的水平方向哈市垂直方向
    >>> x = np.arange(8.0)
    >>> np.array_split(x, 3) #array_split(ary, indices_or_sections, axis=0)
        [array([ 0.,  1.,  2.]), array([ 3.,  4.,  5.]), array([ 6.,  7.])]
    
    复制
    简单的赋值,b指向a的对象
    >>> a = np.arange(12)
    >>> b = a            # no new object is created
    >>> b is a           # a and b are two names for the same ndarray object
    True
    >>> b.shape = 3,4    # changes the shape of a
    >>> a.shape
    (3, 4)
    
    
    快照模式
    >>> c = a.view()
    >>> c is a
    False
    >>> c.base is a    # c is a view of the data owned by a
    True
    >>> c.flags.owndata
    False
    >>> c.shape = 2,6    # a's shape doesn't change
    >>> a.shape(3, 4)
    >>> c[0,4] = 1234    # a's data changes
    >>> a
    array([[   0,    1,    2,    3],
           [1234,    5,    6,    7],      
            [   8,    9,   10,   11]])
            
     
     深度拷贝
     >>> d = a.copy()   # a new array object with new data is created
     >>> d is a
     False
     >>> d.base is a    # d doesn't share anything with a
     False
     >>> d[0,0] = 9999
     >>> a
     array([[   0,   10,   10,    3],
            [1234,   10,   10,    7],       
            [   8,   10,   10,   11]])

关键字

上一篇: Python Day5

下一篇: python数据结构