Numpy: Average of values corresponding to unique coordinate positions

前端 未结 4 1461
误落风尘
误落风尘 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:37

    It is very likely going to be faster to flatten your indices, i.e.:

    flat_index = coo[:, 0] * np.max(coo[:, 1]) + coo[:, 1]
    

    then use np.unique on it:

    unq, unq_idx, unq_inv, unq_cnt = np.unique(flat_index,
                                               return_index=True,
                                               return_inverse=True,
                                               return_counts=True)
    unique_coo = coo[unq_idx]
    unique_mean = np.bincount(unq_inv, values) / unq_cnt
    

    than the similar approach using lexsort.

    But under the hood the method is virtually the same.

提交回复
热议问题