def test_model_properties_after_fitting(): """Check the model properties after fitting a deep forest model.""" model = CascadeForestClassifier(**toy_kwargs) model.fit(X_train, y_train) assert len(model) == model.n_layers_ assert model[0] is model._get_layer(0) with pytest.raises(ValueError) as excinfo: model._get_layer(model.n_layers_) assert "The layer index should be in the range" in str(excinfo.value) with pytest.raises(RuntimeError) as excinfo: model._set_layer(0, None) assert "already exists in the internal container" in str(excinfo.value) with pytest.raises(ValueError) as excinfo: model._get_binner(model.n_layers_ + 1) assert "The binner index should be in the range" in str(excinfo.value) with pytest.raises(RuntimeError) as excinfo: model._set_binner(0, None) assert "already exists in the internal container" in str(excinfo.value) # Test the hook on forest estimator assert ( model.get_forest(0, 0, "rf") is model._get_layer(0).estimators_["0-0-rf"].estimator_ ) with pytest.raises(ValueError) as excinfo: model.get_forest(model.n_layers_, 0, "rf") assert "`layer_idx` should be in the range" in str(excinfo.value) with pytest.raises(ValueError) as excinfo: model.get_forest(0, model.n_estimators, "rf") assert "`est_idx` should be in the range" in str(excinfo.value) with pytest.raises(ValueError) as excinfo: model.get_forest(0, 0, "Unknown") assert "`forest_type` should be one of" in str(excinfo.value)
X, y = shap.datasets.iris() # print("X", X.head()) # print("y", y) feature_names = X.columns X = np.array(X) y = np.array(y) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) model = CascadeForestClassifier(backend="sklearn") model.fit(X_train, y_train) forest = model.get_forest(0, 0, "rf") explainer = shap.TreeExplainer(forest) shap_values = explainer.shap_values(X_test) # shap_values = np.array(explainer.shap_values(X_train)) # print(shap_values.shape) # print(shap_values[2].shape) # shap.summary_plot(shap_values[2], X_train) # # # clf = RandomForestClassifier(n_estimators=100) # clf.fit(X_train, y_train) # explainer = shap.TreeExplainer(clf) # shap_values = np.array(explainer.shap_values(X_test)) # shap.summary_plot(shap_values[0],X_train)