为什么在使用ADASYN和10倍CV进行不平衡分类后AUC结果不好?

时间:2019-12-02 02:56:25

标签: python oversampling imbalanced-data

我需要对不平衡样本进行分类(class_1:class_0 = 1:9),每个样本具有6个特征。我使用SVM和k倍CV(k = 10)进行分类。对于折叠,在训练数据集中,我使用ADASYN对少数派进行了过度采样。但是最终的AUC结果表明,每个折叠的AUC差异很大。代码如下。

import numpy as np
import matplotlib.pyplot as plt

from sklearn.preprocessing import StandardScaler
from imblearn.over_sampling import SMOTE, ADASYN
from collections import Counter
from sklearn import svm
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import roc_curve, auc
from scipy import interp

# =============================================================================
# load result
# =============================================================================
class_1 = np.load('class_1.npy')
class_0 = np.load('class_0.npy')

class_1_0 = np.concatenate((class_1, class_0), axis=0)
# =============================================================================
# classification
# =============================================================================
X = StandardScaler().fit_transform(class_1_0)
y = np.hstack([np.ones((1260,),dtype=np.int), np.zeros((11340,),dtype=np.int)]) # label y int format

# =============================================================================
# k-fold Cross-validation with ADASYN and SVM
# =============================================================================
random_state = np.random.RandomState(0)
cv = StratifiedKFold(n_splits=10)
clf = svm.SVC(kernel='linear', C=1, probability=True, random_state=random_state)
tprs = []
aucs = []
mean_fpr = np.linspace(0, 1, 100)

fig = plt.figure(figsize=(10,8))

i = 0
for train, test in cv.split(X, y):
    X_train_oversampled, y_train_oversampled = ADASYN().fit_sample(X[train], y[train]) 
    print(sorted(Counter(y_train_oversampled).items()))
    probas_ = clf.fit(X_train_oversampled, y_train_oversampled).predict_proba(X[test])

    # Compute ROC curve and area the curve
    fpr, tpr, thresholds = roc_curve(y[test], probas_[:, 1])
    tprs.append(interp(mean_fpr, fpr, tpr))
    tprs[-1][0] = 0.0
    roc_auc = auc(fpr, tpr)
    aucs.append(roc_auc)
    plt.plot(fpr, tpr, lw=1, alpha=0.3,
             label='ROC fold %d (AUC = %0.2f)' % (i, roc_auc))
    i += 1
plt.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r',
         label='Chance', alpha=.8)

mean_tpr = np.mean(tprs, axis=0)
mean_tpr[-1] = 1.0
mean_auc = auc(mean_fpr, mean_tpr)
std_auc = np.std(aucs)
plt.plot(mean_fpr, mean_tpr, color='b',
         label=r'Mean ROC (AUC = %0.2f $\pm$ %0.2f)' % (mean_auc, std_auc),
         lw=2, alpha=.8)

std_tpr = np.std(tprs, axis=0)
tprs_upper = np.minimum(mean_tpr + std_tpr, 1)
tprs_lower = np.maximum(mean_tpr - std_tpr, 0)
plt.fill_between(mean_fpr, tprs_lower, tprs_upper, color='grey', alpha=.2,
                 label=r'$\pm$ 1 std. dev.')

plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Example')
plt.legend(loc="lower right")
plt.show()

The AUC of imbalanced classification

0 个答案:

没有答案