Python 类型错误:只有整数标量数组可以转换为具有一维 numpy 索引数组的标量索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50997928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
提问by 3yanlis1bos
I want to write a function that randomly picks elements from a training set, based on the bin probabilitiesprovided. I divide the set indices to 11 bins, then create custom probabilitiesfor them.
我想编写一个函数,根据提供的bin 概率从训练集中随机选择元素。我将集合索引划分为 11 个 bins,然后为它们创建自定义概率。
bin_probs = [0.5, 0.3, 0.15, 0.04, 0.0025, 0.0025, 0.001, 0.001, 0.001, 0.001, 0.001]
X_train = list(range(2000000))
train_probs = bin_probs * int(len(X_train) / len(bin_probs)) # extend probabilities across bin elements
train_probs.extend([0.001]*(len(X_train) - len(train_probs))) # a small fix to match number of elements
train_probs = train_probs/np.sum(train_probs) # normalize
indices = np.random.choice(range(len(X_train)), replace=False, size=50000, p=train_probs)
out_images = X_train[indices.astype(int)] # this is where I get the error
I get the following error:
我收到以下错误:
TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
I find this weird, since I already checked the array of indices that I have created. It is 1-D, it is integer, and it is scalar.
我觉得这很奇怪,因为我已经检查了我创建的索引数组。它是1-D,它是integer,它是scalar。
What am I missing?
我错过了什么?
Note : I tried to pass indices
with astype(int)
. Same error.
注:我试图通过indices
使用astype(int)
。同样的错误。
回答by DYZ
Perhaps the error message is somewhat misleading, but the gist is that X_train
is a list, not a numpy array. You cannot use array indexing on it. Make it an array first:
也许错误消息有点误导,但要点是这X_train
是一个列表,而不是一个 numpy 数组。您不能对其使用数组索引。首先使它成为一个数组:
out_images = np.array(X_train)[indices.astype(int)]
回答by hpaulj
A simple case that generates this error message:
生成此错误消息的简单案例:
In [8]: [1,2,3,4,5][np.array([1])]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-55def8e1923d> in <module>()
----> 1 [1,2,3,4,5][np.array([1])]
TypeError: only integer scalar arrays can be converted to a scalar index
Some variations that work:
一些有效的变体:
In [9]: [1,2,3,4,5][np.array(1)] # this is a 0d array index
Out[9]: 2
In [10]: [1,2,3,4,5][np.array([1]).item()]
Out[10]: 2
In [11]: np.array([1,2,3,4,5])[np.array([1])]
Out[11]: array([2])
Basic python list indexing is more restrictive than numpy's:
基本的 python 列表索引比 numpy 的更严格:
In [12]: [1,2,3,4,5][[1]]
....
TypeError: list indices must be integers or slices, not list
edit
编辑
Looking again at
再看
indices = np.random.choice(range(len(X_train)), replace=False, size=50000, p=train_probs)
indices
is a 1d array of integers - but it certainly isn't scalar. It's an array of 50000 integers. List's cannot be indexed with multiple indices at once, regardless of whether they are in a list or array.
indices
是一维整数数组 - 但它肯定不是标量。它是一个包含 50000 个整数的数组。列表不能同时用多个索引建立索引,无论它们是在列表还是数组中。