繁体   English   中英

使用 bootstrap 方法的置信区间 AUC

[英]Confidence interval AUC with the bootstrap method

今天我尝试做一个引导程序来获得各种不同 ML 算法 AUC 的区间置信度。

我使用了我的个人医疗数据集,其中包含 61 个特征,格式如下:

年龄 女性
65 1个
45 0

例如我使用了这种类型的算法:

X = data_sevrage.drop(['Echec_sevrage'], axis=1)

y = data_sevrage['Echec_sevrage']

X_train, X_test, y_train, y_test= train_test_split(X, y, test_size=0.25, random_state=0)

lr = LogisticRegression(C=10 ,penalty='l1', solver= 'saga', max_iter=500).fit(X_train,y_train)
score=roc_auc_score(y_test,lr.predict_proba(X_test)[:,1])
precision, recall, thresholds = precision_recall_curve(y_test, lr.predict_proba(X_test)[:,1])
auc_precision_recall = metrics.auc(recall, precision)
y_pred = lr.predict(X_test)
print('ROC AUC score :',score)
print('auc_precision_recall :',auc_precision_recall)

最后,当我使用 boostrap 方法获取置信区间时(我从其他主题中获取代码: How to compare ROC AUC scores of different binary classifiers and assess statistical significance in Python?

def bootstrap_auc(clf, X_train, y_train, X_test, y_test, nsamples=1000):
    auc_values = []
    for b in range(nsamples):
        idx = np.random.randint(X_train.shape[0], size=X_train.shape[0])
        clf.fit(X_train[idx], y_train[idx])
        pred = clf.predict_proba(X_test)[:, 1]
        roc_auc = roc_auc_score(y_test.ravel(), pred.ravel())
        auc_values.append(roc_auc)
    return np.percentile(auc_values, (2.5, 97.5))

bootstrap_auc(lr, X_train, y_train, X_test, y_test, nsamples=1000)

我有这个错误:

“没有 [Int64Index([21, 22, 20, 31, 30, 13, 22, 1, 31, 3, 2, 9, 9, 18, 29, 30, 31,\n 31, 16, 11, 23 , 7, 19, 10, 14, 5, 10, 25, 30, 24, 8, 20],\n dtype='int64')] 在[列]中”

我使用其他方法,但我有几乎相同的错误:

 n_bootstraps = 1000
rng_seed = 42  # control reproducibility
bootstrapped_scores = []

rng = np.random.RandomState(rng_seed)
for i in range(n_bootstraps):
    # bootstrap by sampling with replacement on the prediction indices
    indices = rng.randint(0, len(y_pred), len(y_pred))
    if len(np.unique(y_test[indices])) < 2:
        # We need at least one positive and one negative sample for ROC AUC
        # to be defined: reject the sample
        continue

    score = roc_auc_score(y_test[indices], y_pred[indices])
    bootstrapped_scores.append(score)
    print("Bootstrap #{} ROC area: {:0.3f}".format(i + 1, score))

'[6, 3, 12, 14, 10, 7, 9] 不在索引中'

你能帮我吗? 我测试了很多解决方案,但每次都会出现此错误。

谢谢 !

机器学习算法上 AUC 置信区间的 Bootstrap 方法。

问题解决了,只是格式问题。 numpy格式的转换解决了。 谢谢 !

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM