Numpy: Average of values corresponding to unique coordinate positions

前端 未结 4 1456
误落风尘
误落风尘 2021-01-02 05:15

So, I have been browsing stackoverflow for quite some time now, but I can\'t seem to find the solution for my problem

Consider this

import numpy as n         


        
4条回答
  •  离开以前
    2021-01-02 05:43

    You can sort coo with np.lexsort to bring the duplicate ones in succession. Then run np.diff along the rows to get a mask of starts of unique XY's in the sorted version. Using that mask, you can create an ID array that would have the same ID for the duplicates. The ID array can then be used with np.bincount to get the summation of all values with the same ID and also their counts and thus the average values, as the final output. Here's an implementation to go along those lines -

    # Use lexsort to bring duplicate coo XY's in succession
    sortidx = np.lexsort(coo.T)
    sorted_coo =  coo[sortidx]
    
    # Get mask of start of each unique coo XY
    unqID_mask = np.append(True,np.any(np.diff(sorted_coo,axis=0),axis=1))
    
    # Tag/ID each coo XY based on their uniqueness among others
    ID = unqID_mask.cumsum()-1
    
    # Get unique coo XY's
    unq_coo = sorted_coo[unqID_mask]
    
    # Finally use bincount to get the summation of all coo within same IDs 
    # and their counts and thus the average values
    average_values = np.bincount(ID,values[sortidx])/np.bincount(ID)
    

    Sample run -

    In [65]: coo
    Out[65]: 
    array([[1, 2],
           [2, 3],
           [3, 4],
           [3, 4],
           [1, 2],
           [5, 6],
           [1, 2]])
    
    In [66]: values
    Out[66]: array([1, 2, 4, 2, 1, 6, 1])
    
    In [67]: unq_coo
    Out[67]: 
    array([[1, 2],
           [2, 3],
           [3, 4],
           [5, 6]])
    
    In [68]: average_values
    Out[68]: array([ 1.,  2.,  3.,  6.])
    

提交回复
热议问题