weird "too many indices for array" error in python

  • Last Update :
  • Techknowledgy :

Your b is a list of arrays:

In[84]: b = list(map(lambda x: np.arange(x, x + 10), np.arange(0, 5)))
In[85]: b
Out[85]: [array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
   array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
   array([2, 3, 4, 5, 6, 7, 8, 9, 10, 11]),
   array([3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
   array([4, 5, 6, 7, 8, 9, 10, 11, 12, 13])
]

When used as an index:

In[86]: np.arange(1000)[b] /
   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
   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
   IndexError: too many indices
for array

There are various ways we can use your b to index a 1d array:

In[87]: np.arange(1000)[np.hstack(b)]
Out[87]:
   array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7,
      8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 3, 4, 5, 6,
      7, 8, 9, 10, 11, 12, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
   ])

In[89]: np.arange(1000)[np.array(b)] # or np.vstack(b)
Out[89]:
   array([
      [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
      [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
      [2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
      [3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
      [4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
   ])

In[90]: np.arange(1000)[b, ] # 1 d tuple containing b
Out[90]:
   array([
      [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
      [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
      [2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
      [3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
      [4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
   ])

First of all, you're not slicing 0->9, 10->19, 20->29; your slices advance by 1 only: 0->9, 1->10, 11->20. Instead, try this:

n = 32
size = 10
b = list(map(lambda x: np.arange(x, x + size), np.arange(0, n * size, size)))

Suggestion : 2

Note that if b is a ragged list - one or anycodings_index-error more of the arrays is shorter, only the anycodings_index-error hstack version works.,There are various ways we can use your b anycodings_index-error to index a 1d array:,Your b is a list of arrays:,First of all, you're not slicing anycodings_index-error 0->9, 10->19, 20->29; your anycodings_index-error slices advance by 1 only: 0->9, anycodings_index-error 1->10, 11->20. Instead, try this:

Let's create a large np array 'a' with anycodings_python 10,000 entries

import numpy as np
a = np.arange(0, 10000)

Let's slice the array with 'n' indices anycodings_python 0->9, 1->10, 2->11, etc.

n = 32
b = list(map(lambda x: np.arange(x, x + 10), np.arange(0, n)))
c = a[b]

Your b is a list of arrays:

In[84]: b = list(map(lambda x: np.arange(x, x + 10), np.arange(0, 5)))
In[85]: b
Out[85]: [array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
   array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
   array([2, 3, 4, 5, 6, 7, 8, 9, 10, 11]),
   array([3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
   array([4, 5, 6, 7, 8, 9, 10, 11, 12, 13])
]

When used as an index:

In[86]: np.arange(1000)[b] /
   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
   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
   IndexError: too many indices
for array

There are various ways we can use your b anycodings_index-error to index a 1d array:

In[87]: np.arange(1000)[np.hstack(b)]
Out[87]:
   array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7,
      8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 3, 4, 5, 6,
      7, 8, 9, 10, 11, 12, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
   ])

In[89]: np.arange(1000)[np.array(b)] # or np.vstack(b)
Out[89]:
   array([
      [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
      [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
      [2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
      [3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
      [4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
   ])

In[90]: np.arange(1000)[b, ] # 1 d tuple containing b
Out[90]:
   array([
      [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
      [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
      [2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
      [3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
      [4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
   ])

First of all, you're not slicing anycodings_index-error 0->9, 10->19, 20->29; your anycodings_index-error slices advance by 1 only: 0->9, anycodings_index-error 1->10, 11->20. Instead, try this:

n = 32
size = 10
b = list(map(lambda x: np.arange(x, x + size), np.arange(0, n * size, size)))

Suggestion : 3

I am reading a file in python using pandas and then saving it in a numpy array. The file has the dimension of 11303402 rows x 10 columns. I need to split the data for cross validation and for that I sliced the data into 11303402 rows x 9 columns of examples and 1 array of 11303402 rows x 1 col of labels. The following is the code:, 2 days ago Nov 24, 2021  · The indexerror: too many indices for an array means that you have a declared an array in a different dimension and trying to index it in another dimension. For example, … , 1 day ago Apr 22, 2022  · The Python IndexError: too many indices for array occurs when we specify too many index values when accessing a one-dimensional numpy array. To solve the error, … ,The display of third-party trademarks and trade names on this site does not necessarily indicate any affiliation or endorsement of FaqCode4U.com.


tdata = pd.read_csv('train.csv') tdata.columns = 'Arrival_Time', 'Creation_Time', 'x', 'y', 'z', 'User', 'Model', 'Device', 'sensor', 'gt'] User_Data = np.array(tdata) features = User_Data[: , 0: 9] labels = User_Data[: , 9: 10]

Xt = features[idx[: , 0],: ]
1._
pets = ["cat", "dog", "hamster"]
pets = ["cat", "dog", "hamster"]
print(pets[3])
print(pets[3])
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - IndexError Traceback(most recent call last) 1 print(pets[3]) IndexError: list index out of range
import numpy as np  arr = np.array([[2,3,4,5,6], [8, 4, 3, 2, 1]])  print('3rd element on 1st row: ', arr[0,2])
3 rd element on 1 st row: 4
3rd element on 1st row:  4
import numpy as np x = np.array([45, 12, 55, 99, 10, 5, 2]) print(x[0, 3])

Suggestion : 4

Tell us what’s happening: How can I solve this Error? : IndexError: too many indices for array: array is 1-dimensional, but 3 were indexed,How can I solve this Error? :,If so, please edit your post to include it in the Tell us what’s happening section.,So yeah, you are telling the list to grab the entry on index (0,1,2) - which is a tuple and thus a 3D coordinate. Hence it fails.

if(len(list) !=9):

raise ValueError("List not contain nine numbers.")

else:

   ls = np.array(list)

   print(ls)

   mean_rows = [ls[(0, 1, 2)].mean(), ls[(3, 4, 5)].mean(), ls[(6, 7, 8)].mean()]

   mean_columns = [ls[(0, 3, 6)].mean(), ls[(1, 4, 7)].mean(), ls[(2, 5, 8)].mean()]

Suggestion : 5

What happens if we only supply one index to our array? It may be surprising that grades[0] does not throw an error since we are specifying only one index to access data from a 2-dimensional array. Instead, NumPy it will return all of the exam scores for student-0 (Ashley):,This is because NumPy will automatically insert trailing slices for you if you don’t provide as many indices as there are dimensions for your array. grades[0] was treated as grades[0, :].,The output of grades[:, :1] might look somewhat funny. Because the axis-1 slice only includes one column of numbers, the shape of the resulting array is (3, 1). 0 is thus only valid (non-negative) index for axis-1, since there is only one column to specify in the array.,Similar to Python’s sequences, we use 0-based indices and slicing to access the content of an array. However, we must specify an index/slice for each dimension of an array:

# A 0 - D array
np.array(8)

# A 1 - D array, shape - (3, )
np.array([2.3, 0.1, -9.1])

# A 2 - D array, shape - (3, 2)
np.array([
   [93, 95],
   [84, 100],
   [99, 87]
])

# A 3 - D array, shape - (2, 2, 2)
np.array([
   [
      [0, 1],
      [2, 3]
   ],

   [
      [4, 5],
      [6, 7]
   ]
])
>>>
import numpy as np

# A 3 - D array >>>
   x = np.array([
      [
         [0, 1],
         ...[2, 3]
      ],
      ...
      ...[
         [4, 5],
         ...[6, 7]
      ]
   ])

# get: sheet - 0, both rows, flip order of columns >>>
   x[0,: , ::-1]
array([
   [1, 0],
   [3, 2]
])
>>> simple_array = np.array([2.3, 0.1, -9.1])
+ -- -- -- + -- -- -- + -- -- -- +
|
2.3 | 0.1 | -9.1 |
   + -- -- -- + -- -- -- + -- -- -- +
   0 1 2 -
   3 - 2 - 1
>>> simple_array[0]
2.3

   >>>
   simple_array[-2]
0.1

   >>>
   simple_array[1: 3]
array([0.1, -9.1])

   >>>
   simple_array[3]
IndexError: index 3 is out of bounds
for axis 0 with size 3
# using a 1 - dimensional array to store the grades >>>
   grades = np.array([93, 95, 84, 100, 99, 87])