|
| 1 | +from defs import (accuracy, make_scorer, SelectKBest, LogisticRegressionCV, |
| 2 | + group_cv, cross_validate, make_pipeline, X, y, my_groups, |
| 3 | + my_weights, my_other_weights) |
| 4 | + |
| 5 | +# %% |
| 6 | +# Case A: weighted scoring and fitting |
| 7 | + |
| 8 | +lr = LogisticRegressionCV( |
| 9 | + cv=group_cv, |
| 10 | + scoring='accuracy', |
| 11 | + prop_routing={'cv': ['groups'], |
| 12 | + 'scoring': ['sample_weight'], |
| 13 | + } |
| 14 | + # one question here is whether we need to explicitly route sample_weight |
| 15 | + # to LogisticRegressionCV's fitting... |
| 16 | +) |
| 17 | + |
| 18 | +# Alternative syntax, which assumes cv receives 'groups' by default, and that a |
| 19 | +# method-based API is provided on meta-estimators: |
| 20 | +# lr = LogisticRegressionCV( |
| 21 | +# cv=group_cv, |
| 22 | +# scoring='accuracy', |
| 23 | +# ).add_prop_route(scoring='sample_weight') |
| 24 | + |
| 25 | +cross_validate(lr, X, y, cv=group_cv, |
| 26 | + props={'sample_weight': my_weights, 'groups': my_groups}, |
| 27 | + scoring='accuracy', |
| 28 | + prop_routing={'estimator': '*', # pass all props |
| 29 | + 'cv': ['groups'], |
| 30 | + 'scoring': ['sample_weight'], |
| 31 | + }) |
| 32 | + |
| 33 | +# Error handling: if props={'sample_eight': my_weights, ...} was passed |
| 34 | +# instead, LogisticRegressionCV would have to identify that a key was passed |
| 35 | +# that could not be routed nor used, in order to raise an error. |
| 36 | + |
| 37 | +# %% |
| 38 | +# Case B: weighted scoring and unweighted fitting |
| 39 | + |
| 40 | +# Here we rename the sample_weight prop so that we can specify that it only |
| 41 | +# applies to scoring. |
| 42 | +lr = LogisticRegressionCV( |
| 43 | + cv=group_cv, |
| 44 | + scoring='accuracy', |
| 45 | + prop_routing={'cv': ['groups'], |
| 46 | + # read the following as "scoring should consume |
| 47 | + # 'scoring_weight' as if it were 'sample_weight'." |
| 48 | + 'scoring': {'sample_weight': 'scoring_weight'}, |
| 49 | + }, |
| 50 | +) |
| 51 | +cross_validate(lr, X, y, cv=group_cv, |
| 52 | + props={'scoring_weight': my_weights, 'groups': my_groups}, |
| 53 | + scoring='accuracy', |
| 54 | + prop_routing={'estimator': '*', |
| 55 | + 'cv': ['groups'], |
| 56 | + 'scoring': {'sample_weight': 'scoring_weight'}, |
| 57 | + }) |
| 58 | + |
| 59 | +# %% |
| 60 | +# Case C: unweighted feature selection |
| 61 | + |
| 62 | +lr = LogisticRegressionCV( |
| 63 | + cv=group_cv, |
| 64 | + scoring='accuracy', |
| 65 | + prop_routing={'cv': ['groups'], |
| 66 | + 'scoring': ['sample_weight'], |
| 67 | + }) |
| 68 | +pipe = make_pipeline(SelectKBest(), lr, |
| 69 | + prop_routing={'logisticregressioncv': ['sample_weight', |
| 70 | + 'groups']}) |
| 71 | +cross_validate(lr, X, y, cv=group_cv, |
| 72 | + props={'sample_weight': my_weights, 'groups': my_groups}, |
| 73 | + scoring='accuracy', |
| 74 | + prop_routing={'estimator': '*', |
| 75 | + 'cv': ['groups'], |
| 76 | + 'scoring': ['sample_weight'], |
| 77 | + }) |
| 78 | + |
| 79 | +# %% |
| 80 | +# Case D: different scoring and fitting weights |
| 81 | +lr = LogisticRegressionCV( |
| 82 | + cv=group_cv, |
| 83 | + scoring='accuracy', |
| 84 | + prop_routing={'cv': ['groups'], |
| 85 | + # read the following as "scoring should consume |
| 86 | + # 'scoring_weight' as if it were 'sample_weight'." |
| 87 | + 'scoring': {'sample_weight': 'scoring_weight'}, |
| 88 | + }, |
| 89 | +) |
| 90 | +cross_validate(lr, X, y, cv=group_cv, |
| 91 | + props={'scoring_weight': my_weights, 'groups': my_groups, |
| 92 | + 'fitting_weight': my_other_weights}, |
| 93 | + scoring='accuracy', |
| 94 | + prop_routing={'estimator': {'sample_weight': 'fitting_weight', |
| 95 | + 'scoring_weight': 'scoring_weight', |
| 96 | + 'groups': 'groups'}, |
| 97 | + 'cv': ['groups'], |
| 98 | + 'scoring': {'sample_weight': 'scoring_weight'}, |
| 99 | + }) |
0 commit comments