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)
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)
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): args = {
"""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)