【问题标题】:TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices arrayTypeError:只有整数标量数组可以转换为具有 1D numpy 索引数组的标量索引
【发布时间】:2018-12-02 12:30:41
【问题描述】:

我想编写一个函数,根据提供的 bin 概率,从训练集中随机选择元素。我将设置的索引划分为 11 个 bin,然后为它们创建自定义概率

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

我收到以下错误:

TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array

我觉得这很奇怪,因为我已经检查了我创建的索引数组。它是一维,它是整数,它是标量

我错过了什么?

注意:我尝试将indicesastype(int) 一起传递。同样的错误。

【问题讨论】:

    标签: python python-3.x numpy


    【解决方案1】:

    可能导致此错误的另一种情况是

    >>> np.ndindex(np.random.rand(60,60))
    TypeError: only integer scalar arrays can be converted to a scalar index
    

    使用实际形状会修复它。

    >>> np.ndindex(np.random.rand(60,60).shape)
    <numpy.ndindex object at 0x000001B887A98880>
    

    【讨论】:

      【解决方案2】:

      检查您是否传递了正确的参数。与Simon 类似,当np.all 只接受一个数组时,我将两个数组传递给它,这意味着第二个数组被解释为轴。

      【讨论】:

        【解决方案3】:

        每当我以错误的方式使用np.concatenate 时都会出现此错误:

        >>> a = np.eye(2)
        >>> np.concatenate(a, a)
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
          File "<__array_function__ internals>", line 6, in concatenate
        TypeError: only integer scalar arrays can be converted to a scalar index
        

        正确的做法是将两个数组作为元组输入:

        >>> np.concatenate((a, a))
        array([[1., 0.],
               [0., 1.],
               [1., 0.],
               [0., 1.]])
        

        【讨论】:

        • 我落入了同样的陷阱。很容易忽略 np.concatenate() 的要求,即要连接的数组必须作为元组提供。非常感谢!
        【解决方案4】:

        产生此错误信息的简单案例:

        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
        

        一些可行的变体:

        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])
        

        基本的 python 列表索引比 numpy 的限制性更强:

        In [12]: [1,2,3,4,5][[1]]
        ....
        TypeError: list indices must be integers or slices, not list
        

        编辑

        再看

        indices = np.random.choice(range(len(X_train)), replace=False, size=50000, p=train_probs)
        

        indices 是一维整数数组 - 但它肯定不是标量。它是一个包含 50000 个整数的数组。列表不能同时使用多个索引进行索引,无论它们是在列表中还是在数组中。

        【讨论】:

          【解决方案5】:

          也许错误信息有些误导,但要点是 X_train 是一个列表,而不是一个 numpy 数组。您不能对其使用数组索引。先把它变成一个数组:

          out_images = np.array(X_train)[indices.astype(int)]
          

          【讨论】:

          • 我明白了。但是,如果列表太大而无法转换为数组怎么办?原来的 X_train 实际上是一个图像列表。
          • 这为我节省了大量的故障排除时间! (这是一个非常具有误导性的错误消息)
          • 如果坚持X_trainout_images 必须保留一个列表,则另一种方法是使用列表推导。 out_images = [X_train[index] for index in indices]
          猜你喜欢
          • 2021-10-31
          • 2018-04-04
          • 2021-07-15
          • 2021-01-27
          • 2019-04-26
          • 2017-12-15
          • 1970-01-01
          • 2022-01-03
          相关资源
          最近更新 更多