【问题标题】:Problem with Changing Categorical variable with OneHotEncoding使用 OneHotEncoding 更改分类变量的问题
【发布时间】:2019-07-01 23:51:21
【问题描述】:

我有一个性别列,其值为男性 (1) 和女性 (0)。当我使用以下代码对性别列进行 onehotencode 时:

onehotencoder = OneHotEncoder(categorical_features=['gender'])
data = onehotencoder.fit_transform(data).toarray()

我收到以下错误:

IndexError: arrays used as indices must be of integer (or boolean) type

【问题讨论】:

    标签: python python-3.x one-hot-encoding


    【解决方案1】:

    以下描述可以在 OneHotEncoder 的文档中找到:

    categorical_features : ‘all’ or array of indices or mask, default=’all’ Specify what features are treated as categorical. ‘all’: All features are treated as categorical. array of indices: Array of categorical feature indices. mask: Array of length n_features and with dtype=bool.

    因此,您应该传递列的索引,而不是传递列的名称,这应该可以解决您的问题。


    顺便说一句,请注意文档中所述的弃用:
    Deprecated since version 0.20: The categorical_features keyword was deprecated in version 0.20 and will be removed in 0.22. You can use the ColumnTransformer instead.

    【讨论】:

    • 当您帮助我回答问题时,我不知道支持投票功能。立即为答案投票。
    猜你喜欢
    • 1970-01-01
    • 2020-12-27
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    相关资源
    最近更新 更多