TypeError:只有整数标量阵列才能转换为标量索引(对象检测)

发布于 2025-01-24 03:26:38 字数 1964 浏览 1 评论 0原文

我在这一部分方面挣扎。不确定如何修复它!如果有人能告诉我我需要在代码中修复什么,那就太好了。下面是代码&我收到的错误消息。 这是代码:

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))

这是错误消息:

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

我正在获得的输出:

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:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏闹i 2025-01-31 03:26:38

要修复整数标量阵列,可以将标量索引错误转换为

  1. list 的连接阵列
    在这里,我们有2个数组,必须使用 numpy.concatenate() 喜欢 numpy.concatenate([[ar1,ar2])< /code>
        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. tuple 的串联阵列
    使用 numpy.concatenate() 喜欢 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']

如果您使用纯数组并执行一些索引操作,则会显示出相同的错误。为了克服这一点,您可以将普通数组转换为numpy阵列,然后执行所需的操作。

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

请参阅在这里有关更多信息

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文