'TypeError: only integer scalar arrays can be converted to a scalar index (object detection)

I am struggling with this one part. Not sure how to fix it! Would be great if someone could tell me what I need to fix in the code. Down below is the code & error message that I'm receiving. This it the code:

categoriesList=["airplane","automobile","bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck"]

import matplotlib.pyplot as plt
import random
def plotImages(x_test, images_arr, labels_arr, n_images=8):
    fig, axes = plt.subplots(n_images, n_images, figsize=(9,9))
    axes = axes.flatten()

for i in range(100):
    rand = random.randint(0, x_test.shape[0] -1)
    img = images_arr[rand]
    ax = axes[i]

    ax.imshow( img, cmap="Greys_r")
    ax.set_xticks(())
    ax.set_yticks(())
    
    sample = x_test[rand].reshape((1,32,32,3))
    predict_x = model2000.predict(sample)
    label=categoriesList[predict_x[0]]  
    
    if labels_arr[rand][predictions[0]] == 0:
        ax.set_title(label, fontsize=18 - n_images, color="red")
    else:
        ax.set_title(label, fontsize=18 - n_images) 
    
plot = plt.tight_layout()
return plot

display (plotImages(x_test, data_test_picture, y_test, n_images=10))

This is the error message:

TypeError: only integer scalar arrays can be converted to a scalar index
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<command-2104322840429397> in <module>
     28     return plot
     29 
---> 30 display (plotImages(x_test, data_test_picture, y_test, n_images=10))

<command-2104322840429397> in plotImages(x_test, images_arr, labels_arr, n_images)
     18         sample = x_test[rand].reshape((1,32,32,3))
     19         predict_x = model2000.predict(sample)
---> 20         label=categoriesList[predict_x[0]]
     21 
     22         if labels_arr[rand][predictions[0]] == 0:

TypeError: only integer scalar arrays can be converted to a scalar index

The output i'm getting:



Solution 1:[1]

To fix the integer scalar arrays can be converted to a scalar index error

  1. Concatenate array by list Here we have 2 array we have to convert into list using the numpy.concatenate() like numpy.concatenate([ar1, ar2])
        import numpy 
        # Create 2 different arrays 
        ar1 = numpy.array(['Apple',  'Orange',  'Banana',  'Pineapple',  'Grapes']) 
        ar2 = numpy.array(['Onion',  'Potato'])  
        # Concatenate array ar1 & ar2 using numpy.concatenate() 
        ar3 = numpy.concatenate([ar1, ar2])  print(ar3)  
# Output  
['Apple'  'Orange'  'Banana'  'Pineapple'  'Grapes'  'Onion'  'Potato']
  1. Concatenate array by Tuple Convert array 1 and array 2 to tuple using the numpy.concatenate() like numpy.concatenate((ar1, ar2))
        import numpy 
        # Create 2 different arrays 
        ar1 = numpy.array(['Apple',  'Orange',  'Banana',  'Pineapple',  'Grapes']) 
        ar2 = numpy.array(['Onion',  'Potato'])  
        # Concatenate array ar1 & ar2 using numpy.concatenate() 
        ar3 = numpy.concatenate((ar1, ar2))  print(ar3)  
# Output  
['Apple'  'Orange'  'Banana'  'Pineapple'  'Grapes'  'Onion'  'Potato']

If you use the plain array and perform some indexing operation it will show the same error. To overcome this you can convert the ordinary array into a NumPy array and then perform the required operation.

categoriesList=numpy.array(["airplane","automobile","bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck"])

Refer here for more information

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SuryasriKamini-MT