mf_mrtg_default_model.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:MF_MBS_Default_Risk 作者: bentruitt 项目源码 文件源码
def plot_roc_curve(X, y, plot_dir, trial, cv, model):

    mean_tpr = 0.0
    mean_fpr = np.linspace(0, 1, 100)
    thresh_plt = 0.0
    thresh_mean = 0.0
    model_nm = str(model).split("(")[0]
    ### Create StratifiedKFold generator
    cv = StratifiedKFold(y, n_folds=5, shuffle=True)
    ### Initialize StandardScaler
    scaler = StandardScaler()

    for i, (train, test) in enumerate(cv):
        X_train = scaler.fit_transform(X[train])
        X_test = scaler.transform(X[test])
        probas_ = model.fit(X_train, y[train]).predict_proba(X_test)
        # Compute ROC curve and area the curve
        fpr, tpr, thresholds = roc_curve(y[test], probas_[:, 1])
        mean_tpr += interp(mean_fpr, fpr, tpr)
        mean_tpr[0] = 0.0
        roc_auc = auc(fpr, tpr)
        plt.plot(fpr, tpr, lw=1, label='ROC fold %d (area = %0.2f)' % (i+1, roc_auc))
        thresholds[0] = min(1.0, thresholds[0])
        thresholds[-1] = max(0.0, thresholds[-1])
        thresh_mean += interp(mean_fpr, np.linspace(0,1,len(thresholds)), thresholds)
        # plt.plot(fpr, thresholds, lw=1, label='Thresholds %d (%0.2f - %0.2f)' % (i+1, thresholds.max(), thresholds.min())) # np.linspace(0,1,len(thresholds))

    plt.plot([0, 1], [0, 1], '--', color=(0.6, 0.6, 0.6), label='Random')

    thresh_mean /= len(cv)
    mean_tpr /= len(cv)
    mean_tpr[-1] = 1.0
    mean_auc = auc(mean_fpr, mean_tpr)
    plt.plot(mean_fpr, mean_tpr, 'k--', label='Mean ROC (area = %0.2f)' % mean_auc, lw=2)
    plt.plot(mean_fpr, thresh_mean, 'k--', label='Mean Threshold')

    plt.xlim([-0.05, 1.05])
    plt.ylim([-0.05, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('ROC plot for ' + model_nm)
    plt.legend(loc="lower right")
    plt.tight_layout()
    plt.savefig(plot_dir + model_nm + '_ROC_plot_' + trial + '.png')
    plt.close()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号