Sum array by number in numpy

前端 未结 8 1159
别跟我提以往
别跟我提以往 2020-11-30 08:51

Assuming I have a numpy array like: [1,2,3,4,5,6] and another array: [0,0,1,2,2,1] I want to sum the items in the first array by group (the second array) and obtain n-groups

8条回答
  •  猫巷女王i
    2020-11-30 09:38

    I tried scripts from everyone and my considerations are:

    Joe: Will only work if you have few groups.

    kevpie: Too slow because of loops (this is not pythonic way)

    Bi_Rico and Sven: perform good, but will only work for Int32 (if the sum goes over 2^32/2 it will fail)

    Alex: is the fastest one, good for sum.

    But if you want more flexibility and the possibility to group by other statistics use SciPy:

    from scipy import ndimage
    
    data = np.arange(10000000)
    groups = np.arange(1000).repeat(10000)
    ndimage.sum(data, groups, range(1000))
    

    This is good because you have many statistics to group (sum, mean, variance, ...).

提交回复
热议问题