且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在神经网络中使用一种热编码矢量输入标签

更新时间:2022-12-17 10:05:13

您实际上没有进行转换.您只创建了一个3x3的单位矩阵one_hot_targets,但从未使用过它.结果,batch_ydf["target"]的数组:

You actually didn't do the conversion. You've only created a 3x3 identity matrix one_hot_targets, but never used it. As a result, batch_y is an array of df["target"]:

target = df["target"]
l = target.values.tolist()
l = np.array(l)
...
batch_y = np.expand_dims(l, axis=0)  # Has shape `(1, 169307)`!

您的batch_x似乎也不正确,但是feature并未在代码段中定义,因此我无法确切地说出它是什么.

Your batch_x also doesn't seem correct, but the feature is not defined in the snippet, so I can't say what exactly that is.

[更新] 如何进行一键编码:

from sklearn.preprocessing import OneHotEncoder

# Categorical target: 0, 1 or 2. The value is just an example
target = np.array([1, 2, 2, 1, 0, 2, 1, 1, 0, 2, 1])

target = target.reshape([-1, 1])      # add one extra dimension
encoder = OneHotEncoder(sparse=False)
encoder.fit(target)
encoded = encoder.transform(target)   # now it's one-hot: [N, 3]