【问题标题】:How to encode a feature which has a list of categorical values in each row for training an machine learning model?如何对每行中具有分类值列表的特征进行编码以训练机器学习模型?
【发布时间】:2019-11-21 01:03:20
【问题描述】:

我有一个数据集,其中有分类值列表作为特征值。如何对其进行编码以训练模型?

例如,我有一些数据:

feature1: [a, b, c]
feature2: [[category1, category2, category3], [category2], [category3, category4]]

如何编码feature2?

【问题讨论】:

  • 使用 one-hot 编码,将每个列表变成一个向量,其大小为类别总数,在所选类别上为一个,其他地方为零。

标签: python numpy machine-learning scikit-learn


【解决方案1】:

你可以使用LabelEncoder AND OneHotEncode :-

from sklearn.preprocessing import LabelEncoder,OneHotEncoder
labelencoder_x=LabelEncoder()
X[:, 0]=labelencoder_x.fit_transform(X[:,0])   
onehotencoder_x=OneHotEncoder(categorical_features=[0]) 
X=onehotencoder_x.fit_transform(X).toarray()

我想你可以从中得到这个想法。

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 2014-12-27
    • 2011-03-16
    • 2020-05-07
    • 2019-11-07
    • 2019-02-08
    相关资源
    最近更新 更多