Esempio n. 1
0
def get_ensembles_for_credit(test: bool = False):
    c_limits = np.concatenate((np.arange(2, 10), np.arange(10, 29, 2)))
    stbl_cs = lambda: enumerate(c_limits)
    stbl_kw = [{'weak_learners': wl_lst} for wl_lst in get_weak_learner_inst_list_credit(c_limits)]

    kwargs_2 = [{**kw, 'penalty': 'l2', 'alpha': 100, 'max_rules': None} for kw in stbl_kw]
    kwargs_3 = [{**kw, 'penalty': 'l2', 'alpha': 10, 'max_rules': None} for kw in stbl_kw]
    kwargs_4 = [{**kw, 'penalty': 'l2', 'alpha': 1, 'max_rules': None} for kw in stbl_kw]
    kwargs_5 = [{**kw, 'penalty': 'l2', 'alpha': 0.1, 'max_rules': None} for kw in stbl_kw]

    ALL_ENSEMBLES = []
    ALL_ENSEMBLES.append(
        [Model('stbl_l2 - mm0_alpha_10.', stbl, 'max_complexity', c, 'min_mult', 0, kwargs_3[i]) for i, c in stbl_cs() if i < 8]
        + [Model('stbl_l2 - mm0_alpha_1.', stbl, 'max_complexity', c, 'min_mult', 0, kwargs_4[i]) for i, c in stbl_cs() if i < 8]
        + [Model('stbl_l2 - mm0_alpha_0.1', stbl, 'max_complexity', c, 'min_mult', 0, kwargs_5[i]) for i, c in stbl_cs() if i < 8]
        + [Model('stbl_l2 - mm1_alpha_10.', stbl, 'max_complexity', c, 'min_mult', 1, kwargs_3[i]) for i, c in stbl_cs()]
        + [Model('stbl_l2 - mm1_alpha_1.', stbl, 'max_complexity', c, 'min_mult', 1, kwargs_4[i]) for i, c in stbl_cs()]
        + [Model('stbl_l2 - mm1_alpha_0.1', stbl, 'max_complexity', c, 'min_mult', 1, kwargs_5[i]) for i, c in stbl_cs()]
        + [Model('stbl_l2 - mm2_alpha_10.', stbl, 'max_complexity', c, 'min_mult', 2, kwargs_3[i]) for i, c in stbl_cs()]
        + [Model('stbl_l2 - mm2_alpha_1.', stbl, 'max_complexity', c, 'min_mult', 2, kwargs_4[i]) for i, c in stbl_cs()]
        + [Model('stbl_l2 - mm2_alpha_0.1', stbl, 'max_complexity', c, 'min_mult', 2, kwargs_5[i]) for i, c in stbl_cs()]
    )

    BEST_ENSEMBLES = []
    BEST_ENSEMBLES += [
        [Model('stbl_l2_mm0', stbl, 'max_complexity', c, 'min_mult', 0, kwargs_3[i]) for i, c in stbl_cs() if i < 8],
        [Model('stbl_l2_mm1', stbl, 'max_complexity', c, 'min_mult', 1, kwargs_3[i]) for i, c in stbl_cs()]
    ]

    kwargs_2 = [{**kw, 'penalty': 'l1', 'alpha': 100, 'max_rules': None} for kw in stbl_kw]
    kwargs_3 = [{**kw, 'penalty': 'l1', 'alpha': 10, 'max_rules': None} for kw in stbl_kw]
    kwargs_4 = [{**kw, 'penalty': 'l1', 'alpha': 1, 'max_rules': None} for kw in stbl_kw]
    kwargs_5 = [{**kw, 'penalty': 'l1', 'alpha': 0.1, 'max_rules': None} for kw in stbl_kw]

    ALL_ENSEMBLES.append(
        [Model('stbl_l1 - mm0_alpha_100.', stbl, 'max_complexity', c, 'min_mult', 0, kwargs_2[i]) for i, c in stbl_cs()]
        + [Model('stbl_l1 - mm0_alpha_10.', stbl, 'max_complexity', c, 'min_mult', 0, kwargs_3[i]) for i, c in stbl_cs()]
        + [Model('stbl_l1 - mm0_alpha_1.', stbl, 'max_complexity', c, 'min_mult', 0, kwargs_4[i]) for i, c in stbl_cs()]
        + [Model('stbl_l1 - mm0_alpha_0.1', stbl, 'max_complexity', c, 'min_mult', 0, kwargs_5[i]) for i, c in stbl_cs()]
        + [Model('stbl_l1 - mm1_alpha_100.', stbl, 'max_complexity', c, 'min_mult', 1, kwargs_2[i]) for i, c in stbl_cs()]
        + [Model('stbl_l1 - mm1_alpha_10.', stbl, 'max_complexity', c, 'min_mult', 1, kwargs_3[i]) for i, c in stbl_cs()]
        + [Model('stbl_l1 - mm1_alpha_1.', stbl, 'max_complexity', c, 'min_mult', 1, kwargs_4[i]) for i, c in stbl_cs()]
        + [Model('stbl_l1 - mm1_alpha_0.1', stbl, 'max_complexity', c, 'min_mult', 1, kwargs_5[i]) for i, c in stbl_cs()]
        + [Model('stbl_l1 - mm2_alpha_100.', stbl, 'max_complexity', c, 'min_mult', 2, kwargs_2[i]) for i, c in stbl_cs()]
        + [Model('stbl_l1 - mm2_alpha_10.', stbl, 'max_complexity', c, 'min_mult', 2, kwargs_3[i]) for i, c in stbl_cs()]
        + [Model('stbl_l1 - mm2_alpha_1.', stbl, 'max_complexity', c, 'min_mult', 2, kwargs_4[i]) for i, c in stbl_cs()]
        + [Model('stbl_l1 - mm2_alpha_0.1', stbl, 'max_complexity', c, 'min_mult', 2, kwargs_5[i]) for i, c in stbl_cs()]
    )

    BEST_ENSEMBLES += [
        [Model('stbl_l1_mm0', stbl, 'max_complexity', c, 'min_mult', 0, kwargs_2[i]) for i, c in stbl_cs()],
        [Model('stbl_l1_mm1', stbl, 'max_complexity', c, 'min_mult', 1, kwargs_2[i]) for i, c in stbl_cs()]
    ]

    return ALL_ENSEMBLES if not test else BEST_ENSEMBLES
Esempio n. 2
0
from imodels import (
    SkopeRulesClassifier as skope, RuleFitClassifier as rfit, FPLassoClassifier as fpl, 
    FPSkopeClassifier as fps, BayesianRuleListClassifier as brl, BoostedRulesClassifier as brs
)
from sklearn.ensemble import RandomForestClassifier as rf, GradientBoostingClassifier as gb
from sklearn.tree import DecisionTreeClassifier

from experiments.config.config_general import RULEFIT_KWARGS, BRL_KWARGS, FPL_KWARGS
from experiments.models.stablelinear import StableLinearClassifier as stbl
from experiments.util import Model
from experiments.util import get_best_model_under_complexity


CREDIT_ESTIMATORS = []
CREDIT_ESTIMATORS.append(
    [Model('random_forest - mid_0', rf, 'n_estimators', n, 'min_impurity_decrease', 0., {'max_depth': 2}) for n in np.arange(1, 8)]
    + [Model('random_forest - mid_1e-4', rf, 'n_estimators', n, 'min_impurity_decrease', 1e-4, {'max_depth': 2}) for n in np.arange(1, 8)]
    + [Model('random_forest - mid_5e-4', rf, 'n_estimators', n, 'min_impurity_decrease', 5e-4, {'max_depth': 2}) for n in np.arange(1, 8)]
)
CREDIT_ESTIMATORS.append(
    [Model('gradient_boosting - mid_0', gb, 'n_estimators', n, 'min_impurity_decrease', 0., {'max_depth': 2}) for n in np.arange(1, 8)]
    + [Model('gradient_boosting - mid_50', gb, 'n_estimators', n, 'min_impurity_decrease', 50, {'max_depth': 2}) for n in np.arange(1, 30, 3)]
    + [Model('gradient_boosting - mid_80', gb, 'n_estimators', n, 'min_impurity_decrease', 80, {'max_depth': 2}) for n in np.arange(1, 40, 4)]
)
CREDIT_ESTIMATORS.append(
    [Model('skope_rules - prec_0.5', skope, 'n_estimators', n, 'precision_min', 0.5, {'max_depth': 2}) for n in np.arange(1, 80, 8)]
    + [Model('skope_rules - prec_.45', skope, 'n_estimators', n, 'precision_min', 0.45, {'max_depth': 2}) for n in [1, 2, 3] + np.arange(5, 40, 4).tolist()]
    + [Model('skope_rules - prec_0.4', skope, 'n_estimators', n, 'precision_min', 0.4, {'max_depth': 2}) for n in np.arange(1, 11).tolist() + [13, 15, 17]]
)
CREDIT_ESTIMATORS.append(
    [Model('rulefit - alpha_30', rfit, 'n_estimators', n, 'alpha', 30, RULEFIT_KWARGS) for n in np.arange(1, 13)]
Esempio n. 3
0
import numpy as np
from imodels import (SkopeRulesClassifier as skope, RuleFitClassifier as rfit,
                     FPLassoClassifier as fpl, FPSkopeClassifier as fps,
                     BayesianRuleListClassifier as brl, BoostedRulesClassifier
                     as brs)
from sklearn.ensemble import RandomForestClassifier as rf, GradientBoostingClassifier as gb
from sklearn.tree import DecisionTreeClassifier

from experiments.config.config_general import RULEFIT_KWARGS, BRL_KWARGS, FPL_KWARGS
from experiments.models.stablelinear import StableLinearClassifier as stbl
from experiments.util import Model
from experiments.util import get_best_model_under_complexity

RECIDIVISM_ESTIMATORS = []
RECIDIVISM_ESTIMATORS.append([
    Model('random_forest - mss_2', rf, 'n_estimators', n, 'min_samples_split',
          2, {'max_depth': 3}) for n in np.arange(1, 8)
] + [
    Model('random_forest - mss_100', rf, 'n_estimators', n,
          'min_samples_split', 100, {'max_depth': 3}) for n in np.arange(1, 8)
] + [
    Model('random_forest - mss_500', rf, 'n_estimators', n,
          'min_samples_split', 500, {'max_depth': 3}) for n in np.arange(1, 8)
] + [
    Model('random_forest - mss_1000', rf, 'n_estimators', n,
          'min_samples_split', 1000, {'max_depth': 3})
    for n in np.arange(1, 8)
] + [
    Model('random_forest - mss_1500', rf, 'n_estimators', n,
          'min_samples_split', 1500, {'max_depth': 3})
    for n in np.arange(1, 8)
] + [
Esempio n. 4
0
def get_ensembles_for_recidivism(test: bool = False):
    c_limits = np.arange(2, 40)
    stbl_cs = lambda: enumerate(c_limits)
    stbl_kw = [{
        'weak_learners': wl_lst
    } for wl_lst in get_weak_learner_inst_list_recidivism(c_limits)]

    kwargs_0 = [{
        **kw, 'penalty': 'l2',
        'alpha': 200,
        'max_rules': None
    } for kw in stbl_kw]
    kwargs_1 = [{
        **kw, 'penalty': 'l2',
        'alpha': 100,
        'max_rules': None
    } for kw in stbl_kw]
    kwargs_2 = [{
        **kw, 'penalty': 'l2',
        'alpha': 50,
        'max_rules': None
    } for kw in stbl_kw]
    kwargs_3 = [{
        **kw, 'penalty': 'l2',
        'alpha': 10,
        'max_rules': None
    } for kw in stbl_kw]
    kwargs_4 = [{
        **kw, 'penalty': 'l2',
        'alpha': 1,
        'max_rules': None
    } for kw in stbl_kw]

    ALL_ENSEMBLES = []
    ALL_ENSEMBLES.append([
        Model('stbl_l2 - mm1_alpha_200', stbl, 'max_complexity', c, 'min_mult',
              1, kwargs_0[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l2 - mm1_alpha_100', stbl, 'max_complexity', c, 'min_mult',
              1, kwargs_1[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l2 - mm1_alpha_50', stbl, 'max_complexity', c, 'min_mult',
              1, kwargs_2[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l2 - mm1_alpha_10', stbl, 'max_complexity', c, 'min_mult',
              1, kwargs_3[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l2 - mm1_alpha_1', stbl, 'max_complexity', c, 'min_mult',
              1, kwargs_4[i]) for i, c in stbl_cs()
    ])

    BEST_ENSEMBLES = []
    BEST_ENSEMBLES += [[
        Model('stbl_l2_mm1', stbl, 'max_complexity', c, 'min_mult', 1,
              kwargs_2[i]) for i, c in stbl_cs()
    ]]

    kwargs_0 = [{
        **kw, 'penalty': 'l1',
        'alpha': 200,
        'max_rules': None
    } for kw in stbl_kw]
    kwargs_1 = [{
        **kw, 'penalty': 'l1',
        'alpha': 100,
        'max_rules': None
    } for kw in stbl_kw]
    kwargs_2 = [{
        **kw, 'penalty': 'l1',
        'alpha': 50,
        'max_rules': None
    } for kw in stbl_kw]
    kwargs_3 = [{
        **kw, 'penalty': 'l1',
        'alpha': 10,
        'max_rules': None
    } for kw in stbl_kw]
    kwargs_4 = [{
        **kw, 'penalty': 'l1',
        'alpha': 1,
        'max_rules': None
    } for kw in stbl_kw]

    ALL_ENSEMBLES.append([
        Model('stbl_l1 - mm0_alpha_200', stbl, 'max_complexity', c, 'min_mult',
              0, kwargs_0[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l1 - mm0_alpha_100', stbl, 'max_complexity', c, 'min_mult',
              0, kwargs_1[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l1 - mm0_alpha_50', stbl, 'max_complexity', c, 'min_mult',
              0, kwargs_2[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l1 - mm0_alpha_10', stbl, 'max_complexity', c, 'min_mult',
              0, kwargs_3[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l1 - mm0_alpha_1', stbl, 'max_complexity', c, 'min_mult',
              0, kwargs_4[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l1 - mm1_alpha_200', stbl, 'max_complexity', c, 'min_mult',
              1, kwargs_0[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l1 - mm1_alpha_100', stbl, 'max_complexity', c, 'min_mult',
              1, kwargs_1[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l1 - mm1_alpha_50', stbl, 'max_complexity', c, 'min_mult',
              1, kwargs_2[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l1 - mm1_alpha_10', stbl, 'max_complexity', c, 'min_mult',
              1, kwargs_3[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l1 - mm1_alpha_1', stbl, 'max_complexity', c, 'min_mult',
              1, kwargs_4[i]) for i, c in stbl_cs()
    ])

    BEST_ENSEMBLES += [[
        Model('stbl_l1_mm0', stbl, 'max_complexity', c, 'min_mult', 0,
              kwargs_2[i]) for i, c in stbl_cs()
    ],
                       [
                           Model('stbl_l1_mm1', stbl, 'max_complexity', c,
                                 'min_mult', 1, kwargs_2[i])
                           for i, c in stbl_cs()
                       ]]

    return ALL_ENSEMBLES if not test else BEST_ENSEMBLES
Esempio n. 5
0
def get_ensembles_hard(test=False):
    stbl_kw_hard = [{
        'weak_learners': wl_lst
    } for wl_lst in get_weak_learner_inst_list(c_limits)]

    kwargs_2 = [{
        **kw, 'penalty': 'l2',
        'alpha': 100,
        'max_rules': None
    } for kw in stbl_kw_hard]
    kwargs_3 = [{
        **kw, 'penalty': 'l2',
        'alpha': 10,
        'max_rules': None
    } for kw in stbl_kw_hard]
    kwargs_4 = [{
        **kw, 'penalty': 'l2',
        'alpha': 1,
        'max_rules': None
    } for kw in stbl_kw_hard]
    kwargs_5 = [{
        **kw, 'penalty': 'l2',
        'alpha': 0.1,
        'max_rules': None
    } for kw in stbl_kw_hard]

    BEST_ENSEMBLES = []
    BEST_ENSEMBLES += [[
        Model('stbl_l2_mm0', stbl, 'max_complexity', c, 'min_mult', 0,
              kwargs_4[i]) for i, c in stbl_cs()
    ],
                       [
                           Model('stbl_l2_mm1', stbl, 'max_complexity', c,
                                 'min_mult', 1, kwargs_4[i])
                           for i, c in stbl_cs()
                       ]]

    ALL_ENSEMBLES = []
    ALL_ENSEMBLES.append([
        Model('stbl_l2 - mm0_alpha_100', stbl, 'max_complexity', c, 'min_mult',
              0, kwargs_2[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l2 - mm0_alpha_10', stbl, 'max_complexity', c, 'min_mult',
              0, kwargs_3[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l2 - mm0_alpha_1', stbl, 'max_complexity', c, 'min_mult',
              0, kwargs_4[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l2 - mm0_alpha_0.1', stbl, 'max_complexity', c, 'min_mult',
              0, kwargs_5[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l2 - mm1_alpha_100', stbl, 'max_complexity', c, 'min_mult',
              1, kwargs_2[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l2 - mm1_alpha_10', stbl, 'max_complexity', c, 'min_mult',
              1, kwargs_3[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l2 - mm1_alpha_1', stbl, 'max_complexity', c, 'min_mult',
              1, kwargs_4[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l2 - mm1_alpha_0.1', stbl, 'max_complexity', c, 'min_mult',
              1, kwargs_5[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l2 - mm2_alpha_100', stbl, 'max_complexity', c, 'min_mult',
              2, kwargs_2[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l2 - mm2_alpha_10', stbl, 'max_complexity', c, 'min_mult',
              2, kwargs_3[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l2 - mm2_alpha_1', stbl, 'max_complexity', c, 'min_mult',
              2, kwargs_4[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l2 - mm2_alpha_0.1', stbl, 'max_complexity', c, 'min_mult',
              2, kwargs_5[i]) for i, c in stbl_cs()
    ])

    kwargs_2 = [{
        **kw, 'penalty': 'l1',
        'alpha': 100,
        'max_rules': None
    } for kw in stbl_kw_hard]
    kwargs_3 = [{
        **kw, 'penalty': 'l1',
        'alpha': 10,
        'max_rules': None
    } for kw in stbl_kw_hard]
    kwargs_4 = [{
        **kw, 'penalty': 'l1',
        'alpha': 1,
        'max_rules': None
    } for kw in stbl_kw_hard]
    kwargs_5 = [{
        **kw, 'penalty': 'l1',
        'alpha': 0.1,
        'max_rules': None
    } for kw in stbl_kw_hard]

    BEST_ENSEMBLES += [[
        Model('stbl_l1_mm0', stbl, 'max_complexity', c, 'min_mult', 0,
              kwargs_4[i]) for i, c in stbl_cs()
    ],
                       [
                           Model('stbl_l1_mm1', stbl, 'max_complexity', c,
                                 'min_mult', 1, kwargs_5[i])
                           for i, c in stbl_cs()
                       ]]

    ALL_ENSEMBLES.append([
        Model('stbl_l1 - mm0_alpha_100', stbl, 'max_complexity', c, 'min_mult',
              0, kwargs_2[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l1 - mm0_alpha_10', stbl, 'max_complexity', c, 'min_mult',
              0, kwargs_3[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l1 - mm0_alpha_1', stbl, 'max_complexity', c, 'min_mult',
              0, kwargs_4[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l1 - mm0_alpha_0.1', stbl, 'max_complexity', c, 'min_mult',
              0, kwargs_5[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l1 - mm1_alpha_100', stbl, 'max_complexity', c, 'min_mult',
              1, kwargs_2[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l1 - mm1_alpha_10', stbl, 'max_complexity', c, 'min_mult',
              1, kwargs_3[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l1 - mm1_alpha_1', stbl, 'max_complexity', c, 'min_mult',
              1, kwargs_4[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l1 - mm1_alpha_0.1', stbl, 'max_complexity', c, 'min_mult',
              1, kwargs_5[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l1 - mm2_alpha_100', stbl, 'max_complexity', c, 'min_mult',
              2, kwargs_2[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l1 - mm2_alpha_10', stbl, 'max_complexity', c, 'min_mult',
              2, kwargs_3[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l1 - mm2_alpha_1', stbl, 'max_complexity', c, 'min_mult',
              2, kwargs_4[i]) for i, c in stbl_cs()
    ] + [
        Model('stbl_l1 - mm2_alpha_0.1', stbl, 'max_complexity', c, 'min_mult',
              2, kwargs_5[i]) for i, c in stbl_cs()
    ])
    return ALL_ENSEMBLES if not test else BEST_ENSEMBLES