Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add multiple values to one numpy array index

Simple Version: if I do this:

import numpy as np
a = np.zeros(2)
a[[1, 1]] += np.array([1, 1])

I get [0, 1] as an output. but I would like [0, 2]. Is that possible somehow, using implicit numpy looping instead of looping over it myself?

What-I-actually-need-to-do version:

I have a structured array that contains an index, a value, and some boolean value. I would like to sum those values at those indices, based on the boolean. Clearly that can be done with a simple loop, but it seems like it should be possible with clever numpy indexing (as above).

For example, I have an array with 5 elements that I want to populate from the array with values, indices, and conditions:

import numpy as np
size = 5
nvalues = 10
np.random.seed(1)
a = np.zeros(nvalues, dtype=[('val', float), ('ix', int), ('cond', bool)])
a = np.rec.array(a)
a.val = np.random.rand(nvalues)
a.cond = (np.random.rand(nvalues) > 0.3)
a.ix = np.random.randint(size, size=nvalues)

# obvious solution
obvssum = np.zeros(size)
for i in a:
    if i.cond:
        obvssum[i.ix] += i.val

# is something this possible?
doesntwork = np.zeros(size)
doesntwork[a[a.cond].ix] += a[a.cond].val

print(doesntwork)
print(obvssum)

Output:

[ 0.          0.          0.61927097  0.02592623  0.29965467]
[ 0.          0.          1.05459336  0.02592623  1.27063303]

I think what's happening here is if a[a.cond].ix were guaranteed to be unique, my method would work just fine, as noted in the simple example.

like image 906
Matt Avatar asked Feb 01 '17 21:02

Matt


People also ask

How do I add a value to each element of an array in NumPy?

Numpy Array – Add a constant to all elements of the array Adding a constant to a NumPy array is as easy as adding two numbers. To add a constant to each and every element of an array, use addition arithmetic operator + . To addition operator, pass array and constant as operands as shown below.

Can you use += on NumPy array?

Numpy arrays are mutable objects that have clearly defined in place operations. If a and b are arrays of the same shape, a += b adds the two arrays together, using a as an output buffer.

Can you add elements to a NumPy array?

If you are using array module, you can use the concatenation using the + operator, append(), insert(), and extend() functions to add elements to the array. If you are using NumPy arrays, use the append() and insert() function.

Is appending to NumPy array efficient?

Appending to numpy arrays is very inefficient. This is because the interpreter needs to find and assign memory for the entire array at every single step. Depending on the application, there are much better strategies. If you know the length in advance, it is best to pre-allocate the array using a function like np.


1 Answers

This is what the at method of NumPy ufuncs is for:

output = numpy.zeros(size)
numpy.add.at(output, a[a.cond].ix, a[a.cond].val)
like image 173
user2357112 supports Monica Avatar answered Sep 30 '22 10:09

user2357112 supports Monica