Group and average NumPy matrix

前端 未结 4 1141
一整个雨季
一整个雨季 2021-02-20 07:46

Say I have an arbitrary numpy matrix that looks like this:

arr = [[  6.0   12.0   1.0]
       [  7.0   9.0   1.0]
       [  8.0   7.0   1.0]
       [  4.0   3.0          


        
4条回答
  •  遇见更好的自我
    2021-02-20 08:16

    You can do:

    for x in sorted(np.unique(arr[...,2])):
        results.append([np.average(arr[np.where(arr[...,2]==x)][...,0]), 
                        np.average(arr[np.where(arr[...,2]==x)][...,1]),
                        x])
    

    Testing:

    >>> arr
    array([[  6.,  12.,   1.],
           [  7.,   9.,   1.],
           [  8.,   7.,   1.],
           [  4.,   3.,   2.],
           [  6.,   1.,   2.],
           [  2.,   5.,   2.],
           [  9.,   4.,   3.],
           [  2.,   1.,   4.],
           [  8.,   4.,   4.],
           [  3.,   5.,   4.]])
    >>> results=[]
    >>> for x in sorted(np.unique(arr[...,2])):
    ...     results.append([np.average(arr[np.where(arr[...,2]==x)][...,0]), 
    ...                     np.average(arr[np.where(arr[...,2]==x)][...,1]),
    ...                     x])
    ... 
    >>> results
    [[7.0, 9.3333333333333339, 1.0], [4.0, 3.0, 2.0], [9.0, 4.0, 3.0], [4.333333333333333, 3.3333333333333335, 4.0]]
    

    The array arr does not need to be sorted, and all the intermediate arrays are views (ie, not new arrays of data). The average is calculated efficiently directly from those views.

提交回复
热议问题