Python TypeError:只有整数标量数组可以转换为标量索引 [英] Python TypeError : only integer scalar arrays can be converted to a scalar index

查看:93
本文介绍了Python TypeError:只有整数标量数组可以转换为标量索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为这个问题提供了两个数组的简单示例: labelArray 是一维数组,其索引处的标签与nD数组的相同索引相对应. someValuesArray .我得到了标签2出现的所有索引,并希望检索 someValuesArray 中的值.

I have made for this question an easier example of two arrays: The labelArray is a 1D-array has labels at indices which corresponds to the same indices of the nD-array someValuesArray. I get all indices where label 2 occurs and want to retrieve the values in 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的值.

So if I want label 2, I get indices 0 and 3, which should give me the values of the indices 0 and 3 in the corresponding array someValuesArray.

但是当我想调用函数时,会收到TypeError @ array [indices] .

But I receive a TypeError @ array[indices] when I want to call my function.

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列表,这会导致您收到错误消息.

As mentioned in the comments, someValuesArray is a list of 2d numpy arrays. I've converted that to an np.array. The code sample in your question attempts to index a python list with a numpy array, which causes the error message you receive.

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] 返回的两个索引均用于指向数组中的行.

Both indices returned by indices = np.where(values_of_array == value)[0] are used to point to rows in the array.

您已经从函数中返回了整个数组:您真的要返回 array [indices] 吗?

You have returned the entire array from the function: did you really mean to return array[indices]?

这篇关于Python TypeError:只有整数标量数组可以转换为标量索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆