### Install Documentation Dependencies Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/CONTRIBUTING.md Install dependencies required to build the project documentation locally. Recommended to use a separate environment. ```bash pip install -r docs/requirements.txt ``` -------------------------------- ### Install Development Dependencies Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/CONTRIBUTING.md Install the necessary dependencies for local development, including testing and linting tools. ```bash pip install -e ".[dev]" ``` -------------------------------- ### Running the HiClass Example Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/full_example.md Execute the saved Python script from your terminal to run the HiClass example. ```bash python hello_hiclass.py ``` -------------------------------- ### Failed HiClass Import Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/verify.md This example demonstrates the expected output when HiClass is not installed correctly. A ModuleNotFoundError will be raised, indicating an issue with the installation. ```python Python 3.9.7 (default, Sep 16 2021, 13:09:58) [GCC 7.5.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>> import hiclass Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'hiclass' ``` -------------------------------- ### Install Pipenv Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/pipenv.md Installs the pipenv package using pip. ```bash pip install pipenv ``` -------------------------------- ### Install HiClass using pip Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/install.md Install the base HiClass package from PyPI. This is the recommended method. ```bash pip install hiclass ``` -------------------------------- ### Install Git Hooks Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/CONTRIBUTING.md Set up pre-commit hooks to automatically format code and catch potential issues before committing. ```bash pre-commit install ``` -------------------------------- ### Activate Conda Virtual Environment Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/conda.md Activate the 'hiclass-environment' to start using the isolated Python installation and its packages. ```bash conda activate hiclass-environment ``` -------------------------------- ### Expected Output of HiClass Example Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/full_example.md The predicted hierarchical labels after running the example script. ```python [['Animal' 'Reptile' 'Lizard'] ['Animal' 'Reptile' 'Snake'] ['Animal' 'Mammal' 'Cow'] ['Animal' 'Mammal' 'Sheep']] ``` -------------------------------- ### Install HiClass with optional packages Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/install.md Install HiClass along with optional packages, such as 'ray' for parallel processing. Replace '' with the desired extra. ```bash pip install hiclass"[]" ``` -------------------------------- ### Install HiClass using conda Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/install.md Install HiClass from the conda-forge channel. Use this if you prefer conda for package management. ```bash conda install -c conda-forge hiclass --yes ``` -------------------------------- ### Install Snakemake Environment Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/benchmarks/consumer_complaints/README.md Clone the HiClass repository and create a Conda environment for Snakemake using the provided YAML file. ```shell git clone https://github.com/scikit-learn-contrib/hiclass.git cd hiclass/benchmarks/consumer_complaints conda env create --name snakemake --file envs/snakemake.yml ``` -------------------------------- ### LocalClassifierPerNode Example Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/classifiers.md Demonstrates fitting and predicting with the LocalClassifierPerNode. This snippet shows a basic usage scenario with sample data. ```python >>> from hiclass import LocalClassifierPerNode >>> y = [['1', '1.1'], ['2', '2.1']] >>> X = [[1, 2], [3, 4]] >>> lcpn = LocalClassifierPerNode() >>> lcpn.fit(X, y) >>> lcpn.predict(X) array([['1', '1.1'], ['2', '2.1']]) ``` -------------------------------- ### Activate Pipenv Shell Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/pipenv.md Starts a new shell session with the Pipenv virtual environment activated. ```bash pipenv shell ``` -------------------------------- ### Full HiClass Example with RandomForest Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/full_example.md This snippet demonstrates how to train a LocalClassifierPerNode using RandomForestClassifier for hierarchical classification. Save this code as hello_hiclass.py. ```python """Contents of hello_hiclass.py""" from hiclass import LocalClassifierPerNode from sklearn.ensemble import RandomForestClassifier # Define data X_train = [[1], [2], [3], [4]] X_test = [[4], [3], [2], [1]] Y_train = [ ['Animal', 'Mammal', 'Sheep'], ['Animal', 'Mammal', 'Cow'], ['Animal', 'Reptile', 'Snake'], ['Animal', 'Reptile', 'Lizard'], ] # Use random forest classifiers for every node rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf) # Train local classifier per node classifier.fit(X_train, Y_train) # Predict predictions = classifier.predict(X_test) print(predictions) ``` -------------------------------- ### Random Forest Hyperparameter Tuning Configuration Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/benchmarks/consumer_complaints/README.md Example YAML configuration for tuning Random Forest hyperparameters using Optuna and Hydra. Defines parameter choices for n_estimators and criterion. ```yaml defaults: - _self_ - optuna hydra: sweeper: params: n_estimators: choice(100, 200) criterion: choice("gini", "entropy", "log_loss") n_estimators: 1 criterion: 1 ``` -------------------------------- ### LessInclusivePolicy Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/utilities.md Implements the less inclusive policy, inheriting from InclusivePolicy. It provides methods to initialize the policy and retrieve positive and negative examples for a given node, with a different definition for negative examples. ```APIDOC ## class BinaryPolicy.LessInclusivePolicy(digraph: DiGraph, X: ndarray, y: ndarray, sample_weight=None) ### Description Implement the less inclusive policy of the referenced paper. ### Parameters * **digraph** (*nx.DiGraph*) – DiGraph which is used for inferring nodes relationships. * **X** (*np.ndarray*) – Features which will be used for fitting a model. * **y** (*np.ndarray*) – Labels which will be assigned to the different samples. Has to be 2D array. * **sample_weight** (*array-like* *of* *shape* *(**n_samples* *,* *)* *,* *default=None*) – Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight. ## Method: __init__ ### Description Initialize a BinaryPolicy with the required data. ### Parameters * **digraph** (*nx.DiGraph*) – DiGraph which is used for inferring nodes relationships. * **X** (*np.ndarray*) – Features which will be used for fitting a model. * **y** (*np.ndarray*) – Labels which will be assigned to the different samples. Has to be 2D array. * **sample_weight** (*array-like* *of* *shape* *(**n_samples* *,* *)* *,* *default=None*) – Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight. ## Method: get_binary_examples(node) ### Description Gather all positive and negative examples for a given node. ### Parameters **node** – Node for which the positive and negative examples should be searched. ### Returns * **X** (*np.ndarray*) – The subset with positive and negative features. * **y** (*np.ndarray*) – The subset with positive and negative labels. ## Method: negative_examples(node) ### Description Gather all negative examples corresponding to the given node. This includes all examples except the examples for the current node and its children. ### Parameters **node** – Node for which the negative examples should be searched. ### Returns **negative_examples** – A mask for which examples are included (True) and which are not. * **Return type:** np.ndarray ## Method: positive_examples(node) ### Description Gather all positive examples corresponding to the given node. This includes examples for the given node and its descendants. ### Parameters **node** – Node for which the positive examples should be searched. ### Returns **positive_examples** – A mask for which examples are included (True) and which are not. * **Return type:** np.ndarray ``` -------------------------------- ### Successful HiClass Import Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/verify.md Run this code in a Python interpreter to verify a successful HiClass installation. A successful import indicates the library is ready for use. ```python Python 3.8.13 (default, Mar 28 2022, 11:38:47) [GCC 7.5.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>> import hiclass >>> ``` -------------------------------- ### get_params Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/classifiers.md Gets the parameters of the estimator, including those of nested objects. ```APIDOC ## get_params(deep=True) ### Description Get parameters for this estimator. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **deep** (*bool*, *default=True*) – If True, will return the parameters for this estimator and contained subobjects that are estimators. ### Returns **params** – Parameter names mapped to their values. ### Return type dict ``` -------------------------------- ### Train and Predict with LocalClassifierPerNode Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/README.md This snippet shows how to train a LocalClassifierPerNode using RandomForestClassifier for each node and make predictions. Ensure scikit-learn and hiclass are installed. ```python from hiclass import LocalClassifierPerNode from sklearn.ensemble import RandomForestClassifier # Define data X_train = [[1], [2], [3], [4]] X_test = [[4], [3], [2], [1]] Y_train = [ ['Animal', 'Mammal', 'Sheep'], ['Animal', 'Mammal', 'Cow'], ['Animal', 'Reptile', 'Snake'], ['Animal', 'Reptile', 'Lizard'], ] # Use random forest classifiers for every node rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf) # Train local classifier per node classifier.fit(X_train, Y_train) # Predict predictions = classifier.predict(X_test) ``` -------------------------------- ### Define Data for Pipeline Example Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/README.md Prepare text data and corresponding hierarchical labels for training a scikit-learn pipeline with HiClass. This data is used to demonstrate feature extraction and hierarchical classification. ```python from hiclass import LocalClassifierPerParentNode from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer from sklearn.linear_model import LogisticRegression from sklearn.pipeline import Pipeline # Define data X_train = [ 'Struggling to repay loan', 'Unable to get annual report', ] X_test = [ 'Unable to get annual report', 'Struggling to repay loan', ] Y_train = [ ['Loan', 'Student loan'], ['Credit reporting', 'Reports'] ] ``` -------------------------------- ### InclusivePolicy Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/utilities.md Implements the inclusive policy for binary classification. It provides methods to initialize the policy and retrieve positive and negative examples for a given node. ```APIDOC ## class BinaryPolicy.InclusivePolicy(digraph: DiGraph, X: ndarray, y: ndarray, sample_weight=None) ### Description Implement the inclusive policy of the referenced paper. ### Parameters * **digraph** (*nx.DiGraph*) – DiGraph which is used for inferring nodes relationships. * **X** (*np.ndarray*) – Features which will be used for fitting a model. * **y** (*np.ndarray*) – Labels which will be assigned to the different samples. Has to be 2D array. * **sample_weight** (*array-like* *of* *shape* *(**n_samples* *,* *)* *,* *default=None*) – Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight. ## Method: __init__ ### Description Initialize a BinaryPolicy with the required data. ### Parameters * **digraph** (*nx.DiGraph*) – DiGraph which is used for inferring nodes relationships. * **X** (*np.ndarray*) – Features which will be used for fitting a model. * **y** (*np.ndarray*) – Labels which will be assigned to the different samples. Has to be 2D array. * **sample_weight** (*array-like* *of* *shape* *(**n_samples* *,* *)* *,* *default=None*) – Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight. ## Method: get_binary_examples(node) ### Description Gather all positive and negative examples for a given node. ### Parameters **node** – Node for which the positive and negative examples should be searched. ### Returns * **X** (*np.ndarray*) – The subset with positive and negative features. * **y** (*np.ndarray*) – The subset with positive and negative labels. ## Method: negative_examples(node) ### Description Gather all negative examples corresponding to the given node. This includes all examples, except the examples for the given node, its descendants and successors. ### Parameters **node** – Node for which the negative examples should be searched. ### Returns **negative_examples** – A mask for which examples are included (True) and which are not. * **Return type:** np.ndarray ## Method: positive_examples(node) ### Description Gather all positive examples corresponding to the given node. This includes examples for the given node and its descendants. ### Parameters **node** – Node for which the positive examples should be searched. ### Returns **positive_examples** – A mask for which examples are included (True) and which are not. * **Return type:** np.ndarray ``` -------------------------------- ### get_params(deep=True) Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/classifiers.md Gets the parameters of the estimator, with an option to include nested estimators. ```APIDOC ## get_params(deep=True) ### Description Get parameters for this estimator. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **deep** (*bool*, *default=True*) – If True, will return the parameters for this estimator and contained subobjects that are estimators. ### Returns * **params** – Parameter names mapped to their values. ### Return type dict ``` -------------------------------- ### Build Documentation Locally Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/CONTRIBUTING.md Navigate to the docs directory and build the HTML documentation. This allows for previewing changes. ```bash cd docs make html ``` -------------------------------- ### Create and Navigate to Environment Directory Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/pipenv.md Creates a new directory for the virtual environment and changes the current directory to it. ```bash mkdir hiclass-environment && cd hiclass-environment ``` -------------------------------- ### Fit and Predict with LocalClassifierPerLevel Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/classifiers.md Demonstrates how to initialize, fit, and predict using the LocalClassifierPerLevel class with sample data. ```python >>> from hiclass import LocalClassifierPerLevel >>> y = [['1', '1.1'], ['2', '2.1']] >>> X = [[1, 2], [3, 4]] >>> lcpl = LocalClassifierPerLevel() >>> lcpl.fit(X, y) >>> lcpl.predict(X) array([['1', '1.1'], ['2', '2.1']]) ``` -------------------------------- ### Initialize and Use FlatClassifier Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/classifiers.md Demonstrates how to initialize a FlatClassifier, fit it with sample data and hierarchical labels, and then predict using the fitted classifier. This snippet shows the basic workflow for using the FlatClassifier. ```python >>> from hiclass import FlatClassifier >>> y = [['1', '1.1'], ['2', '2.1']] >>> X = [[1, 2], [3, 4]] >>> flat = FlatClassifier() >>> flat.fit(X, y) >>> flat.predict(X) array([['1', '1.1'], ['2', '2.1']]) ``` -------------------------------- ### Initialize and Use LocalClassifierPerParentNode Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/classifiers.md Demonstrates how to initialize, fit, and predict using the LocalClassifierPerParentNode. This is useful for basic hierarchical classification tasks. ```python >>> from hiclass import LocalClassifierPerParentNode >>> y = [['1', '1.1'], ['2', '2.1']] >>> X = [[1, 2], [3, 4]] >>> lcppn = LocalClassifierPerParentNode() >>> lcppn.fit(X, y) >>> lcppn.predict(X) array([['1', '1.1'], ['2', '2.1']]) ``` -------------------------------- ### FlatClassifier.__init__() Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/index.md Initializes a FlatClassifier instance. This is the constructor for the FlatClassifier class, setting up the initial state of the classifier. ```APIDOC ## FlatClassifier.__init__() ### Description Initializes a FlatClassifier instance. ### Method __init__ ### Parameters (No parameters explicitly documented in the source for __init__) ``` -------------------------------- ### BinaryPolicy.SiblingsPolicy Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/utilities.md Implements the siblings policy for binary classification. It is used to infer node relationships and retrieve positive and negative examples for a given node. ```APIDOC ## class BinaryPolicy.SiblingsPolicy ### Description Implements the siblings policy of the referenced paper. ### Parameters * **digraph** (*nx.DiGraph*) – DiGraph which is used for inferring nodes relationships. * **X** (*np.ndarray*) – Features which will be used for fitting a model. * **y** (*np.ndarray*) – Labels which will be assigned to the different samples. Has to be 2D array. * **sample_weight** (*array-like* *of* *shape* *(**n_samples* *,* *)* *,* *default=None*) – Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight. ## method get_binary_examples(node) ### Description Gather all positive and negative examples for a given node. ### Parameters * **node** – Node for which the positive and negative examples should be searched. ### Returns * **X** (*np.ndarray*) – The subset with positive and negative features. * **y** (*np.ndarray*) – The subset with positive and negative labels. ## method negative_examples(node) ### Description Gather all negative examples corresponding to the given node. This includes all examples for nodes that have the same ancestors as the given node, as well as their descendants. ### Parameters * **node** – Node for which the negative examples should be searched. ### Returns * **negative_examples** (*np.ndarray*) – A mask for which examples are included (True) and which are not. ## method positive_examples(node) ### Description Gather all positive examples corresponding to the given node. This includes examples for the given node and its descendants. ### Parameters * **node** – Node for which the positive examples should be searched. ### Returns * **positive_examples** (*np.ndarray*) – A mask for which examples are included (True) and which are not. ``` -------------------------------- ### Instantiate LocalClassifierPerLevel Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/local_classifier.md Create a LocalClassifierPerLevel model using a RandomForestClassifier for each level in the hierarchy. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerLevel(local_classifier=rf) ``` -------------------------------- ### LessExclusivePolicy Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/utilities.md Implements the less exclusive policy, a variation of the binary policy. It allows for gathering positive and negative examples with specific criteria for exclusion. ```APIDOC ## class BinaryPolicy.LessExclusivePolicy ### Description Implement the less exclusive policy of the referenced paper. ### Parameters * **digraph** (*nx.DiGraph*) – DiGraph which is used for inferring nodes relationships. * **X** (*np.ndarray*) – Features which will be used for fitting a model. * **y** (*np.ndarray*) – Labels which will be assigned to the different samples. Has to be 2D array. * **sample_weight** (*array-like* *of* *shape* *(**n_samples* *,* *)* *,* *default=None*) – Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight. ## get_binary_examples(node) ### Description Gather all positive and negative examples for a given node. ### Parameters * **node** – Node for which the positive and negative examples should be searched. ### Returns * **X** (*np.ndarray*) – The subset with positive and negative features. * **y** (*np.ndarray*) – The subset with positive and negative labels. ## negative_examples(node) ### Description Gather all negative examples corresponding to the given node. This includes all examples except the examples for the current node and its children. ### Parameters * **node** – Node for which the negative examples should be searched. ### Returns **negative_examples** (*np.ndarray*) – A mask for which examples are included (True) and which are not. ## positive_examples(node) ### Description Gather all positive examples corresponding to the given node. This only includes examples for the given node. ### Parameters * **node** – Node for which the positive examples should be searched. ### Returns **positive_examples** (*np.ndarray*) – A mask for which examples are included (True) and which are not. ``` -------------------------------- ### Activate Virtual Environment (Unix-like) Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/venv.md Activates the previously created Python virtual environment. This command is for Unix-like systems (Linux, macOS). After activation, your shell prompt will change to indicate the active environment. ```bash source env/hiclass-environment/bin/activate ``` -------------------------------- ### ExclusivePolicy Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/utilities.md Implements the exclusive policy for binary classification. It provides methods to initialize the policy and gather positive and negative examples for a given node. ```APIDOC ## class BinaryPolicy.ExclusivePolicy ### Description Implements the exclusive policy of the referenced paper. ### Parameters * **digraph** (*nx.DiGraph*) – DiGraph which is used for inferring nodes relationships. * **X** (*np.ndarray*) – Features which will be used for fitting a model. * **y** (*np.ndarray*) – Labels which will be assigned to the different samples. Has to be 2D array. * **sample_weight** (*array-like* *of* *shape* *(**n_samples* *,* *)* *,* *default=None*) – Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight. ## get_binary_examples(node) ### Description Gather all positive and negative examples for a given node. ### Parameters * **node** – Node for which the positive and negative examples should be searched. ### Returns * **X** (*np.ndarray*) – The subset with positive and negative features. * **y** (*np.ndarray*) – The subset with positive and negative labels. ## negative_examples(node) ### Description Gather all negative examples corresponding to the given node. This includes all examples except the positive ones. ### Parameters * **node** – Node for which the negative examples should be searched. ### Returns **negative_examples** (*np.ndarray*) – A mask for which examples are included (True) and which are not. ## positive_examples(node) ### Description Gather all positive examples corresponding to the given node. This only includes examples for the given node. ### Parameters * **node** – Node for which the positive examples should be searched. ### Returns **positive_examples** (*np.ndarray*) – A mask for which examples are included (True) and which are not. ``` -------------------------------- ### Instantiate LocalClassifierPerNode Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/local_classifier.md Create a LocalClassifierPerNode model using a RandomForestClassifier for each node, excluding the root. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf) ``` -------------------------------- ### BinaryPolicy.ExclusiveSiblingsPolicy Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/utilities.md Implements the exclusive siblings policy for binary classification. It is used to infer node relationships and retrieve positive and negative examples for a given node. ```APIDOC ## class BinaryPolicy.ExclusiveSiblingsPolicy ### Description Implement the exclusive siblings policy of the referenced paper. ### Parameters * **digraph** (*nx.DiGraph*) – DiGraph which is used for inferring nodes relationships. * **X** (*np.ndarray*) – Features which will be used for fitting a model. * **y** (*np.ndarray*) – Labels which will be assigned to the different samples. Has to be 2D array. * **sample_weight** (*array-like* *of* *shape* *(**n_samples* *,* *)* *,* *default=None*) – Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight. ## method get_binary_examples(node) ### Description Gather all positive and negative examples for a given node. ### Parameters * **node** – Node for which the positive and negative examples should be searched. ### Returns * **X** (*np.ndarray*) – The subset with positive and negative features. * **y** (*np.ndarray*) – The subset with positive and negative labels. ## method negative_examples(node) ### Description Gather all negative examples corresponding to the given node. This includes examples for all nodes that have the same parent as the given node. ### Parameters * **node** – Node for which the negative examples should be searched. ### Returns * **negative_examples** (*np.ndarray*) – A mask for which examples are included (True) and which are not. ## method positive_examples(node) ### Description Gather all positive examples corresponding to the given node. This only includes examples for the given node. ### Parameters * **node** – Node for which the positive examples should be searched. ### Returns * **positive_examples** (*np.ndarray*) – A mask for which examples are included (True) and which are not. ``` -------------------------------- ### set_fit_request(sample_weight: bool | None | str = '$UNCHANGED$') Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/classifiers.md Configures metadata routing for the `fit` method, specifying whether `sample_weight` metadata should be requested. ```APIDOC ## set_fit_request(sample_weight: bool | None | str = '$UNCHANGED$') ### Description Configure whether metadata should be requested to be passed to the `fit` method. Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with `enable_metadata_routing=True` (see `sklearn.set_config()`). Please check the User Guide on how the routing mechanism works. The options for each parameter are: - `True`: metadata is requested, and passed to `fit` if provided. The request is ignored if metadata is not provided. - `False`: metadata is not requested and the meta-estimator will not pass it to `fit`. - `None`: metadata is not requested, and the meta-estimator will raise an error if the user provides it. - `str`: metadata should be passed to the meta-estimator with this given alias instead of the original name. The default (`sklearn.utils.metadata_routing.UNCHANGED`) retains the existing request. This allows you to change the request for some parameters and not others. #### Versionadded Added in version 1.3. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **sample_weight** (*str*, *True*, *False*, or *None*, *default=sklearn.utils.metadata_routing.UNCHANGED*) – Metadata routing for `sample_weight` parameter in `fit`. ### Returns * **self** – The updated object. ### Return type object ``` -------------------------------- ### Running HiClass Pipeline Locally with Snakemake Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/benchmarks/consumer_complaints/README.md Command to activate the conda environment and run the Snakemake pipeline locally. Includes flags for continued execution, printing commands, reasoning, conda usage, and core count. ```shell conda activate snakemake snakemake --keep-going --printshellcmds --reason --use-conda --cores 48 ``` -------------------------------- ### Create Python Virtual Environment (Unix-like) Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/venv.md Creates a new Python virtual environment named 'hiclass-environment' inside an 'env' subdirectory using the `venv` module. This command is for Unix-like systems (Linux, macOS). ```bash python -m venv env/hiclass-environment ``` -------------------------------- ### Instantiate LocalClassifierPerParentNode Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/local_classifier.md Create a LocalClassifierPerParentNode model using a RandomForestClassifier for each parent node. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerParentNode(local_classifier=rf) ``` -------------------------------- ### Run Local Tests Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/CONTRIBUTING.md Execute the test suite locally with coverage reporting. Ensures code quality and adherence to standards. ```bash pytest -v --cov=hiclass --cov-fail-under=90 --cov-report html ``` -------------------------------- ### Create Python Virtual Environment (Windows) Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/venv.md Creates a new Python virtual environment named 'hiclass-environment' inside an 'env' subdirectory using the `venv` module. This command is for Windows systems. ```bash python -m venv env\hiclass-environment ``` -------------------------------- ### Activate Virtual Environment (Windows) Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/venv.md Activates the previously created Python virtual environment. This command is for Windows systems. After activation, your shell prompt will change to indicate the active environment. ```bash .\env\hiclass-environment\Scripts\activate ``` -------------------------------- ### Import LocalClassifierPerNode Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/local_classifier.md Import the LocalClassifierPerNode class and RandomForestClassifier for use in local hierarchical classification. ```python from hiclass import LocalClassifierPerNode from sklearn.ensemble import RandomForestClassifier ``` -------------------------------- ### Import LocalClassifierPerLevel Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/local_classifier.md Import the LocalClassifierPerLevel class and RandomForestClassifier for use in local hierarchical classification. ```python from hiclass import LocalClassifierPerLevel from sklearn.ensemble import RandomForestClassifier ``` -------------------------------- ### Local Classifier Per Node with Isotonic Calibration Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/algorithms/calibration.md Demonstrates training and calibrating a LocalClassifierPerNode using RandomForestClassifier with isotonic regression and multiply probability aggregation. Requires scikit-learn and hiclass. ```python from sklearn.ensemble import RandomForestClassifier from hiclass import LocalClassifierPerNode # Define data X_train = [[1], [2], [3], [4]] X_test = [[4], [3], [2], [1]] X_cal = [[5], [6], [7], [8]] Y_train = [ ["Animal", "Mammal", "Sheep"], ["Animal", "Mammal", "Cow"], ["Animal", "Reptile", "Snake"], ["Animal", "Reptile", "Lizard"], ] Y_cal = [ ["Animal", "Mammal", "Cow"], ["Animal", "Mammal", "Sheep"], ["Animal", "Reptile", "Lizard"], ["Animal", "Reptile", "Snake"], ] # Use random forest classifiers for every node rf = RandomForestClassifier() # Use local classifier per node with isotonic regression as calibration method classifier = LocalClassifierPerNode( local_classifier=rf, calibration_method="isotonic", probability_combiner="multiply" ) # Train local classifier per node classifier.fit(X_train, Y_train) # Calibrate local classifier per node classifier.calibrate(X_cal, Y_cal) # Predict probabilities probabilities = classifier.predict_proba(X_test) # Print probabilities and labels for the last level print(classifier.classes_[2]) print(probabilities) ``` -------------------------------- ### Train and Predict with Pipeline Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/README.md Execute the training and prediction steps using the previously defined scikit-learn pipeline. This demonstrates the end-to-end workflow for hierarchical multi-label classification with feature extraction. ```python # Train local classifier per parent node pipeline.fit(X_train, Y_train) # Predict predictions = pipeline.predict(X_test) ``` -------------------------------- ### Import LocalClassifierPerParentNode Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/local_classifier.md Import the LocalClassifierPerParentNode class and RandomForestClassifier for use in local hierarchical classification. ```python from hiclass import LocalClassifierPerParentNode from sklearn.ensemble import RandomForestClassifier ``` -------------------------------- ### Create Conda Virtual Environment Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/get_started/conda.md Use this command to create a new isolated Python 3.8 environment named 'hiclass-environment'. The '--yes' flag automatically confirms prompts. ```bash conda create --name hiclass-environment python=3.8 --yes ``` -------------------------------- ### Calculate F1 Score with Different Averaging Methods Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/utilities.md Demonstrates how to compute the F1 score using 'micro' and 'macro' averaging. Ensure y_true and y_pred are properly formatted hierarchical labels. ```python >>> import numpy as np >>> from hiclass.metrics import f1 >>> y_true = [[0, 1, 2], [3, 4, 5]] >>> y_pred = [[0, 1, 2], [6, 7, 8]] >>> f1(y_true, y_pred, average='micro') 0.5 >>> f1(y_true, y_pred, average='macro') 0.5 ``` -------------------------------- ### fit(X, y, sample_weight=None) Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/classifiers.md Fits a local classifier per node using the provided training data and optional sample weights. ```APIDOC ## fit(X, y, sample_weight=None) ### Description Fit a local classifier per node. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **X** (*{array-like*, *sparse matrix}* *of* *shape* *(**n_samples*, *n_features*) – The training input samples. Internally, its dtype will be converted to `dtype=np.float32`. If a sparse matrix is provided, it will be converted into a sparse `csr_matrix`. * **y** (*array-like* *of* *shape* *(**n_samples*, *n_levels*) – The target values, i.e., hierarchical class labels for classification. * **sample_weight** (*array-like* *of* *shape* *(**n_samples*,)* *,* *default=None*) – Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight. ### Returns * **self** – Fitted estimator. ### Return type object ``` -------------------------------- ### Snakemake Cluster Job Submission with Slurm Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/benchmarks/consumer_complaints/README.md This command submits a Snakemake pipeline to a Slurm cluster. It configures both the `srun` command for Snakemake execution and the parameters for individual jobs submitted to Slurm. ```shell srun \ --account \ --mem=G \ --cpus-per-task= \ --time=-:: \ --partition \ snakemake \ --keep-going \ --printshellcmds \ --reason \ --use-conda \ --cores \ --resources mem_gb= \ --restart-times \ --jobs \ --cluster \ "sbatch \ --account \ --partition \ --mem=G \ --cpus-per-task= \ --time=-::" ``` -------------------------------- ### Handle Zero Division in F1 Score Calculation Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/utilities.md Shows how to manage cases where the F1 score is ill-defined due to zero division. The `zero_division` parameter can be set to 0.0, 1.0, or np.nan to control the output. ```python >>> # zero division >>> y_true = [[0, 1], [2, 3]] >>> y_pred = [[4, 5], [6, 7]] >>> f1(y_true, y_pred) F-score is ill-defined and being set to 0.0. Use `zero_division` parameter to control this behavior. 0.0 >>> f1(y_true, y_pred, zero_division=1.0) 1.0 >>> f1(y_true, y_pred, zero_division=np.nan) nan ``` -------------------------------- ### FlatClassifier Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/classifiers.md Initializes a FlatClassifier. This classifier flattens the hierarchy internally and uses a provided scikit-learn estimator for classification. ```APIDOC ## class FlatClassifier.FlatClassifier(local_classifier: BaseEstimator = LogisticRegression()) ### Description A flat classifier utility that accepts as input a hierarchy and flattens it internally. ### Parameters #### `__init__` Parameters - **local_classifier** (BaseEstimator, default=LogisticRegression) - The scikit-learn model used for the flat classification. Needs to have fit, predict and clone methods. ``` -------------------------------- ### FlatClassifier.fit() Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/index.md Fits the FlatClassifier model to the training data. This method trains the classifier using the provided input features and target labels. ```APIDOC ## FlatClassifier.fit() ### Description Fits the FlatClassifier model to the training data. ### Method fit ### Parameters (No parameters explicitly documented in the source for fit) ``` -------------------------------- ### set_fit_request Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/classifiers.md Configures metadata routing for the `fit` method. ```APIDOC ## set_fit_request(, sample_weight: bool | None | str = '$UNCHANGED$') -> [LocalClassifierPerParentNode](#LocalClassifierPerParentNode.LocalClassifierPerParentNode) ### Description Configure whether metadata should be requested to be passed to the `fit` method. Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with `enable_metadata_routing=True` (see `sklearn.set_config()`). Please check the User Guide on how the routing mechanism works. The options for each parameter are: - `True`: metadata is requested, and passed to `fit` if provided. The request is ignored if metadata is not provided. - `False`: metadata is not requested and the meta-estimator will not pass it to `fit`. - `None`: metadata is not requested, and the meta-estimator will raise an error if the user provides it. - `str`: metadata should be passed to the meta-estimator with this given alias instead of the original name. The default (`sklearn.utils.metadata_routing.UNCHANGED`) retains the existing request. This allows you to change the request for some parameters and not others. #### Versionadded Added in version 1.3. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **sample_weight** (*str*, *True*, *False*, or *None*, *default=sklearn.utils.metadata_routing.UNCHANGED*) – Metadata routing for `sample_weight` parameter in `fit`. ### Returns **self** – The updated object. ### Return type object ``` -------------------------------- ### set_params Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/classifiers.md Sets the parameters of the estimator. ```APIDOC ## set_params(**params) ### Description Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as `Pipeline`). The latter have parameters of the form `__` so that it’s possible to update each component of a nested object. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * ** **params** (*dict*) – Estimator parameters. ### Returns **self** – Estimator instance. ### Return type estimator instance ``` -------------------------------- ### get_params Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/classifiers.md Retrieves the parameters of the estimator, optionally including parameters of contained sub-estimators. This is useful for inspecting the configuration of the classifier. ```APIDOC ## get_params(deep=True) ### Description Get parameters for this estimator. ### Method GET (implied) ### Parameters #### Query Parameters - **deep** (bool) - Optional - If True, will return the parameters for this estimator and contained subobjects that are estimators. ### Returns - **params** (dict) - Parameter names mapped to their values. ### Return type dict ``` -------------------------------- ### calibrate Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/classifiers.md Fits a local calibrator per node using the provided data. ```APIDOC ## calibrate(X, y) ### Description Fit a local calibrator per node. ### Parameters * **X** (array-like or sparse matrix of shape (n_samples, n_features)) – The calibration input samples. * **y** (array-like of shape (n_samples, n_levels)) – The target values, i.e., hierarchical class labels for classification. ### Returns self – Calibrated estimator. ### Return type object ``` -------------------------------- ### Build Scikit-learn Pipeline with HiClass Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/README.md Construct a scikit-learn pipeline that includes text vectorization and a HiClass LocalClassifierPerParentNode. This pipeline is designed to handle sparse matrices efficiently. ```python # Use logistic regression classifiers for every parent node lr = LogisticRegression() pipeline = Pipeline([ ('count', CountVectorizer()), ('tfidf', TfidfTransformer()), ('lcppn', LocalClassifierPerParentNode(local_classifier=lr)), ]) ``` -------------------------------- ### set_params Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/classifiers.md Sets the parameters of this estimator. This method works on simple estimators as well as on nested objects. ```APIDOC ## set_params(**params) ### Description Set the parameters of this estimator. ### Parameters * ** **params** (dict) – Estimator parameters. ### Returns self – Estimator instance. ### Return type estimator instance ``` -------------------------------- ### get_metadata_routing() Source: https://github.com/scikit-learn-contrib/hiclass/blob/main/docs/source/api/classifiers.md Retrieves the metadata routing configuration for the estimator. ```APIDOC ## get_metadata_routing() ### Description Get metadata routing of this object. Please check User Guide on how the routing mechanism works. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Returns * **routing** – A `MetadataRequest` encapsulating routing information. ### Return type MetadataRequest ```