Group and average NumPy matrix

前端 未结 4 1115
一整个雨季
一整个雨季 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:07

    A compact solution is to use numpy_indexed (disclaimer: I am its author), which implements a fully vectorized solution:

    import numpy_indexed as npi
    npi.group_by(arr[:, 2]).mean(arr)
    

提交回复
热议问题