【问题标题】:Insert one-hot along with other data types与其他数据类型一起插入 one-hot
【发布时间】:2021-12-10 05:46:31
【问题描述】:

我在一行中有具有数字和分类类型属性的数据。归一化后的数值将在 0 - 1 的范围内,浮点型。还有一些连续的数据,也是浮点型的。 如何将 floatone-hot-vector(分类)数据作为一行数据插入到 Keras 层中?

预期的数据行示例:

[float, float, float, [one-hot-vector]]

[0.1, 0.2, 0.4, [0 0 0 1]]

【问题讨论】:

  • 只需将它们连接起来。
  • 一个热向量实际上只是代表一堆特征,所以你可以把它们正常地放在[0.1, 0.2, 0.4, 0, 0, 0, 1]中。 pandas 库有一个名为 get_dumies 的函数,它应该可以帮助您。
  • 跟随博士。史努比的建议 - 串联起到了作用。

标签: python machine-learning keras one-hot-encoding


【解决方案1】:

您可以使用 Get Dummies 或 Categorical Codes。

import pandas as pd
 
# Intitialise data of lists
data = [{'Year': 2020, 'Airport':2000, 'Casino':5000, 'Stadium':9000, 'Size':'Small'}, 
       {'Year': 2019, 'Airport':3000, 'Casino':4000, 'Stadium':12000, 'Size':'Medium'},
       {'Year': 2018, 'Airport':5000, 'Casino':9000, 'Stadium':10000, 'Size':'Medium'},
       {'Year': 2017, 'Airport':5000, 'Casino':10000, 'Stadium':15000, 'Size':'Large'}]
df = pd.DataFrame(data)


df = df.set_index(['Year'])
df

df_fin = pd.DataFrame({col: df[col].astype('category').cat.codes for col in df}, index=df.index)
df_fin

One-Hot-Encoding 的优点是结果是二进制而不是序数,并且所有内容都位于正交向量空间中。缺点是对于高基数,特征空间真的会很快爆炸,你开始与维度的诅咒作斗争。您可以在 OHE 之后使用 PCA!

【讨论】:

    猜你喜欢
    • 2017-11-06
    • 2018-07-10
    • 1970-01-01
    • 2020-10-31
    • 2019-10-11
    • 2022-01-02
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多