我有两个不同的csv文件,其中一个是训练数据,另一个是测试数据。我从这些train_features_df和test_features_df中创建了两个不同的数据框(dataframe)。请注意,测试和训练数据具有多个分类列,因此我需要对它们应用labelEncoder,因为它适用于我的数据集。所以我分别在训练和测试数据上应用了label encoder。当我打印出新编码的训练和测试数据集的值时,我发现对于相同特征的相同分类值,来自新编码数据的输出是不同的。这是否意味着我必须合并训练和测试数据?然后再应用标签编码,最后再将它们分开?
from sklearn.preprocessing import LabelEncoder
target=train_features_df['y']
train_features_df=train_features_df.drop(['y'], axis=1)
train_features_df.head()
y = target.values
print("printing feature column of train datasets: \n")
print(train_features_df.values)
le=LabelEncoder()
X_train_label_encoded=train_features_df.apply(le.fit_transform)
print("\n printing feature column of train datasets after label encoder: \n")
print(X_train_label_encoded.head())
print("printing test feature datasets: \n")
print(test_features_df)
X_test_label_encoded=test_features_df.apply(le.fit_transform)
print("printing test feature encoded datasets: \n")
print(X_test_label_encoded)
上面的输出结果如下:
printing feature column of train datasets:
[['k' 'v' 'at' ... 0 0 0]
['k' 't' 'av' ... 0 0 0]
['az' 'w' 'n' ... 0 0 0]
X0 X1 X2 X3 X4 X5 X6 X8 X10 X12 ... X375 X376 X377 X378 \
0 32 23 17 0 3 24 9 14 0 0 ... 0 0 1 0
1 32 21 19 4 3 28 11 14 0 0 ... 1 0 0 0
2 20 24 34 2 3 27 9 23 0 0 ... 0 0 0 0
printing test feature datasets:
X0 X1 X2 X3 X4 X5 X6 X8 X10 X12 ... X375 X376 X377 X378 X379 \
0 az v n f d t a w 0 0 ... 0 0 0 1 0
1 t b ai a d b g y 0 0 ... 0 0 1 0 0
2 az v as f d a j j 0 0 ... 0 0 0 1 0
X0 X1 X2 X3 X4 X5 X6 X8 X10 X12 ... X375 X376 X377 X378 \
0 21 23 34 5 3 26 0 22 0 0 ... 0 0 0 1
1 42 3 8 0 3 9 6 24 0 0 ... 0 0 1 0
2 21 23 17 5 3 0 9 9 0 0 ... 0 0 0 1
3 21 13 34 5 3 31 11 13 0 0 ... 0 0 0 1
4 45 20 17 2 3 30 8 12 0 0 ... 1 0 0 0
如果我们在训练数据中进行标签编码,第一列的az
值会被转换为20,而在测试数据中进行标签编码后,第一列的az
值会被转换为21。