1

I have a numpy array such as

original_array=np.arange(5)

I have another array that stores the value of indices to be updated

indices=[0,1,2,1,1]

Repetition in elements of indices signify that the element is being updated multiple times.

I have also the array that stores the values to be added.

updation_values=[0.2,0.2,0.4, 0.5, 0.4]

Usually I update the array as

for update_value, index in zip(updation_values, indices):
    original_array[index]+=update_value

Is there a better way of doing it apart from loops?

original_array[indices]+=updation_values

does not seem to work as it only does the updation of the last instance of each unique index.

sophros
  • 14,672
  • 11
  • 46
  • 75

1 Answers1

0

There are two questions in yours:

  1. Why it does not work as expected?

  2. How to make it faster?

Why it does not work?

You are creating integer ndarray - which means that fractional updates get rounded to the nearest integer and since the updates are <=0.5, there is no apparent change. You can see the ndarray as it is created works for ints:

original_array=np.arange(5)#, dtype=np.float)
indices=[0,1,2,1,1]
updation_values=[0.2,0.2,0.4, 0.5, 0.4]
updation_values=[1,1,1,1,1]

for update_value, index in zip(updation_values, indices):
    original_array[index] += update_value

print(original_array)

[1 4 3 3 4]

To ensure you are creating a ndarray with floats you have to use dtype optional parameter:

original_array=np.arange(5, dtype=np.float)

How to make it faster?

Generally, you want to use NumPy vectorization. Unfortunately, you have repetition in your indices variable so:

original_array[indices]+=updation_values

will not work.

You can see yourself:

print(original_array)
original_array[[0,1,2]] += 10
print(original_aray)

results with:

[1 4 3 3 4]

[11 14 13 3 4]

and so does:

original_array[[0,1,2,0,0]] += 10
sophros
  • 14,672
  • 11
  • 46
  • 75
  • I tried even with integer values. It doesn't work. I made ```updation_values = [1,1,1,1,1]``` and the final result I got was ```original_array=[1,2,3,3,4]``` instead of ```original_array=[1,4,3,3,4]``` – Valay Agarawal Jun 14 '21 at 14:07
  • It does for me resulting with: `[1 4 3 3 4]` – sophros Jun 14 '21 at 14:10
  • It does. And there is no surprise. If you run it in Jupyter notebook - restart the Python session and restart from scratch with the changes. – sophros Jun 14 '21 at 14:17
  • I am comfortable with loops, and I know it works. My question is how to make it faster. With the second method, ```original_array[indices]+=updation_values``` it does not work. I know why it doesn't work, but how I can do that. Please note the difference in the code blocks of loop and vector. – Valay Agarawal Jun 14 '21 at 14:20
  • Right, didn't address this one. I don't think you can do it this way as you have repeated indices in the `indices` variable. – sophros Jun 14 '21 at 14:29