def test_stacked_feature_importance(mock_fit, X_y_regression,
                                    stackable_regressors):
    X, y = X_y_regression
    input_pipelines = [
        RegressionPipeline([regressor]) for regressor in stackable_regressors
    ]
    clf = StackedEnsembleRegressor(input_pipelines=input_pipelines, n_jobs=1)
    clf.fit(X, y)
    mock_fit.assert_called()
    clf._is_fitted = True
    with pytest.raises(NotImplementedError,
                       match="feature_importance is not implemented"):
        clf.feature_importance
def test_stacked_ensemble_multilevel(linear_regression_pipeline_class):
    # checks passing a stacked ensemble classifier as a final estimator
    X = pd.DataFrame(np.random.rand(50, 5))
    y = pd.Series(np.random.rand(50, ))
    base = StackedEnsembleRegressor(
        input_pipelines=[linear_regression_pipeline_class(parameters={})],
        n_jobs=1)
    clf = StackedEnsembleRegressor(
        input_pipelines=[linear_regression_pipeline_class(parameters={})],
        final_estimator=base,
        n_jobs=1)
    clf.fit(X, y)
    y_pred = clf.predict(X)
    assert len(y_pred) == len(y)
    assert not np.isnan(y_pred.to_series()).all()
def test_stacked_ensemble_n_jobs_negative_one(
        X_y_regression, linear_regression_pipeline_class):
    X, y = X_y_regression
    input_pipelines = [linear_regression_pipeline_class(parameters={})]
    clf = StackedEnsembleRegressor(input_pipelines=input_pipelines)
    expected_parameters = {
        "input_pipelines": input_pipelines,
        "final_estimator": None,
        'cv': None,
        'n_jobs': -1
    }
    assert clf.parameters == expected_parameters
    clf.fit(X, y)
    y_pred = clf.predict(X)
    assert len(y_pred) == len(y)
    assert not np.isnan(y_pred.to_series()).all()
def test_stacked_fit_predict_regression(X_y_regression, stackable_regressors):
    X, y = X_y_regression
    input_pipelines = [
        RegressionPipeline([regressor]) for regressor in stackable_regressors
    ]
    clf = StackedEnsembleRegressor(input_pipelines=input_pipelines, n_jobs=1)
    clf.fit(X, y)
    y_pred = clf.predict(X)
    assert len(y_pred) == len(y)
    assert isinstance(y_pred, ww.DataColumn)
    assert not np.isnan(y_pred.to_series()).all()

    clf = StackedEnsembleRegressor(input_pipelines=input_pipelines,
                                   final_estimator=RandomForestRegressor(),
                                   n_jobs=1)
    clf.fit(X, y)
    y_pred = clf.predict(X)
    assert len(y_pred) == len(y)
    assert isinstance(y_pred, ww.DataColumn)
    assert not np.isnan(y_pred.to_series()).all()
def test_stacked_ensemble_init_with_multiple_same_estimators(
        X_y_regression, linear_regression_pipeline_class):
    # Checks that it is okay to pass multiple of the same type of estimator
    X, y = X_y_regression
    input_pipelines = [
        linear_regression_pipeline_class(parameters={}),
        linear_regression_pipeline_class(parameters={})
    ]
    clf = StackedEnsembleRegressor(input_pipelines=input_pipelines, n_jobs=1)
    expected_parameters = {
        "input_pipelines": input_pipelines,
        "final_estimator": None,
        'cv': None,
        'n_jobs': 1
    }
    assert clf.parameters == expected_parameters

    fitted = clf.fit(X, y)
    assert isinstance(fitted, StackedEnsembleRegressor)

    y_pred = clf.predict(X)
    assert len(y_pred) == len(y)
    assert not np.isnan(y_pred.to_series()).all()