[Numpy-discussion] Python scalar float indices into array work - but not for array indices - why ?

Sebastian Haase seb.haase@gmail....
Wed Nov 17 13:27:59 CST 2010

```On Wed, Nov 17, 2010 at 8:15 PM, Robert Kern <robert.kern@gmail.com> wrote:
> On Wed, Nov 17, 2010 at 13:11, Sebastian Haase <seb.haase@gmail.com> wrote:
>> On Wed, Nov 17, 2010 at 7:48 PM, Nathaniel Smith <njs@pobox.com> wrote:
>>> On Wed, Nov 17, 2010 at 10:32 AM, Sebastian Haase <seb.haase@gmail.com> wrote:
>>>> On Wed, Nov 17, 2010 at 7:26 PM, Robert Kern <robert.kern@gmail.com> wrote:
>>>>> On Wed, Nov 17, 2010 at 12:20, Sebastian Haase <seb.haase@gmail.com> wrote:
>>>>>> Why does numpy not accept float arrays as indices ?
>>>>>> I was very happy and quite surprised once I found out that it worked
>>>>>> at all for Python float scalars,
>>>>>> but would it not just be consequent to also allow float ndarrays then ?
>>>>>
>>>>> It only works for float scalars by accident. Do not rely on it.
>>>>
>>>> Could you be more specific ?  As a feature, it for sure can be useful.
>>>
>>> I think Robert Kern has the same intuition as me: that supporting
>>> float indices is pointless. So, can you give any *specific examples*
>>> of things you can do with float indices that would be difficult or
>>> more expensive using integer indices? That's probably the best way to
>>> convince people.
>>>
>>> -- Nathaniel
>> Well,
>> suppose you have 2 vectors of floating point coordinates `x` and `y`
>> and you want to do operations utilizing fancy indexing like
>> image[ [x,y] ] +=  1
>>
>> As I just realized, this specific case seems to be addressed by histogram2d,
>> however, if float indices would work this would of course be much more
>> general: higher dimensionality and not just '+=' operations.
>
> Actually, it wouldn't work even if x and y were integers.
>

I guess you are right again - see this simplified 1d test:
>>> a = np.zeros(4, int)
>>> a
[0 0 0 0]
>>> a[ [1,3] ] += 1
>>> a
[0 1 0 1]
>>> a[ [1,3,1] ] += 1
>>> a
[0 2 0 2]
>>>
>>> a = np.zeros(4, int)
>>> a
[0 0 0 0]
>>> a[ [np.array((1,3))] ] += 1
>>> a
[0 1 0 1]
>>> a[ [np.array((1,3,1))] ] += 1
>>> a
[0 2 0 2]

So, the fancy indexing appears to treat arrays exactly like plain lists.
And my idea of using it for operating on a sequence of indices appears
to work at first,
but then in case of duplicate indices (1 in my example) the += works
only once ....
I don't understand ...

Thanks for any hint,
Sebastian
```