首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将G-均值集成到cross_validate学习函数中?

如何将G-均值集成到cross_validate学习函数中?
EN

Stack Overflow用户
提问于 2021-04-02 14:25:46
回答 2查看 870关注 0票数 2
代码语言:javascript
运行
复制
from sklearn.model_selection import cross_validate
scores = cross_validate(LogisticRegression(class_weight='balanced',max_iter=100000),
                        X,y, cv=5, scoring=('roc_auc', 'average_precision','f1','recall','balanced_accuracy'))
scores['test_roc_auc'].mean(), scores['test_average_precision'].mean(),scores['test_f1'].mean(),scores['test_recall'].mean(),scores['test_balanced_accuracy'].mean()

如何在上述交叉验证评分参数下计算下列G-均值:

代码语言:javascript
运行
复制
from imblearn.metrics import geometric_mean_score
print('The geometric mean is {}'.format(geometric_mean_score(y_test, y_test_pred)))

代码语言:javascript
运行
复制
from sklearn.metrics import accuracy_score
g_mean = 1.0
    #
for label in np.unique(y_test):
    idx = (y_test == label)
    g_mean *= accuracy_score(y_test[idx], y_test_pred[idx])
    #
g_mean = np.sqrt(g_mean)
score = g_mean
print(score)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-04-03 16:09:06

只是把它当作一个定制的得分手。

代码语言:javascript
运行
复制
from sklearn.metrics import make_scorer
from imblearn.metrics import geometric_mean_score

gm_scorer = make_scorer(geometric_mean_score, greater_is_better=True, average='binary')

greater_is_better=True设置为最佳值更接近1。geometrics_mean_score的附加参数可以直接传递给make_scorer

完整示例

代码语言:javascript
运行
复制
from sklearn.model_selection import cross_validate
from sklearn.metrics import make_scorer
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from imblearn.metrics import geometric_mean_score

X, y = load_breast_cancer(return_X_y=True)

gm_scorer = make_scorer(geometric_mean_score, greater_is_better=True)

scores = cross_validate(
    LogisticRegression(class_weight='balanced',max_iter=100000),
    X,y, 
    cv=5, 
    scoring=gm_scorer
)
scores
>>>
{'fit_time': array([0.76488066, 0.69808364, 1.22158527, 0.94157672, 1.01577377]),
 'score_time': array([0.00103951, 0.00100923, 0.00065804, 0.00071168, 0.00068736]),
 'test_score': array([0.91499142, 0.93884403, 0.9860133 , 0.92439026, 0.9525989 ])}

编辑

若要指定多个指标,请将一个dict传递给scoring参数

代码语言:javascript
运行
复制
scores = cross_validate(
    LogisticRegression(class_weight='balanced',max_iter=100000),
    X,y, 
    cv=5, 
    scoring={'gm_scorer': gm_scorer, 'AUC': 'roc_auc', 'Avg_Precision': 'average_precision'}
)
scores
>>>
{'fit_time': array([1.03509665, 0.96399784, 1.49760461, 1.13874388, 1.32006526]),
 'score_time': array([0.00560617, 0.00357151, 0.0057447 , 0.00566769, 0.00549698]),
 'test_gm_scorer': array([0.91499142, 0.93884403, 0.9860133 , 0.92439026, 0.9525989 ]),
 'test_AUC': array([0.99443171, 0.99344907, 0.99801587, 0.97949735, 0.99765258]),
 'test_Avg_Precision': array([0.99670544, 0.99623085, 0.99893162, 0.98640759, 0.99861043])}
票数 3
EN

Stack Overflow用户

发布于 2021-04-02 14:42:17

您需要定制一个记分员,下面是一个例子:https://stackoverflow.com/a/53850851/12384070,如果它是您唯一想要的得分手,您可以这样做:

代码语言:javascript
运行
复制
scores = cross_validate(LogisticRegression(class_weight='balanced',max_iter=100000),
                        X,y, cv=5, scoring=your_custom_function)

我想你可以用另一个得分手,正如医生所解释的:

代码语言:javascript
运行
复制
If scoring reprents multiple scores, one can use:

a list or tuple of unique strings;

a callable returning a dictionary where the keys are the metric names and the values are the metric scores;

a dictionary with metric names as keys and callables a values.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66920782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档