Last updated: Apr 12, 2024
Reading timeยท4 min
The NumPy "TypeError: only integer scalar arrays can be converted to a scalar index" occurs for 3 main reasons:
numpy.concatenate()
method with multiple, individual arrays.
instead of a tuple containing multiple arrays.numpy.ndindex()
method.Here is an example of how the error occurs.
import numpy as np arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # ๐๏ธ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(arr) print(type(arr)) # ๐๏ธ <class 'list'> indices = np.array([0, 2, 3]) # โ๏ธ TypeError: only integer scalar arrays can be converted to a scalar index print(arr[indices])
The arr
variable stores a native Python list.
You can use the type()
class to verify this as shown in the code sample.
The type class returns the type of an object.
numpy.array()
method to convert the Python list to a NumPy arrayTo solve the error in this case, we have to use the numpy.array() method to convert the Python list to an array.
import numpy as np arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # โ Convert the Python list to a NumPy array arr = np.array(arr) # ๐๏ธ [0 1 2 3 4 5 6 7 8 9] print(arr) print(type(arr)) # ๐๏ธ <class 'numpy.ndarray'> indices = np.array([0, 2, 3]) print(arr[indices]) # ๐๏ธ [0 2 3]
The code sample uses the numpy.array()
method to convert the Python list to an
array.
This enables us to use a NumPy array of indices to index the other NumPy array.
We could've also converted the list to an array right before using NumPy array indexing.
import numpy as np arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # ๐๏ธ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(arr) print(type(arr)) # ๐๏ธ <class 'list'> indices = np.array([0, 2, 3]) print(np.array(arr)[indices]) # ๐๏ธ [0 2 3]
We used the numpy.array()
method to convert the list to an array right before
using a NumPy array of indices.
numpy.concatenate()
method with multiple, individual arraysYou will also get the error if you call numpy.concatenate with multiple, individual arrays.
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) # โ๏ธ TypeError: only integer scalar arrays can be converted to a scalar index arr3 = np.concatenate(arr1, arr2) print(arr3)
The error in the example is called because we passed multiple, separate arrays
to the numpy.concatenate()
method.
Instead, you should call the numpy.concatenate()
method with a tuple
containing multiple arrays.
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) arr3 = np.concatenate((arr1, arr2)) # ๐๏ธ [1 2 3 4 5 6] print(arr3)
Notice that we have 2 sets of parentheses in the call to numpy.concatenate()
.
The first argument the numpy.concatenate()
method takes is a sequence of
arrays.
The method joins the supplied sequence of arrays along an existing axis.
The arrays in the tuple must have the same shape, except in the dimension
corresponding to the axis (the first axis (0
) by default).
numpy.ndindex()
Another cause of the error is when you pass an array instead of a shape to the numpy.ndindex() method.
Here is an example.
import numpy as np arr = np.array([ [1, 2], [3, 4], [5, 6], [7, 8] ]) # โ๏ธ TypeError: only integer scalar arrays can be converted to a scalar index print(np.ndindex(arr))
The numpy.ndindex()
method creates an N-dimensional iterator object to index
arrays.
The method takes a shape as a parameter.
You can use the array.shape
attribute to pass a shape to the method when
calling it.
import numpy as np arr = np.array([ [1, 2], [3, 4], [5, 6], [7, 8] ]) # <numpy.ndindex object at 0x7f800758fa60> print(np.ndindex(arr.shape))
The array.shape() method returns a tuple containing the array's dimensions.
import numpy as np arr = np.array([ [1, 2], [3, 4], [5, 6], [7, 8] ]) # (4, 2) print(arr.shape)
You can learn more about the related topics by checking out the following tutorials: