def test_regressor_custom_cascade_layer_workflow_in_memory(y_train): model = CascadeForestRegressor() n_estimators = 4 estimators = [DecisionTreeRegressor() for _ in range(n_estimators)] model.set_estimator(estimators) # set custom base estimators predictor = DecisionTreeRegressor() model.set_predictor(predictor) model.fit(X_train_reg, y_train) y_pred_before = model.predict(X_test_reg) # Save and Reload model.save(save_dir) model = CascadeForestRegressor() model.load(save_dir) # Predictions after loading y_pred_after = model.predict(X_test_reg) # Make sure the same predictions before and after model serialization assert_array_equal(y_pred_before, y_pred_after) assert (model.get_estimator(0, 0, "custom") is model._get_layer(0).estimators_["0-0-custom"].estimator_) model.clean() # clear the buffer shutil.rmtree(save_dir)
def test_custom_base_estimator_invalid_n_splits(): model = CascadeForestRegressor() n_estimators = 4 estimators = [DecisionTreeClassifier() for _ in range(n_estimators)] with pytest.raises(ValueError) as excinfo: model.set_estimator(estimators, n_splits=1) assert "should be at least 2" in str(excinfo.value)
def test_custom_base_estimator_missing_predict(): class tmp_estimator: def __init__(self): pass def fit(self, X, y): pass model = CascadeForestRegressor() with pytest.raises(AttributeError) as excinfo: model.set_estimator([tmp_estimator()]) assert "The `predict` method" in str(excinfo.value) with pytest.raises(AttributeError) as excinfo: model.set_predictor(tmp_estimator()) assert "The `predict` method of the predictor" in str(excinfo.value)