![](/img/trans.png)
[英]TypeError: only integer scalar arrays can be converted to a scalar index in python
[英]Python TypeError : only integer scalar arrays can be converted to a scalar index
我为这个问题提供了两个数组的简单示例: labelArray
是一个一维数组,其索引处的标签与nD数组someValuesArray
的相同索引相对应。 我得到了标签2出现的所有索引,并希望检索someValuesArray
的值。
labelArray = [2,0,1,2]
someValuesArray= [array([[ 2.15072 , 2.12438 , 2.27047 , 2.64567 ,
2.22976 , 2.18186 ]], dtype=float32),
array([ [ 2.29442, 2.3087 , 2.3111 , 2.1962 , 2.23694, 2.16988]], dtype=float32)),
array([[2.82851 , 2.73032 , 2.78301 , 1.71722 , 1.81542 , 1.78189 ]], dtype=float32)),
array([[ 1.19271, 1.14721, 1.27894 , 1.16637, 1.23343, 1.21666]], dtype=float32)]
因此,如果我想要标签2,我会得到索引0和3,这应该给我相应数组someValuesArray
索引0和3的值。
但是,当我想调用函数时,会收到TypeError @ array[indices]
。
TypeError: only integer scalar arrays can be converted to a scalar index
我的功能:
def searchValues(array, value):
labelArray = [2,0,1,2]
values_of_array = np.array(labelArray)
indices = np.where(values_of_array == value)[0]
return array[indices]
searchValues(someValuesArray,2)
如评论中所述, someValuesArray
是2d numpy数组的列表 。 我已经将其转换为np.array
。 您问题中的代码示例尝试使用numpy数组索引python列表,这会导致您收到错误消息。
In [111]: a=np.array(someValuesArray) # Convert to a numpy array
In [112]: a
Out[112]:
array([[[ 2.15071988, 2.12438011, 2.2704699 , 2.64566994, 2.22975993, 2.18185997]],
[[ 2.29442 , 2.30870008, 2.31110001, 2.19619989, 2.23693991, 2.16987991]],
[[ 2.82851005, 2.73031998, 2.78301001, 1.71721995, 1.81542003, 1.78189003]],
[[ 1.19271004, 1.14721 , 1.27893996, 1.16637003, 1.23343003, 1.21666002]]], dtype=float32)
In [113]: def searchValues(array, value):
labelArray = [2,0,1,2]
values_of_array = np.array(labelArray)
indices = np.where(values_of_array == value)[0]
# print statements added to see what's happening
print("Indices: ", indices)
print("Array selection: \n", array[indices])
return array
searchValues(a,2)
[Out]
Indices: [0 3]
Array selection:
[[[ 2.15071988 2.12438011 2.2704699 2.64566994 2.22975993 2.18185997]] # a[0]
[[ 1.19271004 1.14721 1.27893996 1.16637003 1.23343003 1.21666002]]] # a[3]
Out[113]:
array(
[[[ 2.15071988, 2.12438011, 2.2704699 , 2.64566994, 2.22975993, 2.18185997]],
[[ 2.29442 , 2.30870008, 2.31110001, 2.19619989, 2.23693991, 2.16987991]],
[[ 2.82851005, 2.73031998, 2.78301001, 1.71721995, 1.81542003, 1.78189003]],
[[ 1.19271004, 1.14721 , 1.27893996, 1.16637003, 1.23343003, 1.21666002]]], dtype=float32)
indices = np.where(values_of_array == value)[0]
返回的两个索引都用于指向数组中的行。
您已经从函数中返回了整个数组:您真的要返回array[indices]
吗?
我可以通过以下方式重现您的错误消息:
列出具有标量数组的索引:
In [7]: [1,2,3,4][np.array(1)]
Out[7]: 2
具有(1,)形状数组的列表索引; 一个元素但不是标量数组:
In [8]: [1,2,3,4][np.array([1])]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-4ad73b219fa3> in <module>()
----> 1 [1,2,3,4][np.array([1])]
TypeError: only integer scalar arrays can be converted to a scalar index
但是索引这样的数组是可以的:
In [9]: np.array([1,2,3,4])[np.array([1])]
Out[9]: array([2])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.