【问题标题】:How to get better precision and recall with imbalanced dataset in python如何在 python 中使用不平衡的数据集获得更好的精度和召回率
【发布时间】:2020-08-10 06:13:23
【问题描述】:

我正在研究医疗保险欺诈检测模型。数据非常不平衡,有 14 个欺诈性阳性案例和大约 100 万个非欺诈性案例。我最初有 8 个特征,但是通过对分类变量进行一次性编码,我有 103 个特征(这是由于有 94 个唯一的提供者类型)。我创建了一个将逻辑回归分类器与 SMOTE 相结合的管道。

##########
#Use pipeline - combination of SMOTE and logistic regression model 
# Define which resampling method and which ML model to use in the pipeline

resampling = SMOTE(random_state = 27, sampling_strategy = "minority")
model = LogisticRegression(solver='liblinear')
pipeline = Pipeline([('SMOTE', resampling), ('Logistic Regression', model)])

# Split your data X and y, into a training and a test set and fit the pipeline onto the training data
y = PartB_encoded['Is_fraud']
X = PartB_encoded.drop(['Is_fraud'], axis = 1)       
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=27)
pipeline.fit(X_train, y_train) 
predicted = pipeline.predict(X_test)       
print("Accuracy score: ", accuracy_score(y_true = y_test, y_pred = predicted))  
print("Precision score: ", precision_score(y_true = y_test, y_pred=predicted)) 
print("Recall score: ", recall_score(y_true = y_test, y_pred= predicted)) 

# Obtain the results from the classification report and confusion matrix 
print('Classifcation report:\n', classification_report(y_test, predicted))
conf_mat = confusion_matrix(y_true=y_test, y_pred=predicted)
print('Confusion matrix:\n', conf_mat)

这是我的输出:

Accuracy score:  0.9333130935552119
Precision score:  2.3716352424997034e-05
Recall score:  0.09090909090909091
Classification report:
               precision    recall  f1-score   support

       False       1.00      0.93      0.97    632407
        True       0.00      0.09      0.00        11

    accuracy                           0.93    632418
   macro avg       0.50      0.51      0.48    632418
weighted avg       1.00      0.93      0.97    632418

Confusion matrix:
 [[590243  42164]
 [    10      1]]

显然我的召回率和准确率极低,无法接受。如何提高召回率和准确率?我正在考虑进行欠采样,但如果我将负类从大约 100 万条记录中更改——> 14 条记录以匹配我的正类,我担心会删除太多数据。我也在考虑删除功能,但我不确定如何确定要删除哪些功能。

【问题讨论】:

  • 虽然在数据集不平衡时可以使用一些技术,但我认为它不适用于您的情况。 100 万 vs 14:不仅数据不平衡,而且 14 太少,无法学习。你必须收集(甚至模拟自己)更多的欺诈数据
  • 确实如@Wazaki 所说;请记住,机器学习不是魔法。

标签: python machine-learning classification imbalanced-data


【解决方案1】:

我们在处理金融欺诈检测时遇到了类似的问题,通常实际欺诈数据不到 0.1%。您必须对主要类进行欠采样,同时注意确保各种内部类的表示保持不变。因此,首先对您的主要人口进行聚类,然后从每个聚类中进行选择,为主要类创建一个精简的人口。尝试使用 80:20、90:10 等比例,直到达到可观的精度和召回率。像 SMOTE 这样的过采样技术并不是真正可取的,因为在大多数情况下,综合准备的数据会与真实数据不同

【讨论】:

    猜你喜欢
    • 2018-12-24
    • 2023-02-10
    • 2019-12-27
    • 2019-08-18
    • 2021-05-17
    • 2018-07-04
    • 2017-01-04
    • 2018-03-17
    • 2017-11-18
    相关资源
    最近更新 更多