def test_model_workflow_partial_mode(): """Run the workflow of deep forest with a local buffer.""" case_kwargs = copy.deepcopy(kwargs) case_kwargs.update({"partial_mode": True}) model = CascadeForestClassifier(**case_kwargs) model.fit(X_train, y_train) # Predictions before saving y_pred_before = model.predict(X_test) # Save and Reload model.save(save_dir) model = CascadeForestClassifier(**case_kwargs) model.load(save_dir) # Predictions after loading y_pred_after = model.predict(X_test) # Make sure the same predictions before and after model serialization assert_array_equal(y_pred_before, y_pred_after) model.clean() # clear the buffer shutil.rmtree(save_dir)
def test_model_workflow_in_memory(backend): """Run the workflow of deep forest with in-memory mode.""" case_kwargs = copy.deepcopy(kwargs) case_kwargs.update({"partial_mode": False}) case_kwargs.update({"backend": backend}) model = CascadeForestClassifier(**case_kwargs) model.fit(X_train, y_train) # Test feature_importances_ if backend == "sklearn": model.get_layer_feature_importances(0) else: with pytest.raises(RuntimeError) as excinfo: model.get_layer_feature_importances(0) assert "Please use the sklearn backend" in str(excinfo.value) # Predictions before saving y_pred_before = model.predict(X_test) # Save and Reload model.save(save_dir) model = CascadeForestClassifier(**case_kwargs) model.load(save_dir) # Make sure the same predictions before and after model serialization y_pred_after = model.predict(X_test) assert_array_equal(y_pred_before, y_pred_after) shutil.rmtree(save_dir)
def test_custom_cascade_layer_workflow_partial_mode(): model = CascadeForestClassifier(partial_mode=True) n_estimators = 4 estimators = [DecisionTreeClassifier() for _ in range(n_estimators)] model.set_estimator(estimators) # set custom base estimators predictor = DecisionTreeClassifier() model.set_predictor(predictor) model.fit(X_train, y_train) y_pred_before = model.predict(X_test) # Save and Reload model.save(save_dir) model = CascadeForestClassifier() model.load(save_dir) # Predictions after loading y_pred_after = model.predict(X_test) # Make sure the same predictions before and after model serialization assert_array_equal(y_pred_before, y_pred_after) model.clean() # clear the buffer shutil.rmtree(save_dir)
def test_model_workflow_in_memory(backend): """Run the workflow of deep forest with in-memory mode.""" case_kwargs = copy.deepcopy(kwargs) case_kwargs.update({"partial_mode": False}) case_kwargs.update({"backend": backend}) model = CascadeForestClassifier(**case_kwargs) model.fit(X_train, y_train) # Predictions before saving y_pred_before = model.predict(X_test) # Save and Reload model.save(save_dir) model = CascadeForestClassifier(**case_kwargs) model.load(save_dir) # Make sure the same predictions before and after model serialization y_pred_after = model.predict(X_test) assert_array_equal(y_pred_before, y_pred_after) shutil.rmtree(save_dir)
def test_classifier_custom_cascade_layer_workflow_in_memory(): model = CascadeForestClassifier() n_estimators = 4 estimators = [DecisionTreeClassifier() for _ in range(n_estimators)] model.set_estimator(estimators) # set custom base estimators predictor = DecisionTreeClassifier() model.set_predictor(predictor) model.fit(X_train_clf, y_train_clf) y_pred_before = model.predict(X_test_clf) # Save and Reload model.save(save_dir) model = CascadeForestClassifier() model.load(save_dir) # Predictions after loading y_pred_after = model.predict(X_test_clf) # 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)