|
| 1 | +from defs import (accuracy, group_cv, make_scorer, SelectKBest, |
| 2 | + LogisticRegressionCV, cross_validate, |
| 3 | + make_pipeline, X, y, my_groups, my_weights, |
| 4 | + my_other_weights) |
| 5 | + |
| 6 | +# %% |
| 7 | +# Case A: weighted scoring and fitting |
| 8 | + |
| 9 | +# Here we presume that GroupKFold requests `groups` by default. |
| 10 | +# We need to explicitly request weights in make_scorer and for |
| 11 | +# LogisticRegressionCV. Both of these consumers understand the meaning |
| 12 | +# of the key "sample_weight". |
| 13 | + |
| 14 | +weighted_acc = make_scorer(accuracy, request_props=['sample_weight']) |
| 15 | +lr = LogisticRegressionCV( |
| 16 | + cv=group_cv, |
| 17 | + scoring=weighted_acc, |
| 18 | +).request_sample_weight(fit=['sample_weight']) |
| 19 | +cross_validate(lr, X, y, cv=group_cv, |
| 20 | + props={'sample_weight': my_weights, 'groups': my_groups}, |
| 21 | + scoring=weighted_acc) |
| 22 | + |
| 23 | +# Error handling: if props={'sample_eight': my_weights, ...} was passed, |
| 24 | +# cross_validate would raise an error, since 'sample_eight' was not requested |
| 25 | +# by any of its children. |
| 26 | + |
| 27 | +# %% |
| 28 | +# Case B: weighted scoring and unweighted fitting |
| 29 | + |
| 30 | +# Since LogisticRegressionCV requires that weights explicitly be requested, |
| 31 | +# removing that request means the fitting is unweighted. |
| 32 | + |
| 33 | +weighted_acc = make_scorer(accuracy, request_props=['sample_weight']) |
| 34 | +lr = LogisticRegressionCV( |
| 35 | + cv=group_cv, |
| 36 | + scoring=weighted_acc, |
| 37 | +) |
| 38 | +cross_validate(lr, X, y, cv=group_cv, |
| 39 | + props={'sample_weight': my_weights, 'groups': my_groups}, |
| 40 | + scoring=weighted_acc) |
| 41 | + |
| 42 | +# %% |
| 43 | +# Case C: unweighted feature selection |
| 44 | + |
| 45 | +# Like LogisticRegressionCV, SelectKBest needs to request weights explicitly. |
| 46 | +# Here it does not request them. |
| 47 | + |
| 48 | +weighted_acc = make_scorer(accuracy, request_props=['sample_weight']) |
| 49 | +lr = LogisticRegressionCV( |
| 50 | + cv=group_cv, |
| 51 | + scoring=weighted_acc, |
| 52 | +).request_sample_weight(fit=['sample_weight']) |
| 53 | +sel = SelectKBest() |
| 54 | +pipe = make_pipeline(sel, lr) |
| 55 | +cross_validate(pipe, X, y, cv=group_cv, |
| 56 | + props={'sample_weight': my_weights, 'groups': my_groups}, |
| 57 | + scoring=weighted_acc) |
| 58 | + |
| 59 | +# %% |
| 60 | +# Case D: different scoring and fitting weights |
| 61 | + |
| 62 | +# Despite make_scorer and LogisticRegressionCV both expecting a key |
| 63 | +# sample_weight, we can use aliases to pass different weights to different |
| 64 | +# consumers. |
| 65 | + |
| 66 | +weighted_acc = make_scorer(accuracy, |
| 67 | + request_props={'scoring_weight': 'sample_weight'}) |
| 68 | +lr = LogisticRegressionCV( |
| 69 | + cv=group_cv, |
| 70 | + scoring=weighted_acc, |
| 71 | +).request_sample_weight(fit='fitting_weight') |
| 72 | +cross_validate(lr, X, y, cv=group_cv, |
| 73 | + props={ |
| 74 | + 'scoring_weight': my_weights, |
| 75 | + 'fitting_weight': my_other_weights, |
| 76 | + 'groups': my_groups, |
| 77 | + }, |
| 78 | + scoring=weighted_acc) |
0 commit comments