Ejemplo n.º 1
0
def test_regressor():

    lr = wrap(LinearRegression, compat=True)
    assert hasattr(lr, "predict")
    assert hasattr(lr, "score")

    lr = wrap(LinearRegression)
    assert hasattr(lr, "normalize")
Ejemplo n.º 2
0
def test_regressor():

    lr = wrap(LinearRegression, compat=True)
    check_estimator(lr)
    assert hasattr(lr, 'predict')
    assert hasattr(lr, 'score')

    lr = wrap(LinearRegression)
    check_estimator(lr)
    assert hasattr(lr, 'normalize')
Ejemplo n.º 3
0
def test_classifier():

    svc = wrap(SVC)
    check_estimator(svc)
    assert hasattr(svc, 'predict')
    assert hasattr(svc, 'decision_function')

    svc_proba = wrap(SVC(probability=True))
    check_estimator(svc_proba)
    assert hasattr(svc_proba, 'predict_proba')
    assert hasattr(svc_proba, 'predict_log_proba')
Ejemplo n.º 4
0
def test_transformer():

    wrap(KernelCenterer, compat=True)

    tr = wrap(KernelCenterer)
    assert hasattr(tr, "transform")

    ss = wrap(StandardScaler)
    # check_estimator(ss) fails because the wrapper is not excluded
    # from tests that are known to fail for StandardScaler...
    assert hasattr(ss, "partial_fit")
    assert hasattr(ss, "inverse_transform")
    assert hasattr(ss, "fit_transform")
Ejemplo n.º 5
0
    def test_reshaping_estimator(self):

        estimator = wrap(ReshapingEstimator(new_shape=(-1, 2)),
                         reshapes='feat_1')

        # test DataArray
        X_da = self.X.var_2d

        y = X_da[:, :2].drop('feat_1')
        y['dummy'] = y.dummy[:, 0]

        estimator.fit(X_da)
        yp = estimator.predict(X_da)

        assert_allclose(yp, y)

        # test Dataset
        X_ds = self.X.var_2d.to_dataset()

        y = X_ds.var_2d[:, :2].drop('feat_1')
        y['dummy'] = y.dummy[:, 0]

        estimator.fit(X_ds)
        yp = estimator.predict(X_ds).var_2d

        assert_allclose(yp, y)
Ejemplo n.º 6
0
def test_ndim_reshaping_estimator():

    X = xr.Dataset(
        {
            'var_1':
            (['sample', 'feat_1', 'feat_2'], np.random.random((100, 10, 10)))
        },
        coords={
            'sample': range(100),
            'feat_1': range(10),
            'feat_2': range(10),
            'dummy': (['sample', 'feat_1'], np.random.random((100, 10)))
        })

    y = X.var_1[:, :5, 0].drop(['feat_1', 'feat_2'])
    y = y.rename({'feat_1': 'feature'})
    y['dummy'] = y.dummy[:, 0]

    estimator = wrap(ReshapingEstimator(new_shape=(-1, 5, 0)),
                     reshapes={'feature': ['feat_1', 'feat_2']})

    estimator.fit(X, X)
    yp = estimator.predict(X).var_1

    assert_allclose(yp, y)
Ejemplo n.º 7
0
    def test_ndim_reshaping_estimator(self):

        estimator = wrap(ReshapingEstimator(new_shape=(-1, 5, 0)),
                         reshapes={'feature': ['feat_1', 'feat_2']})

        # test DataArray
        X_da = self.X.var_3d

        Xt = X_da[:, :5, 0].drop(['feat_1',
                                  'feat_2']).rename({'feat_1': 'feature'})
        Xt['dummy'] = Xt.dummy[:, 0]

        estimator.fit(X_da)
        Xt_da = estimator.transform(X_da)
        Xr_da = estimator.inverse_transform(Xt_da)

        assert_allclose(Xt_da, Xt)

        # test Dataset
        X_ds = self.X.var_3d.to_dataset()

        y = X_ds.var_3d[:, :5, 0].drop(['feat_1', 'feat_2'])
        y = y.rename({'feat_1': 'feature'})
        y['dummy'] = y.dummy[:, 0]

        estimator.fit(X_ds)
        yp = estimator.predict(X_ds).var_3d

        assert_allclose(yp, y)
Ejemplo n.º 8
0
def test_classifier():

    lr = wrap(LogisticRegression)
    # wrappers don't pass check_estimator anymore because estimators
    # "should not set any attribute apart from parameters during init"
    assert hasattr(lr, "predict")
    assert hasattr(lr, "decision_function")

    lr = wrap(LogisticRegression)
    assert hasattr(lr, "C")

    svc_proba = wrap(SVC(probability=True))
    # check_estimator(svc_proba) fails because the wrapper is not excluded
    # from tests that are known to fail for SVC...
    assert hasattr(svc_proba, "predict_proba")
    assert hasattr(svc_proba, "predict_log_proba")
Ejemplo n.º 9
0
    def test_reshaping_transformer(self):

        estimator = wrap(ReshapingEstimator(new_shape=(-1, 2)),
                         reshapes="feat_1")

        # test DataArray
        X_da = self.X.var_3d

        y = X_da[:, :2].drop("feat_1")
        y["dummy"] = y.dummy[:, 0]

        estimator.fit(X_da)
        yp = estimator.transform(X_da)

        assert_allclose(yp, y)

        # test Dataset
        X_ds = self.X.var_2d.to_dataset()

        y = X_ds.var_2d[:, :2].drop("feat_1")
        y["dummy"] = y.dummy[:, 0]

        estimator.fit(X_ds)
        yp = estimator.transform(X_ds).var_2d

        assert_allclose(yp, y)
Ejemplo n.º 10
0
def test_classifier():

    lr = wrap(LogisticRegression, compat=True)
    check_estimator(lr)
    assert hasattr(lr, 'predict')
    assert hasattr(lr, 'decision_function')

    lr = wrap(LogisticRegression)
    check_estimator(lr)
    assert hasattr(lr, 'C')

    svc_proba = wrap(SVC(probability=True))
    # check_estimator(svc_proba) fails because the wrapper is not excluded
    # from tests that are known to fail for SVC...
    assert hasattr(svc_proba, 'predict_proba')
    assert hasattr(svc_proba, 'predict_log_proba')
Ejemplo n.º 11
0
    def test_ndim_reshaping_estimator(self):

        estimator = wrap(
            ReshapingEstimator(new_shape=(-1, 5, 0)),
            reshapes={"feature": ["feat_1", "feat_2"]},
        )

        # test DataArray
        X_da = self.X.var_3d

        Xt = (X_da[:, :5, 0].drop(["feat_1",
                                   "feat_2"]).rename({"feat_1": "feature"}))
        Xt["dummy"] = Xt.dummy[:, 0]

        estimator.fit(X_da)
        Xt_da = estimator.transform(X_da)
        estimator.inverse_transform(Xt_da)

        assert_allclose(Xt_da, Xt)

        # test Dataset
        X_ds = self.X.var_3d.to_dataset()

        y = X_ds.var_3d[:, :5, 0].drop(["feat_1", "feat_2"])
        y = y.rename({"feat_1": "feature"})
        y["dummy"] = y.dummy[:, 0]

        estimator.fit(X_ds)
        yp = estimator.predict(X_ds).var_3d

        assert_allclose(yp, y)
Ejemplo n.º 12
0
    def test_partial_fit(self):

        estimator = wrap(StandardScaler())

        # check pass-through wrapper
        estimator.partial_fit(self.X.var_2d.values)
        assert hasattr(estimator, "mean_")

        with self.assertRaises(ValueError):
            estimator.partial_fit(self.X.var_2d)
        with self.assertRaises(ValueError):
            estimator.partial_fit(self.X)

        # check DataArray wrapper
        estimator = clone(estimator)
        estimator.partial_fit(self.X.var_2d)

        with self.assertRaises(ValueError):
            estimator.partial_fit(self.X.var_2d.values)
        with self.assertRaises(ValueError):
            estimator.partial_fit(self.X)
        assert hasattr(estimator, "mean_")

        # check Dataset wrapper
        estimator = clone(estimator)
        estimator.partial_fit(self.X.var_2d.to_dataset())

        with self.assertRaises(ValueError):
            estimator.partial_fit(self.X.var_2d.values)
        with self.assertRaises(ValueError):
            estimator.partial_fit(self.X.var_2d)
        assert hasattr(estimator, "mean_")
Ejemplo n.º 13
0
    def test_reshaping_estimator_singleton(self):

        estimator = wrap(
            ReshapingEstimator(new_shape=(-1, 0)),
            reshapes='feat_1'
        )

        # test DataArray
        X_da = self.X.var_2d

        y = X_da[:, 0].drop('feat_1')
        estimator.fit(X_da)
        yp = estimator.predict(X_da)

        assert_allclose(yp, y)

        # test Dataset
        X_ds = self.X

        y = X_ds.var_2d[:, 0].drop('feat_1')

        estimator.fit(X_ds)
        yp = estimator.predict(X_ds).var_2d

        assert_allclose(yp, y)
Ejemplo n.º 14
0
def test_transformer():

    ss = wrap(StandardScaler)
    check_estimator(ss)
    assert hasattr(ss, 'partial_fit')
    assert hasattr(ss, 'transform')
    assert hasattr(ss, 'inverse_transform')
    assert hasattr(ss, 'fit_transform')
def test_ndim_dummy_estimator():

    X = xr.DataArray(
        np.random.random((100, 10, 10)),
        coords={'sample': range(100), 'feat_1': range(10), 'feat_2': range(10)},
        dims=['sample', 'feat_1', 'feat_2']
    )

    estimator = wrap(DummyEstimator())

    estimator.fit(X, X)
    yp = estimator.predict(X)

    assert_equal(yp, X)
def test_dummy_transformer():

    X = xr.DataArray(
        np.random.random((100, 10)),
        coords={'sample': range(100), 'feature': range(10)},
        dims=['sample', 'feature']
    )

    estimator = wrap(DummyTransformer())

    estimator.fit(X)
    yp = estimator.transform(X)

    assert_equal(yp, X)
def test_wrapped_transformer():

    from sklearn.preprocessing import StandardScaler

    X = xr.DataArray(
        np.random.random((100, 10)),
        coords={'sample': range(100), 'feature': range(10)},
        dims=['sample', 'feature']
    )

    estimator = wrap(StandardScaler()).partial_fit(X)
    estimator.partial_fit(X)

    assert_allclose(X, estimator.inverse_transform(estimator.transform(X)))
Ejemplo n.º 18
0
def test_dummy_transformer():

    X = xr.Dataset(
        {'var_1': (['sample', 'feature'], np.random.random((100, 10)))},
        coords={
            'sample': range(100),
            'feature': range(10)
        })

    estimator = wrap(DummyTransformer())

    estimator.fit(X)
    yp = estimator.transform(X)

    assert_equal(yp, X)
Ejemplo n.º 19
0
def test_sample_dim():

    from sklearn.decomposition import PCA

    X = xr.Dataset(
        {'var_1': (['feature', 'sample'], np.random.random((10, 100)))},
        coords={
            'sample': range(100),
            'feature': range(10)
        })

    Xt = wrap(PCA(n_components=5), reshapes='feature',
              sample_dim='sample').fit_transform(X)

    npt.assert_equal(Xt.var_1.shape, (5, 100))
def test_score():

    from sklearn.linear_model import LinearRegression

    X = xr.DataArray(
        np.random.random((100, 10)),
        coords={'sample': range(100), 'feature': range(10)},
        dims=['sample', 'feature']
    )

    y = np.random.random(100)

    wrapper = wrap(LinearRegression, reshapes='feature').fit(X, y)

    wrapper.score(X, y)
Ejemplo n.º 21
0
    def test_attributes(self):

        estimator = wrap(StandardScaler())

        # check pass-through wrapper
        estimator.fit(self.X.var_2d.values)
        npt.assert_allclose(estimator.mean_, estimator.estimator_.mean_)

        # check DataArray wrapper
        estimator.fit(self.X.var_2d)
        npt.assert_allclose(estimator.mean_, estimator.estimator_.mean_)

        # check Dataset wrapper
        estimator.fit(self.X.var_2d.to_dataset())
        npt.assert_allclose(estimator.mean_['var_2d'],
                            estimator.estimator_dict_['var_2d'].mean_)
Ejemplo n.º 22
0
def test_score():

    from sklearn.linear_model import LinearRegression

    X = xr.Dataset(
        {'var_1': (['sample', 'feature'], np.random.random((100, 10)))},
        coords={
            'sample': range(100),
            'feature': range(10)
        })

    y = np.random.random(100)

    wrapper = wrap(LinearRegression, reshapes='feature').fit(X, y)

    wrapper.score(X, y)
    def add_quantiles(self, ds_s='ds'):
        from sklearn.preprocessing import QuantileTransformer as QT
        from sklearn_xarray import wrap

        ds: xr.Dataset = getattr(self, ds_s)
        if 'dummy' not in list(ds.dims):
            ds = ds.expand_dims('dummy')

        for r, qr in zip(self.r_cols, self.r_pcols):
            _is_inf = ds[r] == np.inf
            no_inf_max = ds[r].where(~_is_inf).max()
            da = ds[r].copy()
            da = da.where(~_is_inf, no_inf_max)
            qt = wrap(QT, sample_dim='time_utc')
            ds[qr] = qt.fit_transform(da.transpose('time_utc', 'dummy'))

        setattr(self, ds_s, ds)
def test_sample_dim():

    from sklearn.decomposition import PCA

    X = xr.DataArray(
        np.random.random((10, 100)),
        coords={'sample': range(100), 'feature': range(10)},
        dims=['feature', 'sample']
    )

    wrapper = wrap(PCA(n_components=5), reshapes='feature', sample_dim='sample')

    Xt = wrapper.fit_transform(X)
    Xr = wrapper.inverse_transform(Xt)

    npt.assert_equal(Xt.shape, (5, 100))
    npt.assert_equal(Xr.shape, (10, 100))
Ejemplo n.º 25
0
def test_reshaping_estimator_singleton():

    X = xr.Dataset(
        {'var_1': (['sample', 'feature'], np.random.random((100, 10)))},
        coords={
            'sample': range(100),
            'feature': range(10),
            'dummy': (['sample', 'feature'], np.random.random((100, 10)))
        })

    y = X.var_1[:, 0].drop('feature')

    estimator = wrap(ReshapingEstimator(new_shape=(-1, 0)), reshapes='feature')

    estimator.fit(X, X)
    yp = estimator.predict(X).var_1

    assert_allclose(yp, y)
Ejemplo n.º 26
0
    def test_ndim_dummy_estimator(self):

        estimator = wrap(DummyEstimator())

        # test DataArray
        X_da = self.X.var_3d

        estimator.fit(X_da)
        yp = estimator.predict(X_da)

        assert_equal(yp, X_da)

        # test Dataset
        X_ds = self.X

        estimator.fit(X_ds)
        yp = estimator.predict(X_ds)

        assert_equal(yp, X_ds)
Ejemplo n.º 27
0
    def test_wrapped_transformer(self):

        estimator = wrap(StandardScaler())

        # test DataArray
        X_da = self.X.var_2d

        estimator.partial_fit(X_da)

        assert_allclose(X_da,
                        estimator.inverse_transform(estimator.transform(X_da)))

        # test Dataset
        X_ds = self.X.var_2d.to_dataset()

        estimator.fit(X_ds)

        assert_allclose(X_ds,
                        estimator.inverse_transform(estimator.transform(X_ds)))
Ejemplo n.º 28
0
    def test_dummy_transformer(self):

        estimator = wrap(DummyTransformer())

        # test DataArray
        X_da = self.X.var_2d

        estimator.fit(X_da)
        yp = estimator.transform(X_da)

        assert_equal(yp, X_da)

        # test Dataset
        X_ds = self.X

        estimator.fit(X_ds)
        yp = estimator.transform(X_ds)

        assert_equal(yp, X_ds)
Ejemplo n.º 29
0
    def test_update_restore_dims(self):

        estimator = wrap(ReshapingEstimator(new_shape=(-1, 0, 5)),
                         reshapes={'feature': ['feat_1', 'feat_2']})

        X = self.X.var_3d

        estimator.fit(X)

        X_out = estimator.estimator_.transform(X.values)
        dims_new = estimator._update_dims(X, X_out)
        Xt = xr.DataArray(X_out, dims=dims_new)

        assert dims_new == ['sample', 'feature']

        Xr_out = estimator.estimator_.inverse_transform(X_out)
        dims_old = estimator._restore_dims(Xt, Xr_out)

        assert dims_old == ['sample', 'feat_1', 'feat_2']
Ejemplo n.º 30
0
def test_reshaping_transformer():

    X = xr.Dataset(
        {'var_1': (['sample', 'feature'], np.random.random((100, 10)))},
        coords={
            'sample': range(100),
            'feature': range(10),
            'dummy': (['sample', 'feature'], np.random.random((100, 10)))
        })

    y = X.var_1[:, :2].drop('feature')
    y['dummy'] = y.dummy[:, 0]

    estimator = wrap(ReshapingEstimator(new_shape=(-1, 2)), reshapes='feature')

    estimator.fit(X, X)
    yp = estimator.transform(X).var_1

    assert_allclose(yp, y)