zoukankan      html  css  js  c++  java
  • python 常用技巧 — 数组 (array)

    目录:

    1. 数组每一行除以这一行的总数(numpy divide row by row sum)

    2. 数组每一行或者每一列求平均 (python average array columns or rows)

    3. 数组每一行或者每一列求加权平均 (python weight average array columns or rows)

    4. 计算数组得到每一行或者每一列的和 (python sum columns of an array)

    5. 生成指定维度的随机矩阵 (python generate random array)

    6. 数组中对元素进行布尔类型判断 (python check elements in array with Boolean type)

    7. 数组中是否存在满足条件的数 (python check if exsit element in array satisfies a condition)

    8. 数组中所有元素是否有0元素 (python check whether all elements in numpy is zero)

    9. 数组找到满足一个条件或多个的数据(python numpy find data that satisfty one or multiple conditions)

    10. 数组中所有元素相乘(python numpy multiple every element in an array)

    内容:

    1. 数组每一行除以这一行的总数(numpy divide row by row sum)

    https://stackoverflow.com/questions/16202348/numpy-divide-row-by-row-sum

    方法1:

    >>> e
    array([[ 0.,  1.],
           [ 2.,  4.],
           [ 1.,  5.]])
    >>> e/e.sum(axis=1)[:,None]
    array([[ 0.        ,  1.        ],
           [ 0.33333333,  0.66666667],
           [ 0.16666667,  0.83333333]])

    方法2:

    >>> (e.T/e.sum(axis=1)).T
    array([[ 0.        ,  1.        ],
           [ 0.33333333,  0.66666667],
           [ 0.16666667,  0.83333333]])

    方法3:

    >>> e/e.sum(axis=1, keepdims=True)
    array([[ 0.        ,  1.        ],
           [ 0.33333333,  0.66666667],
           [ 0.16666667,  0.83333333]])
    

     

    2. 数组每一行或者每一列求平均 (python average array columns or rows)

    import numpy as np
    In [50]: a=np.arange(1,13).reshape(3,4)
    In [51]: a
    Out[51]: 
    array([[ 1,  2,  3,  4],
           [ 5,  6,  7,  8],
           [ 9, 10, 11, 12]])
    In [52]: np.average(a, axis=1)
    Out[52]: array([ 2.5,  6.5, 10.5])
    In [53]: np.average(a, axis=0)
    Out[53]: array([5., 6., 7., 8.])
    

    3. 数组每一行或者每一列求加权平均 (python weight average array columns or rows)

    https://docs.scipy.org/doc/numpy/reference/generated/numpy.average.html

    >>> data = np.arange(6).reshape((3,2))
    >>> data
    array([[0, 1],
           [2, 3],
           [4, 5]])
    >>> np.average(data, axis=1, weights=[1./4, 3./4])
    array([0.75, 2.75, 4.75])
    

      

    4. 计算数组得到每一行或者每一列的和 (python sum columns of an array)

    https://stackoverflow.com/questions/13567345/how-to-calculate-the-sum-of-all-columns-of-a-2d-numpy-array-efficiently

    >>> import numpy as np
    >>> a = np.arange(12).reshape(4,3)
    >>> a.sum(axis=0)
    array([18, 22, 26])
    >>> a.sum(axis=1)
    array([ 3, 12, 21, 30])

    5. 生成指定维度的随机矩阵 (python generate random array)

    https://www.codespeedy.com/how-to-create-matrix-of-random-numbers-in-python-numpy/

    (1)生成指定维度的小数数组

    In [1]: import numpy as np
    
    In [2]: a=np.random.rand(3,4)
    
    In [3]: a
    Out[3]: 
    array([[0.03403289, 0.31416715, 0.42700029, 0.49101901],
           [0.70750959, 0.4852401 , 0.11448147, 0.21570702],
           [0.87512839, 0.82521751, 0.56915875, 0.67623931]])
    

    (2)生成只能维度的整数数组

    In [8]: np.random.randint(1,10,size=(3,4))
    Out[8]: 
    array([[8, 1, 4, 3],
           [7, 1, 8, 7],
           [2, 5, 4, 3]])
    

     

    6. 数组中对元素进行布尔类型判断 (python check elements in array with Boolean type)

    https://docs.scipy.org/doc/numpy/reference/generated/numpy.all.html

    https://docs.scipy.org/doc/numpy/reference/generated/numpy.any.html

    >>> np.all([-1, 4, 5]) 
    True
    
    >>> np.all([[True,False],[True,True]])
    False
    
    >>> np.all([[True,False],[True,True]], axis=0)
    array([ True, False])
    
    // 如果要判断至少存在一个元素则使用
    
    >>> np.any([-1, 0, 5])
    True
    
    >>> np.any([[True, False], [True, True]])
    True
    
    >>> np.any([[True, False], [False, False]], axis=0)
    array([ True, False])
    

    7. 数组中是否存在满足条件的数 (python check if exsit element in array satisfies a condition)

    In [1]: import numpy as np
    
    In [2]: a=np.arange(1, 13).reshape(3, 4)
    
    In [3]: a
    Out[3]: 
    array([[ 1,  2,  3,  4],
           [ 5,  6,  7,  8],
           [ 9, 10, 11, 12]])
    
    In [4]: a>7
    Out[4]: 
    array([[False, False, False, False],
           [False, False, False,  True],
           [ True,  True,  True,  True]])
    
    In [5]: np.any(a>7)
    Out[5]: True
    
    In [6]: np.all(a>7)
    Out[6]: False
    

    8. 数组中所有元素是否有0元素 (python check whether all elements in numpy is zero)

    https://stackoverflow.com/questions/18395725/test-if-numpy-array-contains-only-zeros

    In [1]: import numpy as np
    
    In [2]: not np.any(np.array([0, 0, 2]))
    Out[2]: False
    
    In [3]: not np.any(np.array([0, 0, 0]))
    Out[3]: True
    
    # 计算非零个数再进行判断 In [4]: np.count_nonzero(np.array([0, 0, 2])) Out[4]: 1 In [5]: np.count_nonzero(np.array([0, 0, 0])) Out[5]: 0
    # 用集合去掉重复元素再判断 In [6]: set(np.array([0, 0, 2])) Out[6]: {0, 2} In [7]: set(np.array([0, 0, 0])) Out[7]: {0}

    9. 数组找到满足一个条件或多个的索引和值(python numpy find index and value that satisfty one or multiple conditions)

    参考资料:https://thispointer.com/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array/

    https://blog.csdn.net/wen123qiang/article/details/49204551

    (1)一个条件

    In [127]: c                                                                                                                                                                                                        
    Out[127]: 
    array([[ 9, 18,  1],
           [ 6,  5,  0],
           [11,  7,  0],
           [11,  4,  0],
           [ 8,  4,  1],
           [10,  5,  0],
           [17, 12,  1],
           [19,  1,  1],
           [ 8, 11,  1],
           [11, 12,  1]])
    # 找到满足条件的索引
    In [128]: np.where((c[:,2]!=0))                                                                                                                                                                    
    Out[128]: (array([0, 4, 6, 7, 8, 9]),)
    # 找到满足条件的值方法1
    In [129]: c[np.where((c[:,2]!=0))]                                                                                                                                                                          
    Out[129]: 
    array([[ 9, 18,  1],
           [ 8,  4,  1],
           [17, 12,  1],
           [19,  1,  1],
           [ 8, 11,  1],
           [11, 12,  1]])
    # 找到满足条件的值方法2
    In [130]: c[c[:,2]!=0]                                                                                                                                                               
    Out[130]: 
    array([[ 9, 18,  1],
           [ 8,  4,  1],
           [17, 12,  1],
           [19,  1,  1],
           [ 8, 11,  1],
           [11, 12,  1]])

    (2)多个条件

    # 找到满足条件的索引
    In [1]: np.where((c[:,2]!=0) & (c[:,1]==12))                                                                                                                                                              
    Out[1]: (array([6, 9]),)
    # 找到满足条件的值方法1
    In [2]: c[np.where((c[:,2]!=0) & (c[:,1]==12))]                                                                                                                                                               
    Out[2]: 
    array([[17, 12,  1],
           [11, 12,  1]])
    # 找到满足条件的值方法2
    In [3]: c[(c[:,2]!=0) & ( c[:,1]==12)]                                                                                                                                      
    Out[3]: 
    array([[17, 12,  1],
           [11, 12,  1]])
    

     

    10. 数组中所有元素相乘(python numpy multiple every element in an array)

    https://stackoverflow.com/questions/44212854/is-there-a-method-in-numpy-to-multiply-every-element-in-an-array

    In [8]: a = np.array([3, 4])
    
    In [9]: a
    Out[9]: array([3, 4])
    
    In [10]: a.shape
    Out[10]: (2,)
    
    In [11]: np.prod(a)
    Out[11]: 12
    

      

      

  • 相关阅读:
    jQuery动态加载动画spin.js
    jQuery自动过滤单词插件
    基于jQuery的自定义滚动条
    jQuery纵向分类下拉菜单导航
    仿酷狗官网新闻焦点图插件
    metro扁平UI网页组件
    HTML5环形音乐播放器
    纯CSS3个性化圆形按钮登录表单
    纯CSS3绘制的黑色图标按钮组合
    纯CSS3实现iOS7扁平化图标
  • 原文地址:https://www.cnblogs.com/ttweixiao-IT-program/p/11866275.html
Copyright © 2011-2022 走看看