【发布时间】:2021-09-24 23:57:38
【问题描述】:
查看以下源代码:
import pandas as pd
from tensorflow.keras import layers, models
colors_df = pd.DataFrame(data=[[5,'yellow'],[1,'red'],[2,'blue'],[3,'green'],[4,'blue'],[7,'purple']], columns=['id', 'color'])
categorical_input = layers.Input(shape=(1,), dtype=tf.string)
one_hot_layer = OneHotEncodingLayer()
one_hot_layer.adapt(colors_df['color'].values)
encoded = one_hot_layer(categorical_input)
numeric_input = layers.Input(shape=(1,), dtype=tf.float32)
concat = layers.concatenate([numeric_input, encoded])
model = models.Model(inputs=[numeric_input, categorical_input], outputs=[concat])
predicted = model.predict([colors_df['id'], colors_df['color']])
print(predicted)
# [[5. 0. 1. 0. 0. 0.]
# [1. 0. 0. 1. 0. 0.]
# [2. 1. 0. 0. 0. 0.]
# [3. 0. 0. 0. 0. 1.]
# [4. 1. 0. 0. 0. 0.]
# [7. 0. 0. 0. 1. 0.]]
在上面的文章中,他们写道:
这个简单的网络只接受一个分类输入,One Hot Encode 对其进行编码,然后将 One Hot Encoded 特征与数字输入特征连接起来。请注意,我在 DataFrame 中添加了一个数字
id列,以说明如何从数字输入中拆分分类输入。
我没看懂。
为什么提供id 列以及这些 5 位单热代码?
它在整个应用程序中的用途是什么?
【问题讨论】:
标签: python tensorflow keras neural-network one-hot-encoding