In Python 3, using numpy, I have an array of values and an array of indexes. I need to find the sum (or mean) of the value array at each index in the index array, for each value in the index array. For speed reasons I am looking to do this using numpy slicing instead of for loops.
import numpy
value_array = numpy.array([[0.1, 0.2, 0.5, 1 ],)
[2 , 5, 10, 20]])
index_array = numpy.array([[ 0, 1, 1, 2 ],
[ 2, 0, 1, 4 ]])
# Something like desired_sum = numpy.sum(value_array[index_array])
# Where the output will be
desired_sum = numpy.array([5.1, 10.7, 3, 0, 20])
This will be running on larger arrays {~shape = (2000, 2000)} with a few hundred indexes.