1

I have two 1D Numpy arrays index_data and charge, which are the same length and contain ints and floats respectively. I am trying to make a total_charge array as follows:

total_charge = []
for i in range(len(index_data)):
    if index_data[i] == 0:
        total_charge.append(charge[i])
    else:
        total_charge[-1] += charge[i]
total_charge = np.array(total_charge)

How would I vectorize this? Help me Numpy wizards, you're my only hope.

2
  • I don't think this lends itself to vectorization very easily ... Commented May 22, 2014 at 19:52
  • 1
    A Star Wars reference from Mr. Spock? No, that's just not right. :) Commented May 22, 2014 at 20:27

1 Answer 1

4

A concrete example of index_data and charge would help clarify the expected input. From reading what you have, however, I think using np.add.reduceat can be used.

Apparently each new group of charges is indicated by a 0 in index_data; otherwise, the values in index_data are not used. total_charge is the sum of the charges in each group.

Here's an example of how reduceat could be used.

First, some demo data:

In [105]: index_data
Out[105]: array([0, 1, 1, 0, 1, 1, 1, 0, 1])

In [106]: charge
Out[106]: array([ 1.5,  2. ,  3. ,  2.5,  1.5,  1. ,  1. ,  2. ,  1. ])

zerolocs gives the indices where 0 occurs in index_data:

In [107]: zerolocs = where(index_data==0)[0]

In [108]: zerolocs
Out[108]: array([0, 3, 7])

Use np.add.reduceat to sum the charge groups.

In [109]: total_charge = np.add.reduceat(charge, zerolocs)

In [110]: total_charge
Out[110]: array([ 6.5,  6. ,  3. ])
Sign up to request clarification or add additional context in comments.

1 Comment

You are a miracle worker. I knew there was some obscure Numpy function that would do exactly what I wanted, I just didn't know which one. And hey, us Trekkies can still appreciate Star Wars.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.