今天我尝试制作一个 bootstrap 来获取各种不同 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方法获得置信区间时(我从其他主题中获取代码:如何在Python中比较不同二元分类器的ROC AUC分数并评估统计显着性?)
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, 31, 16, 11, 23, 7, 19, 10, 14, 5, 10, 25, 30, 24, 8, 20], 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格式的转换可以解决它。谢谢!
请问你是怎么解决这个问题的,我也遇到了同样的错误