一种使用numpy的热编码 [英] One Hot Encoding using numpy

查看:194
本文介绍了一种使用numpy的热编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果输入为零,我想创建一个看起来像这样的数组:

If the input is zero I want to make an array which looks like this:

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

,如果输入为5:

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

对于上述内容,我写道:

For the above I wrote:

np.put(np.zeros(10),5,1)

但是没有用.

有什么方法可以在一排中实现?

Is there any way in which, this can be implemented in one line?

推荐答案

通常,当您希望在机器学习中获得一种用于分类的单编码时,您将拥有一个索引数组.

Usually, when you want to get a one-hot encoding for classification in machine learning, you have an array of indices.

import numpy as np
nb_classes = 6
targets = np.array([[2, 3, 4, 0]]).reshape(-1)
one_hot_targets = np.eye(nb_classes)[targets]

one_hot_targets现在是

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

.reshape(-1)可以确保使用正确的标签格式(您可能还具有[[2], [3], [4], [0]]). -1是一个特殊值,表示将所有剩余的物料放入此维".因为只有一个,所以它使数组变平.

The .reshape(-1) is there to make sure you have the right labels format (you might also have [[2], [3], [4], [0]]). The -1 is a special value which means "put all remaining stuff in this dimension". As there is only one, it flattens the array.

def get_one_hot(targets, nb_classes):
    res = np.eye(nb_classes)[np.array(targets).reshape(-1)]
    return res.reshape(list(targets.shape)+[nb_classes])

包裹

您可以使用 mpu.ml.indices2one_hot .经过测试且易于使用:

Package

You can use mpu.ml.indices2one_hot. It's tested and simple to use:

import mpu.ml
one_hot = mpu.ml.indices2one_hot([1, 3, 0], nb_classes=5)

这篇关于一种使用numpy的热编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆