Example #1
0
def test_BoxCoxTargetTransformer():

    np.random.seed(123)
    X = np.random.randn(100, 10)
    y = np.exp(np.random.randn(100))

    X2 = np.random.randn(100, 10) * 2

    for ll in (0, 0.1, 0.5, 2):

        bb = BoxCoxTargetTransformer(Ridge(), ll=ll)

        bb.fit(X, y)

        yhat = bb.predict(X)
        yhat2 = bb.predict(X2)

        assert yhat.ndim == 1
        assert yhat.shape[0] == y.shape[0]

        assert yhat2.ndim == 1
        assert yhat2.shape[0] == y.shape[0]
Example #2
0
def test_approx_cross_validation_BoxCoxTargetTransformer():

    np.random.seed(123)
    X = np.random.randn(100, 10)
    y = np.exp(np.random.randn(100))

    for ll in (0, 0.1, 0.5, 2):

        # Scorer entered as a string #

        bb = BoxCoxTargetTransformer(Ridge(), ll=ll)
        cv_res1, yhat1 = bb.approx_cross_validation(
            X,
            y,
            scoring=["neg_mean_squared_error"],
            cv=10,
            return_predict=True)

        assert isinstance(cv_res1, pd.DataFrame)
        assert cv_res1.shape[0] == 10
        assert "test_neg_mean_squared_error" in cv_res1
        assert "train_neg_mean_squared_error" in cv_res1

        assert yhat1.ndim == 1
        assert yhat1.shape[0] == y.shape[0]

        with pytest.raises(NotFittedError):
            bb.predict(X)

        with pytest.raises(NotFittedError):
            bb.model.predict(X)

        #########################################
        ###  Scorer entered as a dictionnary  ###
        #########################################
        scoring = create_scoring(Ridge(), ["neg_mean_squared_error"])
        cv_res2, yhat2 = bb.approx_cross_validation(X,
                                                    y,
                                                    scoring=scoring,
                                                    cv=10,
                                                    return_predict=True)

        assert isinstance(cv_res2, pd.DataFrame)
        assert cv_res2.shape[0] == 10
        assert "test_neg_mean_squared_error" in cv_res2
        assert "train_neg_mean_squared_error" in cv_res2

        assert yhat2.ndim == 1
        assert yhat2.shape[0] == y.shape[0]

        with pytest.raises(NotFittedError):
            bb.predict(X)

        with pytest.raises(NotFittedError):
            bb.model.predict(X)

        assert np.abs(cv_res2["test_neg_mean_squared_error"] -
                      cv_res1["test_neg_mean_squared_error"]).max() <= 10**(-5)
        assert np.abs(cv_res2["train_neg_mean_squared_error"] -
                      cv_res1["train_neg_mean_squared_error"]).max() <= 10**(
                          -5)

        assert np.max(np.abs(yhat2 - yhat1)) <= 10**(-5)