首页 > 解决方案 > 如何将列表中的元素提取到numpy数组中

问题描述

所以我从用户那里获取要显示的图像数量,然后我运行一个 for 循环来输入每个图像的图像 ID。我将图像 ID 存储在列表中。

现在我想在一个 numpy 数组中使用列表中的元素(图像 ID)。我正在使用 VGG16 模型进行一些图像处理,并想在用户指定的图像 id 上测试模型。

这是代码:


i = int(input("\nEnter no. of images you want to view: "))
img_no_=[]
for j in range(i):
  img_no_.append(int(input("Enter image number (b/w 1-50): ")))

to_explain = X[[img_no_]]

我不断收到错误:

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.

因此尝试将图像 id 列表保存为元组和 numpy 数组,但在这两种情况下我都遇到了相同的错误。

to_ = numpy.asarray(img_no_)  
#print(to_explain)
#to_1 = tuple(img_no_)
to_explain = X[[tuple(to_)]]

如果您能帮助我避免出现此错误或向我展示满足要求的不同方法,那就太好了。

例如- img_no_ = [12,24,36] 现在我想 to_explain = X[[12,24,36]] (X 是张量)

编辑:')]]' 是一个错字

标签: pythonarraysnumpyimage-processing

解决方案


我不太确定“X”是什么,但我认为您只是想将列表传递为不同列表的第一个元素?

to_explain=X[img_no_]

应该可以正常工作。我认为您最初的错误可能是由于方括号和普通括号的混合造成的?


推荐阅读