Prokop Hapala - 2 months ago 29

Python Question

I'm trying to do histrogram using numpy array indexing (without explicit iteration over array). Just to check if it works as expected I did following test:

`import numpy as np`

arr = np.zeros(10)

inds = np.array([1,2,3,1,3,5,3])

arr[inds] += 1.0

print(arr)

the result is

`[ 0. 1. 1. 1. 0. 1. 0. 0. 0. 0.]`

`[ 0. 2. 1. 3. 0. 1. 0. 0. 0. 0.]`

(i.e. it omits indexes which appear multiple times in index array)

I'm not sure if there is some reason for this behavior (perhaps to make these operation order independent and therefore easier to paralellize).

Is there any other way how to do this in numpy ?

Answer

A well fitted NumPy function for what you require is numpy.bincount().
As the result of this function will have the `size = inds.max()`

, you will have to slice arr to specify which indexes will be added. If not, the shapes will not coincide.

```
import numpy as np
arr = np.zeros(10)
inds = np.array([1,2,3,1,3,5,3])
values = np.bincount(inds)
print values
arr[:values.size]+= values
print(arr)
```

values will be:

```
[0 2 1 3 0 1]
```

and arr will take the form:

`array([ 0., 2., 1., 3., 0., 1., 0., 0., 0., 0.])`