簡體   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