Beispiel #1
0
def run(cls, job, eval=True):
    """Run a test"""
    if job == 'fit':
        lr, _ = est.get_learner(cls, True, False)
        lr.dtype = np.float64
    else:
        lr = run(cls, 'fit', False)

    data = Data(cls, True, False, True)
    X, y = data.get_data((25, 4), 3)

    if job in ['fit', 'transform']:
        (F, wf), _ = data.ground_truth(X, y, data.indexer.partitions)
    else:
        _, (F, wf) = data.ground_truth(X, y, data.indexer.partitions)

    args = {'fit': [X, y], 'transform': [X], 'predict': [X]}[job]

    P = _run(lr, job, *args, return_preds=True)
    if not eval:
        return lr

    np.testing.assert_array_equal(P, F)

    if job in ['fit', 'transform']:
        lrs = lr.sublearners
    else:
        lrs = lr.learner

    w = [obj.estimator.coef_ for obj in lrs]
    np.testing.assert_array_equal(w, wf)
Beispiel #2
0
def test_transformer():
    """[Parallel | Transform] test run transformer as estimator"""
    _, tr = est.get_learner('stack', True, True)

    data = Data('stack', True, True, True)
    X, y = data.get_data((25, 4), 3)

    F = _run(tr, 'fit', X, y, return_preds=True)
    H = _run(tr, 'transform', X, y, return_preds=True)
    P = _run(tr, 'predict', X, y, return_preds=True)
    Z, _ = tr.estimator.fit_transform(X)
    G = _run(tr, 'fit', X, y, return_preds=True, refit=False)

    np.testing.assert_array_equal(H, F)
    np.testing.assert_array_equal(Z, P)
    np.testing.assert_array_equal(G, F)
Beispiel #3
0
from mlens.testing import Data
from mlens.testing.dummy import ESTIMATORS, PREPROCESSING
from mlens.utils.dummy import OLS, Scale
from mlens.utils.exceptions import NotFittedError
from mlens.parallel import make_group
from mlens.estimators import LearnerEstimator, TransformerEstimator, LayerEnsemble
from mlens.externals.sklearn.base import clone

try:
    from sklearn.utils.estimator_checks import check_estimator
    run_sklearn = True
except ImportError:
    check_estimator = None
    run_sklearn = False

data = Data('stack', False, True)
X, y = data.get_data((25, 4), 3)
(F, wf), (P, wp) = data.ground_truth(X, y)

Est = LayerEnsemble
est = LayerEnsemble(make_group(FoldIndex(), ESTIMATORS, PREPROCESSING),
                    dtype=np.float64)


class Tmp(Est):
    """Temporary class

    Wrapper to get full estimator on no-args instantiation. For compatibility
    with older Scikit-learn versions.
    """
    def __init__(self):
Beispiel #4
0
import numpy as np
from mlens.index import FoldIndex
from mlens.utils.dummy import OLS, Scale
from mlens.utils.exceptions import ParameterChangeWarning
from mlens.testing import Data
from mlens.estimators import LearnerEstimator, TransformerEstimator, LayerEnsemble
from mlens.externals.sklearn.base import clone

try:
    from sklearn.utils.estimator_checks import check_estimator
    run_sklearn = True
except ImportError:
    check_estimator = None
    run_sklearn = False

data = Data('stack', False, False)
X, y = data.get_data((25, 4), 3)

est = TransformerEstimator(Scale(), FoldIndex(), dtype=np.float64)

Est = TransformerEstimator


class Tmp(Est):
    """Temporary class

    Wrapper to get full estimator on no-args instantiation. For compatibility
    with older Scikit-learn versions.
    """
    def __init__(self):
        args = {
Beispiel #5
0
"""ML-ENSEMBLE


"""
import numpy as np
from mlens.testing.dummy import OLS, ECM
from mlens.testing import Data

from mlens.ensemble import Subsemble, SuperLearner

data = Data('subsemble', False, False, partitions=2, folds=3)
X, y = data.get_data((30, 4), 3)
data.indexer.fit(X)
(F, wf), (P, wp) = data.ground_truth(X, y, data.indexer.partitions)


def test_subset_fit():
    """[Subsemble] 'fit' and 'predict' runs correctly."""
    meta = OLS()
    meta.fit(F, y)
    g = meta.predict(P)

    ens = Subsemble()
    ens.add(ECM, partitions=2, folds=3, dtype=np.float64)
    ens.add_meta(OLS(), dtype=np.float64)

    ens.fit(X, y)

    pred = ens.predict(X)
    np.testing.assert_array_equal(pred, g)
from mlens.metrics import mape, make_scorer
from mlens.utils.exceptions import FitFailedWarning
from mlens.utils.dummy import OLS, Scale
from mlens.testing import Data
from scipy.stats import randint

try:
    from contextlib import redirect_stdout, redirect_stderr
except ImportError:
    from mlens.externals.fixes import redirect as redirect_stdout
    redirect_stderr = redirect_stdout

np.random.seed(100)

# Stack is nonsense here - we just need proba to be false
X, y = Data('stack', False, False).get_data((100, 2), 20)


def failed_score(p, y):
    """Bad scoring function to test exception handling."""
    raise ValueError("This fails.")


mape_scorer = make_scorer(mape, greater_is_better=False)
bad_scorer = make_scorer(failed_score)


def test_check():
    """[Model Selection] Test check of valid estimator."""
    np.testing.assert_raises(ValueError, Evaluator, mape)