zoukankan      html  css  js  c++  java
  • python开发笔记-ndarray方法属性详解

      Python中的数组ndarray是什么?

    1、NumPy中基本的数据结构

    2、所有元素是同一种类型

    3、别名是array

    4、利于节省内存和提高CPU计算时间

    5、有丰富的函数

       ndarray的创建:

    import numpy as np  
    >>> aArray=np.array([1,2,3])  
    >>> aArray  
    array([1, 2, 3])  
    >>> bArray=np.array([(1,2,3),(4,5,6)])  
    >>> bArray  
    array([[1, 2, 3],  
           [4, 5, 6]])  
    >>> np.arange(1,5,0.5)  
    array([1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])  
    >>> np.random.random((2,2))  
    array([[0.15637741, 0.23650666],  
           [0.37523649, 0.4608882 ]])  
    >>> np.linspace(1,2,10,endpoint=False)  
    array([1. , 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9])  
    

      

    np.ones([2,3])  
    array([[1., 1., 1.],  
           [1., 1., 1.]])  
    >>> np.zeros((2,2))  
    array([[0., 0.],  
           [0., 0.]])  
    >>> np.fromfunction(lambda i,j:(i+1)*(j+1),(9,9))  
    array([[ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.],  
           [ 2.,  4.,  6.,  8., 10., 12., 14., 16., 18.],  
           [ 3.,  6.,  9., 12., 15., 18., 21., 24., 27.],  
           [ 4.,  8., 12., 16., 20., 24., 28., 32., 36.],  
           [ 5., 10., 15., 20., 25., 30., 35., 40., 45.],  
           [ 6., 12., 18., 24., 30., 36., 42., 48., 54.],  
           [ 7., 14., 21., 28., 35., 42., 49., 56., 63.],  
           [ 8., 16., 24., 32., 40., 48., 56., 64., 72.],  
           [ 9., 18., 27., 36., 45., 54., 63., 72., 81.]])  
    

      

    import numpy as np  
    >>> x = np.array([(1,2,3),(4,5,6)])  
    >>> x  
    array([[1, 2, 3],  
           [4, 5, 6]])  
    >>> x.ndim  
    2  
    >>> x.shape  
    (2, 3)  
    >>> x.size  
    6  
    

      

    import numpy as np  
    >>> aArray=np.array([(1,2,3),(4,5,6)])  
    >>> print(aArray[1])  
    [4 5 6]  
    >>> print(aArray[0])  
    [1 2 3]  
    >>> print(aArray[0:2])  
    [[1 2 3]  
     [4 5 6]]  
    >>> print(aArray[:,[0,1]])  
    [[1 2]  
     [4 5]]  
    >>> print(aArray[1,[0,1]])  
    [4 5]  
    >>> for row in aArray:  
        print(row)  
      
          
    [1 2 3]  
    [4 5 6]  
    

      ndarray的操作:  

    import numpy as np  
    >>> aArray=np.array([(1,2,3),(4,5,6)])  
    >>> aArray.shape  
    (2, 3)  
    >>> bArray=aArray.reshape(3,2)  
    >>> bArray  
    array([[1, 2],  
           [3, 4],  
           [5, 6]])  
    >>> aArray  
    array([[1, 2, 3],  
           [4, 5, 6]])  
    

      

    import numpy as np  
    >>> aArray=np.array([(1,2,3),(4,5,6)])  
    >>> aArray.resize(3,2)  
    >>> aArray  
    array([[1, 2],  
           [3, 4],  
           [5, 6]])  
    >>> bArray=np.array([1,3,7])  
    >>> cArray=np.array([3,5,8])  
    >>> np.vstack((bArray,cArray))  
    array([[1, 3, 7],  
           [3, 5, 8]])  
    >>> np.hstack((bArray,cArray))  
    array([1, 3, 7, 3, 5, 8])  
    

      ndarray的运算:

        

    import numpy as np  
    >>> aArray=np.array([(5,5,5),(5,5,5)])  
    >>> bArray=np.array([(2,2,2),(2,2,2)])  
    >>> cArray=aArray*bArray  
    >>> cArray  
    array([[10, 10, 10],  
           [10, 10, 10]])  
    >>> aArray+=bArray  
    >>> aArray  
    array([[7, 7, 7],  
           [7, 7, 7]])  
    

      广播的思想:

         

    a=np.array([1,2,3])  
    >>> b=np.array([[1,2,3],[4,5,6]])  
    >>> a+b  
    array([[2, 4, 6],  
           [5, 7, 9]])  
    

      统计运算:

        

    import numpy as np  
    >>> aArray=np.array([(1,2,3),(4,5,6)])  
    >>> aArray.sum()  
    21  
    >>> aArray.sum(axis=0)  
    array([5, 7, 9])  
    >>> aArray.sum(axis=1)  
    array([ 6, 15])  
    >>> aArray.min()  
    1  
    >>> aArray.argmax()  
    5  
    >>> aArray.mean()  
    3.5  
    >>> aArray.var()  
    2.9166666666666665  
    >>> aArray.std()  
    1.707825127659933  
    

      ndarray的专门应用--线性代数:

        

    >>> import numpy as np  
    >>> x=np.array([[1,2],[3,4]])  
    >>> r1=np.linalg.det(x)  
    >>> print(r1)  
    -2.0000000000000004  
    >>> r1  
    -2.0000000000000004  
    >>> r2=np.linalg.inv(x)  
    >>> r2  
    array([[-2. ,  1. ],  
           [ 1.5, -0.5]])  
    >>> print(r2)  
    [[-2.   1. ]  
     [ 1.5 -0.5]]  
    >>> r3=np.dot(x,x)  
    >>> r3  
    array([[ 7, 10],  
           [15, 22]])  
    >>> print(r3)  
    [[ 7 10]  
     [15 22]] 
    

      

  • 相关阅读:
    C++友元
    C++类与static
    C++const
    reinterpret_cast应用
    学习软件工程课的心得上
    学习软件工程课的心得下
    项目总结报告之假如历史重新再来一次
    人月神话读后感
    团队任务考核
    冲刺周期会议十一
  • 原文地址:https://www.cnblogs.com/68xi/p/8564500.html
Copyright © 2011-2022 走看看