[Numpy-discussion] ix_ and copies

Anne Archibald aarchiba at physics.mcgill.ca
Sun May 30 10:49:39 EDT 2010


On 30 May 2010 11:25, Keith Goodman <kwgoodman at gmail.com> wrote:
> On Sat, May 29, 2010 at 2:49 PM, Anne Archibald
> <aarchiba at physics.mcgill.ca> wrote:
>> On 29 May 2010 15:09, Robert Kern <robert.kern at gmail.com> wrote:
>>> On Sat, May 29, 2010 at 12:27, Keith Goodman <kwgoodman at gmail.com> wrote:
>>>> Will making changes to arr2 never change arr1 if
>>>>
>>>> arr2 = arr1[np.ix_(*lists)]
>>>>
>>>> where lists is a list of (index) lists? np.ix_ returns a tuple of
>>>> arrays so I'm guessing (and hoping) the answer is yes.
>>>
>>> Correct.
>>
>> To expand: any time you do fancy indexing - that is, index by anything
>> but a tuple of integers or slice objects - you get back a copy.
>
> I have never seen such a simple and clear definition of the line
> between regular and fancy indexing.
>
> To make sure I understand I'll try to expand. Is the following right?
>
> Regular indexing (no copy made):
> int
> float
> bool
> slice
> tuple of any combination of the above

I think you should not include bool on this list. Strictly speaking I
believe you can use bools as if they were integers, but that's a
little limited. Normally when one indexes with bools one is indexing
with an array of bools, as a sort of condition index; that is fancy
indexing.

The underlying reason for the copy/no copy distinction is that numpy
arrays must be evenly strided, that is, as you move along any axis,
the space between data elements must not vary. So slicing is no
problem, and supplying an integer is no problem. Supplying a float is
kind of bogus but might work anyway. Supplying None or np.newaxis also
works, since this just adds an axis of length one.

>
> Fancy indexing (copy made):
> Any indexing that is not regular indexing

The only two options here are (essentially) indexing with (tuples of)
arrays of indices or indexing with boolean (condition) arrays.

"Mixed" modes, where you supply a tuple containing some arrays of
indices and/or some booleans but also some slice objects or integers,
may work but may do something unexpected or may simply fail to work.
There was last time I looked no systematic testing of such
constructions, and the implementation was erratic. (This is largely a
definitional issue; given the way numpy's arrays of indices and
boolean indexing work it's not clear how one should interpret such a
mixed indexing operation.)

There have been occasional calls for a pure-python implementation of
numpy indexing for reference purposes. I think such a thing would be
fun to write, but I haven't had time.

Anne

> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



More information about the NumPy-Discussion mailing list