【问题标题】:How to convert one-hot encodings into integers?如何将 one-hot 编码转换为整数?
【发布时间】:2023-03-13 11:50:01
【问题描述】:

我有一个形状为 (100,10) 的 numpy 数组数据集。每一行都是一个单热编码。我想将其转换为形状为 (100,) 的 nd 数组,以便将每个向量行转换为表示非零索引的索引的整数。有没有使用 numpy 或 tensorflow 的快速方法?

【问题讨论】:

  • 所以你要解码每个行向量?你在寻找类似np.argmax() 的东西吗?如果您描述了解码的目的,将会很有帮助。

标签: python numpy tensorflow


【解决方案1】:

您可以使用numpy.argmaxtf.argmax。示例:

import numpy as np  
a  = np.array([[0,1,0,0],[1,0,0,0],[0,0,0,1]])
print('np.argmax(a, axis=1): {0}'.format(np.argmax(a, axis=1)))

输出:

np.argmax(a, axis=1): [1 0 3]

您可能还想查看sklearn.preprocessing.LabelBinarizer.inverse_transform

【讨论】:

  • Argmax 适用于这个例子,因为在 one-hot 中,只有一个 1,其余都是零。对于在 np n-darray 中查找特定值的一般情况,OP 可以使用 np.where。干杯!
【解决方案2】:

正如 Franck Dernoncourt 所指出的,由于 one hot 编码只有一个 1,其余都是 0,因此您可以在此特定示例中使用 argmax。一般来说,如果你想在一个 numpy 数组中找到一个值,你可能会想咨询numpy.where。另外,这个堆栈交换问题:

Is there a NumPy function to return the first index of something in an array?

由于 one-hot 向量是一个全 0 和一个 1 的向量,您可以这样做:

>>> import numpy as np
>>> a = np.array([[0,1,0,0],[1,0,0,0],[0,0,0,1]])
>>> [np.where(r==1)[0][0] for r in a]
[1, 0, 3]

这只是建立一个索引列表,每行为 1。 [0][0] 索引只是为了抛弃 np.where 返回的结构(一个带有数组的元组),这超出了您的要求。

对于任何特定的行,您只想索引到 a。例如,在第 0 行中,1 在索引 1 中找到。

>>> np.where(a[0]==1)[0][0]
1

【讨论】:

    【解决方案3】:

    只需使用np.argmax(x, axis=1)

    例子:

    import numpy as np
    array = np.array([[0, 1, 0, 0], [0, 0, 0, 1]])
    print(np.argmax(array, axis=1))
    > [1 3]
    

    【讨论】:

    • 这与三年前弗兰克的高票答案相同。
    【解决方案4】:

    虽然我强烈建议使用 numpy 来提高速度,但mpu.ml.one_hot2indices(one_hots) 展示了如何在没有 numpy 的情况下做到这一点。只需pip install mpu --user --upgrade

    那你就可以了

    >>> one_hot2indices([[1, 0], [1, 0], [0, 1]])
    [0, 0, 1]
    

    【讨论】:

      【解决方案5】:

      我在这些情况下所做的就是这样。这个想法是将 one-hot 向量解释为 1,2,3,4,5... 数组的索引。

      # Define stuff
      import numpy as np
      one_hots = np.zeros([100,10])
      for k in range(100):
          one_hots[k,:] = np.random.permutation([1,0,0,0,0,0,0,0,0,0])
      
      # Finally, the trick
      ramp = np.tile(np.arange(0,10),[100,1])
      integers = ramp[one_hots==1].ravel()
      

      我更喜欢这个技巧,因为我觉得np.argmax 和其他建议的解决方案可能比索引慢(尽管索引可能会消耗更多内存)

      【讨论】:

        【解决方案6】:
        def int_to_onehot(n, n_classes):
            v = [0] * n_classes
            v[n] = 1
            return v
        
        def onehot_to_int(v):
            return v.index(1)
        
        
        >>> v = int_to_onehot(2, 5)
        >>> v
        [0, 0, 1, 0, 0]
        
        
        >>> i = onehot_to_int(v)
        >>> i
        2
        

        【讨论】:

        • onehot_to_int 仅适用于列表,不适用于多维数组。
        【解决方案7】:

        你可以使用这个简单的代码:

        a=[[0,0,0,0,0,1,0,0,0,0]]
        j=0
        for i in a[0]:
            if i==1:
                print(j)
            else:
                j+=1
        

        5

        【讨论】:

          猜你喜欢
          • 2018-01-26
          • 1970-01-01
          • 2022-01-22
          • 2019-09-27
          • 1970-01-01
          • 2017-07-27
          • 2022-01-19
          • 2020-11-21
          • 1970-01-01
          相关资源
          最近更新 更多