Fixing Python Numpy TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array

Python Numpy TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array is an error which occurs when you use array indexing on a list.

In the blog post I attempt to explain why this error takes place and how you can solve it, I will also add other solutions that could solve the error if possible.

Exploring the Python Numpy TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array

This is an error which occurs when you use array indexing on a list.

You should avoid mixing between different errors. The error message should look like the error message bellow.

                                                                       #
TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
                                                                       #

Bellow we will describe how the error can be solved. With multiple possible solutions.

Solution 1 : convert the list to an array before using array indexing

We have established the fact that this error is the result of using array indexing on a list. Solving the problem becomes easy after you understand it, the solution is to convert the list to an array before using array indexing on it.

                                                                       #
myoutput = trainA[indices.astype(int)] # this is where I get the error
                                                                       #
                                                                       #
myoutput = np.array(trainA)[indices.astype(int)]
                                                                       #

You can avoid using np.array() and not perform the conversion completely, you have to use the method bellow.

                                                                       #
myoutput = [trainA[i] for i in indices]
                                                                       #

After you perform the conversion, the error should be gone. Try the solution bellow instead if the error persists.

Solution 2 : Use the .shape tuple

Another case where the error could take place is if you use

                                                                       #
np.indexA(np.random.rand(11,11))
                                                                       #

Using .shape will solve the error, shape is a tuple that gives you an indication of the number of dimensions in the array

                                                                       #
np.indexA(np.random.rand(11,11).shape)
                                                                       #

The shape attribute for numpy arrays returns the dimensions of the array which is exactly what we need.

The error should be solved after you try one of the two methods above. Thank you for reading.

Summing-up : 

This is the end of our article, I hope the solutions I presented worked for you and the error : Python Numpy TypeError only integer scalar arrays can be converted to a scalar index with 1D numpy indices array has been solved, Learning Python is a fun journey, do not let the errors discourage you.

Keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/