我有 2 个不同的 csv,其中包含训练数据和测试数据。我从这些 train_features_df 和 test_features_df 创建了两个不同的数据框。请注意,测试和训练数据有多个分类列,因此我需要在它们上应用 labelEncoder,因为它适用于我的数据集。所以我在训练和测试数据上分别应用了标签编码器。当我打印训练和测试数据集的新编码值时,我看到对于相同特征的相同分类值,新编码数据的输出是不同的。这是否意味着我必须合并训练和测试数据。然后应用标签编码,然后再将它们分开?
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
如果我们在 lebel 编码后的训练数据帧中看到az
,第一列中的值被转换为值 20,而在 lebel 编码后的测试数据帧az
中,第一列中的值被转换为值 21。