Exemplo n.º 1
0
def test_k_fold_splitter():
    # testing without stratification
    result, logs = k_fold_splitter(sample_data, 2, random_state=42)

    assert len(result) == 2

    train_1_idx = result[0][0]
    test_1_idx = result[0][1][0]
    train_2_idx = result[1][0]
    test_2_idx = result[1][1][0]

    assert set(train_1_idx) == set(test_2_idx)
    assert set(test_1_idx) == set(train_2_idx)

    # testing with stratification
    result, logs = k_fold_splitter(sample_data, 2, random_state=42, stratify_column='space')

    assert len(result) == 2

    train_1_idx = result[0][0]
    test_1_idx = result[0][1][0]
    train_2_idx = result[1][0]
    test_2_idx = result[1][1][0]

    assert set(train_1_idx) == set(test_2_idx)
    assert set(test_1_idx) == set(train_2_idx)

    train_1_strat = sample_data.iloc[train_1_idx]['space']
    test_1_strat = sample_data.iloc[test_1_idx]['space']
    train_2_strat = sample_data.iloc[train_2_idx]['space']
    test_2_strat = sample_data.iloc[test_2_idx]['space']

    assert train_1_strat.nunique() == test_2_strat.nunique()
    assert train_2_strat.nunique() == test_1_strat.nunique()
Exemplo n.º 2
0
from toolz import curry
from fklearn.tuning.parameter_tuners import grid_search_cv
from fklearn.training.regression import linear_regression_learner
from fklearn.validation.splitters import k_fold_splitter
from fklearn.validation.evaluators import mse_evaluator

space = {
    "intercept": lambda: [0, 1],
}


@curry
def param_train_fn(space, train_set):
    return linear_regression_learner(
        features=["income"],
        target="target",
        params={"fit_intercept": space["intercept"]},
    )(train_set)


split_fn = k_fold_splitter(n_splits=2)
tuning_log = grid_search_cv(space,
                            df,
                            param_train_fn=param_train_fn,
                            split_fn=split_fn,
                            eval_fn=mse_evaluator)

for run in tuning_log:
    print(f"mse score in this run per fold: {run['validator_log']}")
    print(f"with parameters: {run['iter_space']}")
Exemplo n.º 3
0
def split_fn():
    return k_fold_splitter(n_splits=2, random_state=30)