Home > Software engineering >  Argsort issue in multi-dimensional array in Python
Argsort issue in multi-dimensional array in Python

Time:07-03

I have arrays I1 (shape=(1, 10, 2)) and I2 (shape=(2,)). I am trying to sort using argsort() but I am getting an error for I2.

import numpy as np

I1=np.array([[[0, 1],
        [0, 3],
        [1, 2],
        [1, 4],
        [2, 5],
        [3, 4],
        [3, 6],
        [4, 7],
        [5, 4],
        [6, 7]]])

             
I2=np.array([[[0, 1],
        [0, 3],
        [1, 2],
        [1, 4],
        [2, 5],
        [3, 4],
        [3, 6],
        [4, 7],
        [5, 4],
        [6, 7]],
                [[0, 1],
                 [0, 3],
                 [1, 2],
                 [1, 4],
                 [2, 5],
                 [3, 4],
                 [3, 6],
                 [4, 7]]])             

order1 = I1[0,:, 1].argsort()
print("order1 =",[order1])
order2 = I2[0,:, 1].argsort()
print("order2 =",[order2])

The error is

in <module>
    order2 = I2[0,:, 1].argsort()

IndexError: too many indices for array: array is 1-dimensional, but 3 were indexed

CodePudding user response:

If you would print I2, you'll quickly see what is causing the problem:

array([list([[0, 1], [0, 3], [1, 2], [1, 4], [2, 5], [3, 4], [3, 6], [4, 7], [5, 4], [6, 7]]),
       list([[0, 1], [0, 3], [1, 2], [1, 4], [2, 5], [3, 4], [3, 6], [4, 7]])],
      dtype=object)

I2 is not a three dimensional array, but a one dimensional array of lists (each individual list consists of a list of 2-element lists).

In fact, when you create I2, with a recent NumPy, you should also see a DeprecationWarning:

VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  I2=np.array([[[0, 1],

which essentially identifies the same problem. Indeed, it states "from ragged nested sequences". Ragged is key here: your input outer list contains two lists that are not of equal length. As a result the three dimensional nested list is not of "rectangular" (box-shaped) dimensions, but it's a collection of list.

If you planned to you use your data this way with NumPy, you can't, really: NumPy is meant for (fast) operations with regular arrays, not with ragged arrays.

  • Related