4

I am using an ndarray to slice another ndarray. Normally I use arr[ind_arr]. numpy seems to not like this and raises a FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated use arr[tuple(seq)] instead of arr[seq].

What's the difference between arr[tuple(seq)] and arr[seq]?

Other questions on StackOverflow seem to be running into this error in scipy and pandas and most people suggest the error to be in the particular version of these packages. I am running into the warning running purely in numpy.

Example posts:

FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated use `arr[tuple(seq)]` instead of `arr[seq]`

FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated use `arr[tuple(seq)]`

FutureWarning with distplot in seaborn

MWE reproducing warning:

import numpy as np

# generate a random 2d array
A = np.random.randint(20, size=(7,7))
print(A, '\n')

# define indices
ind_i = np.array([1, 2, 3])  # along i
ind_j = np.array([5, 6])  # along j

# generate index array using meshgrid
ind_ij = np.meshgrid(ind_i, ind_j, indexing='ij')

B = A[ind_ij]
print(B, '\n')

C = A[tuple(ind_ij)]
print(C, '\n')

# note: both produce the same result
krg
  • 317
  • 3
  • 11

2 Answers2

3

meshgrid returns a list of arrays:

In [50]: np.meshgrid([1,2,3],[4,5],indexing='ij')                                                      
Out[50]: 
[array([[1, 1],
        [2, 2],
        [3, 3]]), array([[4, 5],
        [4, 5],
        [4, 5]])]
In [51]: np.meshgrid([1,2,3],[4,5],indexing='ij',sparse=True)                                          
Out[51]: 
[array([[1],
        [2],
        [3]]), array([[4, 5]])]

ix_ does the same thing, but returns a tuple:

In [52]: np.ix_([1,2,3],[4,5])                                                                         
Out[52]: 
(array([[1],
        [2],
        [3]]), array([[4, 5]]))

np.ogrid also produces the list.

In [55]: arr = np.arange(24).reshape(4,6)                                                              

indexing with the ix tuple:

In [56]: arr[_52]                                                                                      
Out[56]: 
array([[10, 11],
       [16, 17],
       [22, 23]])

indexing with the meshgrid list:

In [57]: arr[_51]                                                                                      
/usr/local/bin/ipython3:1: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  #!/usr/bin/python3
Out[57]: 
array([[10, 11],
       [16, 17],
       [22, 23]])

Often the meshgrid result is used with unpacking:

In [62]: I,J = np.meshgrid([1,2,3],[4,5],indexing='ij',sparse=True)                                    
In [63]: arr[I,J]                                                                                      
Out[63]: 
array([[10, 11],
       [16, 17],
       [22, 23]])

Here [I,J] is the same as [(I,J)], making a tuple of the 2 subarrays.

Basically they are trying to remove a loophole that existed for historical reasons. I don't know if they can change the handling of meshgrid results without causing further compatibility issues.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • Its useful to know the alternatives, thanks. So do we know how the results would be different (as indicated by the `FutureWarning ...which will result either in an error or a different result`)? And otherwise should I try to stick to tuples or unpacked indices as opposed to `arr[seq]`? – krg Jun 13 '19 at 18:16
  • It may try `arr[np.array(idx),...]`, turning the meshgrid list into an array that indexes just the first dimension. – hpaulj Jun 13 '19 at 18:29
  • Did I missing something? `arr[_51]`, `arr[_52]` and `arr[I, J]` give identical results, so where is the difference? – Jason Feb 07 '20 at 01:46
  • @Jason, yes they produce the same thing (except for the warning). That was my point. – hpaulj Feb 07 '20 at 02:51
0

For Future Readers having this FutureWarning: ... and want to correctly resolve them.

  1. Read the answer of @hpaulj here. And notice the point is when he(she) said:

    [...] returns a list of [...]

    [...] returns a tuple of [...]

  2. If you don't know why 1. is the point please read the answer: https://stackoverflow.com/a/71487259/5290519, which is also written by him(her). This answer provides:

    • Why, in the first place, the usage of list as index will cause a warning.

      [4] is a problem because in the past, certain lists were interpreted as though they were tuples. This is a legacy case that developers are trying to cleanup, hence the FutureWarning.

    • A series of concise examples on the (correct) usage of list,tuple,np.array as array index.
  3. If you still don't get the point, try my own answer after I figuring all these complicated concepts: https://stackoverflow.com/a/71493474/5290519

NeoZoom.lua
  • 2,269
  • 4
  • 30
  • 64