【问题标题】:Imblearn Pipeline resulting in poor metricsImblearn Pipeline 导致指标不佳
【发布时间】:2020-11-17 02:07:41
【问题描述】:

我正在处理使用以下代码创建的不平衡数据集

X, y = make_classification(n_samples=10000, n_features=2, n_redundant=0, n_clusters_per_class=1,
                            weights=[0.99], flip_y=0, random_state=1)

我尝试使用 SMOTE 过采样消除不平衡,然后尝试拟合 ML 模型。这是使用普通方法然后通过创建管道来完成的。

普通法

from imblearn.over_sampling import SMOTE

oversampled_data = SMOTE(sampling_strategy=0.5)
X_over, y_over = oversampled_data.fit_resample(X, y)

logistic = LogisticRegression(solver='liblinear')

scoring = ['accuracy', 'precision', 'recall', 'f1']
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)

# evaluating the model
scores = cross_validate(logistic, X_over, y_over, scoring=scoring, cv=cv, n_jobs=-1,  return_train_score=True)

print('Accuracy: {:.2f}, Precison: {:.2f}, Recall: {:.2f} F1: {:.2f}'.format(np.mean(scores['test_accuracy']), np.mean(scores['test_precision']), np.mean(scores['test_recall']), np.mean(scores['test_f1'])))

输出 - 准确度:0.93,精度:0.92,召回率:0.86,F1:0.89

管道

from imblearn.pipeline import make_pipeline, Pipeline
from sklearn.model_selection import cross_val_score

oversampled_data = SMOTE(sampling_strategy=0.5)

pipeline = Pipeline([('smote', oversampled_data), ('model', LogisticRegression())])

# pipeline = make_pipeline(oversampled_data, logistic)

scoring = ['accuracy', 'precision', 'recall', 'f1']
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)

# evaluating the model
scores = cross_validate(pipeline, X, y, scoring=scoring, cv=cv, n_jobs=-1,  return_train_score=True)

print('Accuracy: {:.2f}, Precison: {:.2f}, Recall: {:.2f}, F1: {:.2f}'.format(np.mean(scores['test_accuracy']), np.mean(scores['test_precision']), np.mean(scores['test_recall']), np.mean(scores['test_f1'])))

输出 - 准确度:0.96,精度:0.19,召回:0.84,F1:0.31

我在使用 Pipeline 时做错了什么,为什么使用 Pipeline 时 Precision 和 F1 分数如此之差?

【问题讨论】:

  • 可能管道没有调用 fit_resample 方法?

标签: python machine-learning scikit-learn imblearn


【解决方案1】:

在第一种方法中,您在分割训练集和测试集之前创建合成示例,而在第二种方法中,您在分割之后进行。

前一种方法将合成数据点添加到测试集中,但后一种方法没有。此外,前一种方法会从数据泄漏中产生夸大的分数:它(部分)基于训练数据集中的一些数据点添加合成测试样本。参见例如

【讨论】:

    猜你喜欢
    • 2021-07-14
    • 2020-09-22
    • 2017-03-03
    • 1970-01-01
    • 2020-10-28
    • 2019-03-25
    • 1970-01-01
    • 2023-02-02
    • 2019-04-16
    相关资源
    最近更新 更多