python - 使用 RepeatedStratifiedKFold 5*10 的 cross_val_predict 概率
问题描述
我的目标是从 5*10 StratifiedKfold CV 计算 AUC、特异性、灵敏度和 95 % CI。我还需要阈值为 0.4 的特异性和灵敏度以最大化灵敏度。
到目前为止,我能够为 AUC 实现它。下面的代码:
seed = 42
# Grid Search
fit_intercept=[True, False]
C = [np.arange(1,41,1)]
penalty = ['l1', 'l2']
params = dict(C=C, fit_intercept = fit_intercept, penalty = penalty)
print(params)
logreg = LogisticRegression(random_state=seed)
# instantiate the grid
logreg_grid = GridSearchCV(logreg, param_grid = params , cv=5, scoring='roc_auc', iid='False')
# fit the grid with data
logreg_grid.fit(X_train, y_train)
logreg = logreg_grid.best_estimator_
cv = RepeatedStratifiedKFold(n_splits = 5, n_repeats = 10, random_state = seed)
logreg_scores = cross_val_score(logreg, X_train, y_train, cv=cv, scoring='roc_auc')
print('LogReg:',logreg_scores.mean())
import scipy.stats
def mean_confidence_interval(data, confidence=0.95):
a = 1.0 * np.array(data)
n = len(a)
m, se = np.mean(a), scipy.stats.sem(a)
h = se * scipy.stats.t.ppf((1 + confidence) / 2, n-1)
return m, m-h, m+h
mean_confidence_interval(logreg_scores, confidence=0.95)
输出:(0.7964761904761904,0.7675441789148183,0.8254082020375626)
到目前为止我真的很满意,但是我怎样才能实现这个概率,所以我可以计算 FPR、TPR 和阈值?对于一个简单的 5 倍,我会这样做:
def evaluate_threshold(threshold):
print('Sensitivity(',threshold,'):', tpr[thresholds > threshold][-1])
print('Specificity(',threshold,'):', 1 - fpr[thresholds > threshold][-1])
logreg_proba = cross_val_predict(logreg, X_train, y_train, cv=5, method='predict_proba')
fpr, tpr, thresholds = metrics.roc_curve(y_train, log_proba[:,1])
evaluate_threshold(0.5)
evaluate_threshold(0.4)
#Output would be:
#Sensitivity( 0.5 ): 0.76
#Specificity( 0.5 ): 0.7096774193548387
#Sensitivity( 0.4 ): 0.88
#Specificity( 0.4 ): 0.6129032258064516
如果我用 5*10 CV 以这种方式尝试:
cv = RepeatedStratifiedKFold(n_splits = 5, n_repeats = 10, random_state = seed)
y_pred = cross_val_predict(logreg, X_train, y_train, cv=cv, method='predict_proba')
fpr, tpr, thresholds = metrics.roc_curve(y_train, log_proba[:,1])
evaluate_threshold(0.5)
evaluate_threshold(0.4)
它抛出一个错误:
cross_val_predict only works for partitions
你能帮我解决这个问题吗?
解决方案
这就是我尝试过的。
for i in range(10):
cv = StratifiedKFold(n_splits = 5, random_state = i)
y_pred = cross_val_predict(logreg, X_train, y_train, cv=cv, method='predict_proba')
fpr, tpr, thresholds = metrics.roc_curve(y_train, log_proba[:,1])
evaluate_threshold(0.5)
Out:
Sensitivity( 0.5 ): 0.84
Specificity( 0.5 ): 0.6451612903225806
Sensitivity( 0.5 ): 0.84
Specificity( 0.5 ): 0.6451612903225806
Sensitivity( 0.5 ): 0.84
Specificity( 0.5 ): 0.6451612903225806
and so on....
不幸的是,输出总是相同的,这不是我在使用 RepeatedStratifiedKFold 时所期望的。
也许有人可以给我一个建议?
推荐阅读
- azure-ad-b2c - B2C 密码重置策略强制登录进入忘记密码流程
- git - 如何从 .gitignore 中删除某些文件目录关于 Git
- python - 如何用科学记数法(张量格式)解析文本文件并将它们转换为浮点数
- spring-boot - 为什么 Spring Converter 没有抛出异常?
- grails - CSV 插件控制器在哪里?
- azure-synapse - Synapse Spark Pool 在配置状态下挂起:删除错误
- raspberry-pi - Raspberry Pi dhcp-exit-hooks 适用于 wlan0 但不适用于 eth0
- python-3.x - 使用 BeautifulSoup - Python 从 www.transfermarkt.co.uk 抓取数据
- spring - 返回具体的 RestTemplate bean,如何?
- postgresql - 用于 JDBC URL 的 URI/URL 构建器