问题描述
错误消息可能在某种程度上具有误导性,但要点是,它X_train
是一个列表,而不是一个numpy数组。您不能在其上使用数组索引。首先使其成为数组:
out_images = np.array(X_train)[indices.astype(int)]
解决方法
我想编写一个函数,根据提供的 bin概率 从训练集中随机选择元素。我 将集合索引划分为11个bin ,然后为它们创建 自定义概率 。
bin_probs = [0.5,0.3,0.15,0.04,0.0025,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
我收到以下错误:
TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
我发现这很奇怪,因为我已经检查了创建的索引数组。它是 一维 ,是 整数 ,并且是 标量 。
我想念什么?
注:我试图通过indices
使用astype(int)
。同样的错误。