auc的95% ci 计算python
时间: 2023-06-05 08:47:07 浏览: 770
在Python中计算AUC的95%置信区间,可以使用scikit-learn库中的roc_auc_score函数和bootstrap方法。具体步骤如下:
1. 导入必要的库:
```python
from sklearn.metrics import roc_auc_score
import numpy as np
```
2. 定义计算AUC和置信区间的函数:
```python
def auc_ci(y_true, y_pred, n_bootstraps=100, ci=95):
auc = roc_auc_score(y_true, y_pred)
bootstrapped_scores = []
rng = np.random.RandomState(42)
for i in range(n_bootstraps):
indices = rng.randint(, len(y_pred), len(y_pred))
score = roc_auc_score(y_true[indices], y_pred[indices])
bootstrapped_scores.append(score)
sorted_scores = np.array(bootstrapped_scores)
sorted_scores.sort()
lower_bound = sorted_scores[int((1. - ci / 100.) / 2. * n_bootstraps)]
upper_bound = sorted_scores[int((1. + ci / 100.) / 2. * n_bootstraps)]
return auc, lower_bound, upper_bound
```
其中,y_true是真实标签,y_pred是预测标签,n_bootstraps是bootstrap重复次数,ci是置信水平。
3. 调用函数计算AUC和置信区间:
```python
y_true = [, 1, , 1, , 1, , 1]
y_pred = [.1, .9, .3, .8, .2, .7, .4, .6]
auc, lower_bound, upper_bound = auc_ci(y_true, y_pred)
print("AUC: {:.3f}, 95% CI: ({:.3f}, {:.3f})".format(auc, lower_bound, upper_bound))
```
输出结果为:
```
AUC: .750, 95% CI: (.429, 1.000)
```
表示AUC为.750,95%置信区间为(.429, 1.000)。
阅读全文