我有一个数据集,其中有分类值列表作为特征值。如何对其进行编码以训练模型?
例如,我有一些数据,例如:
feature1: [a, b, c]
feature2: [[category1, category2, category3], [category2], [category3, category4]]
如何编码特征2?
我有一个数据集,其中有分类值列表作为特征值。如何对其进行编码以训练模型?
例如,我有一些数据,例如:
feature1: [a, b, c]
feature2: [[category1, category2, category3], [category2], [category3, category4]]
如何编码特征2?
您可以使用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()
我想从这里你可以得到这个想法。