Package: fgclustering
Forest-Guided Clustering - Explainability for Random Forest ModelsDocs|TutorialsForest-Guided Clustering (FGC) is an explainability method for Random Forest models. Standard explainability methods (e.g. feature importance) assume independence of model features and hence, are not suited in the presence of correlated features. The Forest-Guided Clustering algorithm does not assume independence of model features, because it computes the feature importance based on subgroups of instances that follow similar decision rules within the Random Forest model. Hence, this method is well suited for cases with high correlation among model features.For a detailed comparison of FGC and Permutation Feature Importance, please have a look at the NotebookIntroduction to FGC: Comparison of Forest-Guided Clustering and Feature Importance.DocumentationPlease seeherefor full documentation on:Getting Started (installation, basic usage)Theoretical Background (introduction, general algorith, feature importance)Tutorials (simple use cases, special cases)API documentationFor a short introduction to Forest-Guided Clustering, click below:InstallationRequirementsThis packages was tested forPython 3.7 - 3.11on ubuntu, macos and windows. It depends on thekmedoidspython package. If you are using windows or macos, you may need to first install Rust/Cargo with:conda install -c conda-forge rustIf this does not work, please try to install Cargo from source:git clone https://github.com/rust-lang/cargo
cd cargo
cargo build --releaseFor further information on the kmedoids package, please visitthis page.All other required packages are automatically installed if installation is done viapip.Install OptionsThe installation of the package is done via pip. Note: if you are using conda, first install pip with:conda install pip.PyPI install:pip install fgclusteringInstallation from source:git clone https://github.com/HelmholtzAI-Consultants-Munich/fg-clustering.gitInstallation as python package (run inside directory):pip install .Development Installation as python package (run inside directory):pip install -e . [dev]Basic UsageTo get explainability of your Random Forest model via Forest-Guided Clustering, you simply need to run the following commands:fromfgclusteringimportFgClustering# initialize and run fgclustering objectfgc=FgClustering(model=rf,data=data,target_column='target')fgc.run()# visualize resultsfgc.plot_global_feature_importance()fgc.plot_local_feature_importance()fgc.plot_decision_paths()# obtain optimal number of clusters and vector that contains the cluster label of each data pointoptimal_number_of_clusters=fgc.kcluster_labels=fgc.cluster_labelswheremodel=rfis a Random Forest Classifier or Regressor object,data=datais a dataset containing the same features as required by the Random Forest model, andtarget_column='target'is the name of the target column (i.e.target) in the provided dataset.For detailed instructions, please have a look at the NotebookIntroduction to FGC: Simple Use Cases.Usage on big datasetsIf you are working with the dataset containing large number of samples, you can use one of the following strategies:Use the cores you have at your disposal to parallelize the optimization of the cluster number. You can do so by setting the parametern_jobsto a value > 1 in therun()function.Use the faster implementation of the pam method that K-Medoids algorithm uses to find the clusters by setting the parametermethod_clusteringtofasterpamin therun()function.Use subsampling techniqueFor detailed instructions, please have a look at the NotebookSpecial Case: FGC for Big Datasets.ContributingContributions are more than welcome! Everything from code to notebooks to examples and documentation are all equally valuable so please don't feel you can't contribute. To contribute please fork the project make your changes and submit a pull request. We will do our best to work through any issues with you and get your code merged into the main branch.How to citeIf Forest-Guided Clustering is useful for your research, consider citing the package:@software{lisa_sousa_2022_6445529,
    author       = {Lisa Barros de Andrade e Sousa,
                     Helena Pelin,
                     Dominik Thalmeier,
                     Marie Piraud},
    title        = {{Forest-Guided Clustering - Explainability for Random Forest Models}},
    month        = april,
    year         = 2022,
    publisher    = {Zenodo},
    version      = {v0.2.0},
    doi          = {10.5281/zenodo.7085465},
    url          = {https://doi.org/10.5281/zenodo.7085465}
}Licensefgclusteringis released under the MIT license. SeeLICENSEfor additional details about it.