【问题标题】:Substitute item in numpy array with list用列表替换numpy数组中的项目
【发布时间】:2018-01-15 15:03:02
【问题描述】:

我想替换一个由 0 和 1 组成的 numpy 数组中的一个项目。 我想用列表 [0,1] 替换 1,用列表 [1,0] 替换 0。

我找到了函数numpy.where(),但它不适用于列表。 是否有功能或类似的东西可以为numpy.where(vector==1,[0,1], [1,0]) 做等效的事情?

【问题讨论】:

  • 我感觉您正在尝试创建一个单热张量。这是目的吗?

标签: python arrays python-2.7 numpy


【解决方案1】:

简单的索引应该可以完成这项工作:

In [156]: x = np.array([0,1,0,0,1,1,0])
In [157]: y = np.array([[1,0],[0,1]])
In [158]: y[x]
Out[158]: 
array([[1, 0],
       [0, 1],
       [1, 0],
       [1, 0],
       [0, 1],
       [0, 1],
       [1, 0]])

为了确保这是一个通用的解决方案,而不是一些“布尔”侥幸

In [162]: x = np.array([0,1,0,0,1,2,2])
In [163]: y = np.array([[1,0],[0,1],[1,1]])
In [164]: y[x]
Out[164]: 
array([[1, 0],
       [0, 1],
       [1, 0],
       [1, 0],
       [0, 1],
       [1, 1],
       [1, 1]])

【讨论】:

  • 这是 OP 正在寻找的答案,而不是他/她接受的答案。总之,很整洁!
【解决方案2】:

如果您愿意,您实际上可以使用 where

>>> import numpy as np
>>> vector = np.random.randint(0, 2, (8, 8))
>>> vector
array([[1, 0, 0, 0, 0, 0, 1, 1],
       [1, 1, 0, 1, 0, 0, 0, 1],
       [1, 0, 0, 1, 1, 1, 0, 0],
       [1, 0, 1, 1, 0, 0, 0, 0],
       [0, 1, 0, 1, 1, 1, 1, 1],
       [1, 0, 1, 1, 0, 0, 0, 0],
       [0, 1, 1, 0, 1, 1, 0, 0],
       [1, 1, 1, 1, 1, 1, 0, 0]])
>>> np.where(vector[..., None] == 1, [0,1], [1,0])
# btw. if vector has only 0 and 1 entries you can leave out the " == 1 "
array([[[0, 1],
        [1, 0],
        [1, 0],
        [1, 0],
        [1, 0],
        [1, 0],
        [0, 1],
        [0, 1]],

       [[0, 1],
        [0, 1],
        [1, 0],
        [0, 1],
        [1, 0],
        [1, 0],
        [1, 0],

        etc.

【讨论】:

    【解决方案3】:

    看起来你想制作one-hot vector encoding

    a = np.array([1, 0, 1, 1, 0])  # initial array
    b = np.zeros((len(a), 2))  # where 2 is number of classes
    b[np.arange(len(a)), a] = 1
    print(b)
    

    结果:

    array([[ 0.,  1.],
           [ 1.,  0.],
           [ 0.,  1.],
           [ 0.,  1.],
           [ 1.,  0.]])
    

    【讨论】:

    • 巧合。我的数组中有很多元素。有没有办法让它不那么“昂贵”?
    • 无论如何,您都需要创建辅助数组来存储结果,因为 numpy 不允许动态更改特定项目的尺寸。所以我的解决方案必须适用于您的问题(如果是 1-rank 输入数组)
    【解决方案4】:

    由于不可能在 NumPy 中动态增长数组,因此您必须将数组转换为列表,根据需要进行替换,然后将结果转换回 NumPy 数组,例如:

    In [21]: vector = np.array([0, 0, 1, 0, 1])
    
    In [22]: vlist = vector.tolist()
    
    In [23]: vlist
    Out[23]: [0, 0, 1, 0, 1]
    
    In [24]: new_list = [[1, 0] if el == 0 else [0, 1] for idx, el in enumerate(vlist)]
    
    In [25]: result = np.array(new_list)
    
    In [26]: result
    Out[26]: 
    array([[1, 0],
           [1, 0],
           [0, 1],
           [1, 0],
           [0, 1]])
    

    如果您的一维数组不够大,那么列表推导式方法并不是那么糟糕。

    【讨论】:

      【解决方案5】:

      hpaulj 的回答将是这样做的理想方式。另一种方法是重塑您的原始 numpy 数组,并应用您的标准 np.where() 操作。

      import numpy as np
      
      x = np.array([0,1,0,0,1,1,0])
      x = np.reshape(x,(len(x),1))
      # Equivalently, if you want to be explicit:
      # x = np.array([[e] for e in x])
      print np.where(x==1,[1,0],[0,1])
      # Result:
      array([[0, 1],
             [1, 0],
             [0, 1],
             [0, 1],
             [1, 0],
             [1, 0],
             [0, 1]])
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-25
        • 1970-01-01
        • 2014-06-28
        • 2020-05-29
        • 2019-12-02
        • 2012-11-14
        • 1970-01-01
        • 2022-07-18
        相关资源
        最近更新 更多