I have 2D arrays of counts from which I need to extract a sequence of arbitrary subtotals. In this example they are subtotal columns. Each subtotal is the sum of an arbitrary collection of the base columns, represented by a tuple of addend-indices:
>>> A
[[11, 12, 13, 14, 15]
[21, 22, 23, 24, 25]
[31, 32, 33, 34, 35]]
>>> subtotal_addend_idxs
((0, 1), (1, 2, 3), (3, 4))
>>> desired_result
[[23, 39, 29]
[43, 69, 49]
[63, 99, 69]]
The best code I have for this so far is this:
subtotal_addend_idxs = ((0, 1), (1, 2, 3), (3, 4))
np.hstack(
tuple(
np.sum(A[:, subtotal_addend_idxs], axis=1, keepdims=True)
for addend_idxs in self._column_addend_idxs
)
)
Is there a clever way I can do this with a single numpy call/expression where I don't need a for loop creating a tuple of individual subtotal columns?
Note that the addend-indices are arbitrary; not all indices need appear in a subtotal, the indices do not necessarily appear in increasing order, and the same index can appear in more than one subtotal.