- Each element of the array is a coordinate(x, y).
- Each coordinate has two labels.
Goal: sum the elements that have the same two labels.
How can this be made faster?
import numpy
from scipy import ndimage
label1 = numpy.array([0, 0, 1, 1, 2, 2])
kinds_of_label1 = 3
label2 = numpy.array([0, 1, 0, 0, 1, 1])
kinds_of_label2 = 2
data = numpy.array([[1, 2], [3, 8], [4, 5], [2, 9], [1, 3], [7, 2]])
data_T = data.view().T
### processing ####
label1_and_2 = label1 * kinds_of_label2 + label2
result = numpy.empty((kinds_of_label1 * kinds_of_label2, 2))
result_T = result.view().T
result_T[0] = ndimage.measurements.sum(
position.T[0], labels=label1_and_2,
index=range(kinds_of_label1 * kinds_of_label2)
)
result_T[1] = ndimage.measurements.sum(
position.T[1], labels=label1_and_2,
index=range(kinds_of_label1 * kinds_of_label2)
)
### output ###
print(result)
# [[ 3. 4.]
# [ 1. 2.]
# [ 8. 15.]
# [ 0. 0.]
# [ 0. 0.]
# [ 1. 6.]]
position
come from? – Gareth Rees Jul 4 '14 at 19:16