我试图理解为什么我在有和没有管道的情况下使用逻辑回归得到不同的 AUC-PR 分数。
这是我使用管道的代码:
column_encoder = ColumnTransformer([
('ordinal_enc', OrdinalEncoder(), categorical_cols)
])
pipeline = Pipeline([
('column_encoder', column_enc),
('logreg', LogisticRegressionCV(random_state=777))
])
model = pipeline.fit(X_train, y_train)
y_pred = model.predict(X_test)
print(f'AUC-PR with Pipeline: {average_precision_score(y_test, y_pred):.4f}')
这是我没有管道的代码:
ord_enc = OrdinalEncoder()
ord_encoded_X_train = ord_enc.fit_transform(X_train[categorical_cols])
ord_encoded_X_test = ord_enc.transform(X_test[categorical_cols])
X_train_encoded = X_train.copy(deep=True)
X_test_encoded = X_test.copy(deep=True)
X_train_encoded.loc[:, categorical_cols] = copy.deepcopy(ord_encoded_X_train)
X_test_encoded.loc[:, categorical_cols] = copy.deepcopy(ord_encoded_X_test)
model = LogisticRegression(random_state=777, max_iter=2000)
model.fit(X_train_encoded, y_train)
y_pred = model.predict(X_test_encoded)
print(f'AUC-PR without Pipeline: {average_precision_score(y_test, y_pred):.4f}')
最后:
AUC-PR with Pipeline: 0.1133
AUC-PR without Pipeline: 0.2406
那么,这是为什么呢?