### Run HiClass Script Source: https://hiclass.readthedocs.io/en/latest/get_started/hello_hiclass.html This command executes a HiClass script. It's a basic example to get started. ```bash lightcone run "ship it" ``` -------------------------------- ### Get Binary Examples for ExclusivePolicy Source: https://hiclass.readthedocs.io/en/latest/api/utilities.html Gathers all positive and negative examples for a given node using the ExclusivePolicy. ```python get_binary_examples(_node_) -> tuple ``` -------------------------------- ### Full Example with Inclusive Policy Source: https://hiclass.readthedocs.io/en/latest/auto_examples/plot_binary_policies.html A complete example demonstrating the use of `LocalClassifierPerNode` with the 'inclusive' binary policy for hierarchical classification. ```python from sklearn.ensemble import RandomForestClassifier from hiclass import LocalClassifierPerNode # 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 # And inclusive policy to select training examples for binary classifiers. rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf, binary_policy="inclusive") # Train local classifier per node classifier.fit(X_train, Y_train) # Predict predictions = classifier.predict(X_test) print(predictions) ``` -------------------------------- ### Get Positive Examples for ExclusivePolicy Source: https://hiclass.readthedocs.io/en/latest/api/utilities.html Gathers all positive examples corresponding to the given node. ```python positive_examples(_node_) -> ndarray ``` -------------------------------- ### Inclusive Policy Example Source: https://hiclass.readthedocs.io/en/latest/_sources/auto_examples/plot_binary_policies.rst.txt Select the 'inclusive' binary policy for training binary classifiers. This policy includes all relevant examples for training, making it a comprehensive choice. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf, binary_policy="inclusive") ``` -------------------------------- ### Install Pipenv Source: https://hiclass.readthedocs.io/en/latest/_sources/get_started/pipenv.rst.txt Install the pipenv package using pip. ```bash pip install pipenv ``` -------------------------------- ### Example Data Source: https://hiclass.readthedocs.io/en/latest/auto_examples/plot_pipeline.html This snippet shows an example of hierarchical data labels. ```text [['Credit reporting' 'Reports'] ['Loan' 'Student loan']] ``` -------------------------------- ### Verify Unsuccessful HiClass Import Source: https://hiclass.readthedocs.io/en/latest/_sources/get_started/verify.rst.txt This example demonstrates the expected output when HiClass is not installed correctly. A ModuleNotFoundError will be raised if the import fails. ```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' ``` -------------------------------- ### Unsuccessful HiClass Import Source: https://hiclass.readthedocs.io/en/latest/get_started/verify.html This example shows the expected error message if HiClass is not installed correctly. ```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 HiClass with optional packages Source: https://hiclass.readthedocs.io/en/latest/_sources/get_started/install.rst.txt Install HiClass along with optional packages, such as 'ray' for parallel processing support, by specifying the extra name in brackets. ```bash pip install hiclass"[]" ``` -------------------------------- ### Install HiClass using pip Source: https://hiclass.readthedocs.io/en/latest/_sources/get_started/install.rst.txt Use this command to install the base HiClass package from the Python Package Index (PyPI). ```bash pip install hiclass ``` -------------------------------- ### Run the HiClass Example Script Source: https://hiclass.readthedocs.io/en/latest/get_started/full_example.html Execute the saved `hello_hiclass.py` script from your terminal to see the predictions. ```bash python hello_hiclass.py ``` -------------------------------- ### Activate a Conda Virtual Environment Source: https://hiclass.readthedocs.io/en/latest/get_started/conda.html Activate the 'hiclass-environment' to start using the isolated Python installation and its packages. ```bash conda activate hiclass-environment ``` -------------------------------- ### Activate Conda Virtual Environment Source: https://hiclass.readthedocs.io/en/latest/_sources/get_started/conda.rst.txt Activate the 'hiclass-environment' to start using the isolated Python installation. This command must be run from any folder. ```bash conda activate hiclass-environment ``` -------------------------------- ### Get Negative Examples for ExclusivePolicy Source: https://hiclass.readthedocs.io/en/latest/api/utilities.html Gathers all negative examples corresponding to the given node, including all examples except the positive ones. ```python negative_examples(_node_) -> ndarray ``` -------------------------------- ### Running the HiClass Example Source: https://hiclass.readthedocs.io/en/latest/_sources/get_started/full_example.rst.txt This bash command executes the Python script 'hello_hiclass.py' to perform the hierarchical classification. ```bash python hello_hiclass.py ``` -------------------------------- ### Full Example with Data and Calibration Source: https://hiclass.readthedocs.io/en/latest/_sources/auto_examples/plot_calibration.rst.txt This snippet demonstrates a complete workflow including data definition, model initialization with isotonic calibration, fitting, calibration, and probability prediction. ```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"], ] classifier = LocalClassifierPerNode( local_classifier=RandomForestClassifier(), calibration_method='isotonic' ) classifier.fit(X_train, Y_train) classifier.calibrate(X_cal, Y_cal) print(classifier.predict_proba(X_test)) ``` -------------------------------- ### Install HiClass using conda Source: https://hiclass.readthedocs.io/en/latest/_sources/get_started/install.rst.txt Install HiClass from the conda-forge channel using the conda package manager. The '--yes' flag automatically confirms the installation. ```bash conda install -c conda-forge hiclass --yes ``` -------------------------------- ### Beta Scaling Calibration Source: https://hiclass.readthedocs.io/en/latest/auto_examples/plot_calibration.html Example of setting up a LocalClassifierPerNode with Beta scaling for calibration. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode( local_classifier=rf, calibration_method='beta' ) ``` -------------------------------- ### Full HiClass 'Hello World' Example Source: https://hiclass.readthedocs.io/en/latest/get_started/full_example.html This Python script demonstrates a basic HiClass workflow. It initializes data, sets up a LocalClassifierPerNode with a RandomForestClassifier, trains the model, and makes predictions. 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) ``` -------------------------------- ### CVAP Calibration Source: https://hiclass.readthedocs.io/en/latest/auto_examples/plot_calibration.html Example of setting up a LocalClassifierPerNode with CVAP for calibration. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode( local_classifier=rf, calibration_method='cvap' ) ``` -------------------------------- ### Initialize and Use LocalClassifierPerLevel Source: https://hiclass.readthedocs.io/en/latest/api/classifiers.html Demonstrates how to initialize, fit, and predict using the LocalClassifierPerLevel class with sample data. Ensure the hiclass library is installed and imported. ```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']]) ``` -------------------------------- ### Siblings Policy Example Source: https://hiclass.readthedocs.io/en/latest/_sources/auto_examples/plot_binary_policies.rst.txt Use the 'siblings' binary policy for training binary classifiers. This policy focuses on examples relevant to sibling nodes, providing a specific training context. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf, binary_policy="siblings") ``` -------------------------------- ### Platt Scaling Calibration Source: https://hiclass.readthedocs.io/en/latest/auto_examples/plot_calibration.html Example of setting up a LocalClassifierPerNode with Platt scaling for calibration. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode( local_classifier=rf, calibration_method='platt' ) ``` -------------------------------- ### Less Inclusive Policy Example Source: https://hiclass.readthedocs.io/en/latest/_sources/auto_examples/plot_binary_policies.rst.txt Employ the 'less_inclusive' binary policy when training binary classifiers. This policy is less strict than 'inclusive' in selecting training examples. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf, binary_policy="less_inclusive") ``` -------------------------------- ### Isotonic Regression Calibration Source: https://hiclass.readthedocs.io/en/latest/auto_examples/plot_calibration.html Example of setting up a LocalClassifierPerNode with Isotonic Regression for calibration. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode( local_classifier=rf, calibration_method='isotonic' ) ``` -------------------------------- ### Full HiClass Example with LocalClassifierPerNode Source: https://hiclass.readthedocs.io/en/latest/_sources/get_started/full_example.rst.txt This Python script demonstrates how to use HiClass's LocalClassifierPerNode with scikit-learn's RandomForestClassifier for hierarchical classification. It includes data definition, classifier setup, training, and prediction. ```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) ``` -------------------------------- ### IVAP Calibration Source: https://hiclass.readthedocs.io/en/latest/auto_examples/plot_calibration.html Example of setting up a LocalClassifierPerNode with IVAP for calibration. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode( local_classifier=rf, calibration_method='ivap' ) ``` -------------------------------- ### Parallel Training Setup and Execution Source: https://hiclass.readthedocs.io/en/latest/_downloads/52a055614dd23e43e343ad1f56f599f8/plot_parallel_training.ipynb This snippet sets up a pipeline for hierarchical text classification and configures it for parallel training using all available CPU cores. It includes necessary imports, data loading, classifier instantiation, and the fitting process. A workaround for a documentation-specific stdout fileno issue is also included. ```python import sys from os import cpu_count from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer from sklearn.linear_model import LogisticRegression from sklearn.pipeline import Pipeline from hiclass import LocalClassifierPerParentNode from hiclass.datasets import load_hierarchical_text_classification # Load train and test splits X_train, X_test, Y_train, Y_test = load_hierarchical_text_classification() # We will use logistic regression classifiers for every parent node lr = LogisticRegression(max_iter=1000) pipeline = Pipeline( [ ("count", CountVectorizer()), ("tfidf", TfidfTransformer()), ( "lcppn", LocalClassifierPerParentNode(local_classifier=lr, n_jobs=cpu_count()), ), ] ) # Fixes bug AttributeError: '_LoggingTee' object has no attribute 'fileno' # This only happens when building the documentation # Hence, you don't actually need it for your code to work sys.stdout.fileno = lambda: False # Now, let's train the local classifier per parent node pipeline.fit(X_train, Y_train) ``` -------------------------------- ### Verify Successful HiClass Import Source: https://hiclass.readthedocs.io/en/latest/_sources/get_started/verify.rst.txt Run this code in a Python interpreter to confirm a successful HiClass installation. If the import statement executes without errors, the installation is likely correct. ```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 >>> ``` -------------------------------- ### Less Exclusive Policy Example Source: https://hiclass.readthedocs.io/en/latest/_sources/auto_examples/plot_binary_policies.rst.txt Utilize the 'less_exclusive' binary policy for training binary classifiers. This policy allows for a broader selection of training examples compared to the 'exclusive' policy. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf, binary_policy="less_exclusive") ``` -------------------------------- ### Pipeline with HiClass and Sparse Matrices Source: https://hiclass.readthedocs.io/en/latest/auto_examples/plot_pipeline.html This example demonstrates building a scikit-learn Pipeline that includes HiClass's LocalClassifierPerParentNode, along with feature extraction steps that produce sparse matrices. It shows how to train and predict using this pipeline. ```python from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer from sklearn.linear_model import LogisticRegression from sklearn.pipeline import Pipeline from hiclass import LocalClassifierPerParentNode # 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"]] # We will use logistic regression classifiers for every parent node lr = LogisticRegression() # Let's build a pipeline using CountVectorizer and TfidfTransformer # to extract features as sparse matrices pipeline = Pipeline( [ ("count", CountVectorizer()), ("tfidf", TfidfTransformer()), ("lcppn", LocalClassifierPerParentNode(local_classifier=lr)), ] ) # Now, let's train a local classifier per parent node pipeline.fit(X_train, Y_train) # Finally, let's predict using the pipeline predictions = pipeline.predict(X_test) print(predictions) ``` -------------------------------- ### HiClass Example Data Output Source: https://hiclass.readthedocs.io/en/latest/auto_examples/plot_hello_hiclass.html This snippet shows the expected hierarchical structure of the training data labels. ```text [['Animal' 'Reptile' 'Lizard'] ['Animal' 'Reptile' 'Snake'] ['Animal' 'Mammal' 'Cow'] ['Animal' 'Mammal' 'Sheep']] ``` -------------------------------- ### Parallel Training Pipeline Setup Source: https://hiclass.readthedocs.io/en/latest/auto_examples/plot_parallel_training.html This snippet shows the configuration of a scikit-learn Pipeline for hierarchical text classification using `LocalClassifierPerParentNode`. It demonstrates setting `n_jobs` to 2, indicating parallel processing. This setup is beneficial for larger datasets where training time can be reduced by utilizing multiple CPU cores. ```python Pipeline(steps=[('count', CountVectorizer()), ('tfidf', TfidfTransformer()), ('lcppn', LocalClassifierPerParentNode(local_classifier=LogisticRegression(max_iter=1000), n_jobs=2))]) ``` -------------------------------- ### Activate Pipenv Shell Source: https://hiclass.readthedocs.io/en/latest/_sources/get_started/pipenv.rst.txt Start a new shell session with the Pipenv virtual environment activated. ```bash pipenv shell ``` -------------------------------- ### No Probability Aggregation Source: https://hiclass.readthedocs.io/en/latest/auto_examples/plot_calibration.html Example of setting up a LocalClassifierPerNode with Isotonic Regression and no probability aggregation (None). ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode( local_classifier=rf, calibration_method='isotonic', probability_combiner=None ) ``` -------------------------------- ### Multiply Probability Combiner Source: https://hiclass.readthedocs.io/en/latest/auto_examples/plot_calibration.html Example of setting up a LocalClassifierPerNode with Isotonic Regression and 'multiply' probability combiner. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode( local_classifier=rf, calibration_method='isotonic', probability_combiner='multiply' ) ``` -------------------------------- ### Arithmetic Probability Combiner Source: https://hiclass.readthedocs.io/en/latest/auto_examples/plot_calibration.html Example of setting up a LocalClassifierPerNode with Isotonic Regression and 'arithmetic' probability combiner. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode( local_classifier=rf, calibration_method='isotonic', probability_combiner='arithmetic' ) ``` -------------------------------- ### Instantiate LocalClassifierPerNode Source: https://hiclass.readthedocs.io/en/latest/_sources/get_started/local_classifier.rst.txt Create an instance of LocalClassifierPerNode using a RandomForestClassifier. This setup is specific to the per-node classification strategy. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf) ``` -------------------------------- ### Instantiate LocalClassifierPerLevel Source: https://hiclass.readthedocs.io/en/latest/_sources/get_started/local_classifier.rst.txt Create an instance of LocalClassifierPerLevel using a RandomForestClassifier. This setup is designed for the per-level classification strategy. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerLevel(local_classifier=rf) ``` -------------------------------- ### Expected Output of HiClass Example Source: https://hiclass.readthedocs.io/en/latest/_sources/get_started/full_example.rst.txt This is the expected output when the 'hello_hiclass.py' script is run, showing the hierarchical predictions made by the trained HiClass model. ```python [['Animal' 'Reptile' 'Lizard'] ['Animal' 'Reptile' 'Snake'] ['Animal' 'Mammal' 'Cow'] ['Animal' 'Mammal' 'Sheep']] ``` -------------------------------- ### Inclusive Binary Policy Source: https://hiclass.readthedocs.io/en/latest/auto_examples/plot_binary_policies.html Sets the binary policy to 'inclusive'. This policy selects training examples for binary classifiers. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf, binary_policy="inclusive") ``` -------------------------------- ### Geometric Probability Combiner Source: https://hiclass.readthedocs.io/en/latest/auto_examples/plot_calibration.html Example of setting up a LocalClassifierPerNode with Isotonic Regression and 'geometric' probability combiner. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode( local_classifier=rf, calibration_method='isotonic', probability_combiner='geometric' ) ``` -------------------------------- ### LocalClassifierPerParentNode Example Source: https://hiclass.readthedocs.io/en/latest/api/classifiers.html Demonstrates fitting and predicting with the LocalClassifierPerParentNode. This snippet shows a basic usage scenario with sample data. ```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']]) ``` -------------------------------- ### Less Inclusive Binary Policy Source: https://hiclass.readthedocs.io/en/latest/auto_examples/plot_binary_policies.html Sets the binary policy to 'less_inclusive'. This policy offers a less strict inclusion of training examples. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf, binary_policy="less_inclusive") ``` -------------------------------- ### Train and Predict with HiClass LocalClassifierPerNode Source: https://hiclass.readthedocs.io/en/latest/_downloads/9d7747807e3a54d9b046f541de93742a/plot_hello_hiclass.ipynb This snippet demonstrates a basic hierarchical classification workflow using HiClass. It defines sample training and testing data, initializes a LocalClassifierPerNode with a RandomForestClassifier, trains the model, and performs predictions. Ensure scikit-learn and hiclass are installed. ```python from sklearn.ensemble import RandomForestClassifier from hiclass import LocalClassifierPerNode # 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) ``` -------------------------------- ### Create and Navigate to Project Directory Source: https://hiclass.readthedocs.io/en/latest/get_started/pipenv.html Create a new directory for your project and change into it. ```bash mkdir hiclass-environment && cd hiclass-environment ``` -------------------------------- ### Create Project Directory Source: https://hiclass.readthedocs.io/en/latest/_sources/get_started/pipenv.rst.txt Create a new directory for your project and navigate into it. ```bash mkdir hiclass-environment && cd hiclass-environment ``` -------------------------------- ### Create Python Virtual Environment Source: https://hiclass.readthedocs.io/en/latest/_sources/get_started/venv.rst.txt Initializes a new Python virtual environment within the specified directory. Use the appropriate path separator for your operating system. ```bash python -m venv env/hiclass-environment ``` ```bash python -m venv env\hiclass-environment ``` -------------------------------- ### Initialize and Use FlatClassifier Source: https://hiclass.readthedocs.io/en/latest/api/classifiers.html Demonstrates how to initialize a FlatClassifier, fit it with sample data and hierarchical labels, and then predict using the fitted classifier. The classifier uses a LogisticRegression model by default. ```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']]) ``` -------------------------------- ### Exclusive Policy Example Source: https://hiclass.readthedocs.io/en/latest/_sources/auto_examples/plot_binary_policies.rst.txt Use the 'exclusive' binary policy when training binary classifiers. This policy is suitable for scenarios where each node's classifier should only be trained on examples relevant to its direct children. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf, binary_policy="exclusive") ``` -------------------------------- ### Basic Parallel Training with n_jobs Source: https://hiclass.readthedocs.io/en/latest/_sources/auto_examples/plot_parallel_training.rst.txt Demonstrates how to enable parallel training for estimators that support the `n_jobs` parameter. Set `n_jobs` to -1 to use all available CPU cores. ```python from sklearn.datasets import load_iris from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split # Load a sample dataset X, y = load_iris(return_X_y=True) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # Initialize a classifier with n_jobs=-1 for parallel training # This will use all available CPU cores for training rf_classifier = RandomForestClassifier(n_estimators=100, n_jobs=-1, random_state=42) # Train the classifier rf_classifier.fit(X_train, y_train) # Make predictions y_pred = rf_classifier.predict(X_test) # Evaluate the model (optional) from sklearn.metrics import accuracy_score accuracy = accuracy_score(y_test, y_pred) print(f"Accuracy: {accuracy:.2f}") ``` -------------------------------- ### LocalClassifierPerParentNode.get_params Source: https://hiclass.readthedocs.io/en/latest/api/classifiers.html Gets parameters for this estimator. ```APIDOC ## get_params ### Description Get parameters for this estimator. ### Parameters * **deep** (bool, default=True) - If True, will return the parameters for this estimator and contained subobjects. ### Returns * **params** - Parameter names mapped to their values. ### Return type dict ``` -------------------------------- ### LocalClassifierPerNode with different binary policies Source: https://hiclass.readthedocs.io/en/latest/_downloads/f879c30c3cb546984e18821ff19c74e0/plot_binary_policies.ipynb Demonstrates how to instantiate `LocalClassifierPerNode` with different `binary_policy` settings. Each code tab shows a different policy. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf, binary_policy="exclusive") ``` ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf, binary_policy="less_exclusive") ``` ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf, binary_policy="less_inclusive") ``` ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf, binary_policy="inclusive") ``` ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf, binary_policy="siblings") ``` ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf, binary_policy="exclusive_siblings") ``` -------------------------------- ### LocalClassifierPerParentNode.get_metadata_routing Source: https://hiclass.readthedocs.io/en/latest/api/classifiers.html Gets the metadata routing of this object. ```APIDOC ## get_metadata_routing ### Description Get metadata routing of this object. ### Returns * **routing** - A `MetadataRequest` encapsulating routing information. ### Return type MetadataRequest ``` -------------------------------- ### Activate Virtual Environment (Linux/macOS) Source: https://hiclass.readthedocs.io/en/latest/get_started/venv.html Activates the previously created virtual environment. This command must be run in the shell where you intend to use HiClass. ```bash source env/hiclass-environment/bin/activate ``` -------------------------------- ### Create Virtual Environment (Linux/macOS) Source: https://hiclass.readthedocs.io/en/latest/get_started/venv.html Creates a new virtual environment named 'hiclass-environment' within an 'env' subdirectory. This command is for Unix-like systems. ```bash python -m venv env/hiclass-environment ``` -------------------------------- ### get_params Source: https://hiclass.readthedocs.io/en/latest/api/classifiers.html Gets the parameters for this estimator, including parameters of nested objects if deep is True. ```APIDOC ## get_params ### Description Get parameters for this estimator. ### Parameters #### Parameters - **deep** (bool, default=True) - 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 ``` -------------------------------- ### Calibrating Classifier Directly Source: https://hiclass.readthedocs.io/en/latest/auto_examples/plot_calibration.html Demonstrates calibrating a LocalClassifierPerNode directly using .fit() and .calibrate() methods. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode( local_classifier=rf, calibration_method='isotonic' ) classifier.fit(X_train, Y_train) classifier.calibrate(X_cal, Y_cal) classifier.predict_proba(X_test) ``` -------------------------------- ### get_params Source: https://hiclass.readthedocs.io/en/latest/api/classifiers.html Get parameters for this estimator. This method is inherited from BaseEstimator and is used for hyperparameter tuning. ```APIDOC ## get_params(deep=True) ### Description Get parameters for this estimator. ### 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. ``` -------------------------------- ### Activate Python Virtual Environment Source: https://hiclass.readthedocs.io/en/latest/_sources/get_started/venv.rst.txt Activates the previously created virtual environment. This command differs based on your operating system. ```bash source env/hiclass-environment/bin/activate ``` ```bash .\env\hiclass-environment\Scripts\activate ``` -------------------------------- ### Activate Virtual Environment (Windows) Source: https://hiclass.readthedocs.io/en/latest/get_started/venv.html Activates the previously created virtual environment. This command must be run in the shell where you intend to use HiClass. ```powershell .\env\hiclass-environment\Scripts\activate ``` -------------------------------- ### Local Classifier Per Node with Calibration Source: https://hiclass.readthedocs.io/en/latest/algorithms/calibration.html Demonstrates training and calibrating a LocalClassifierPerNode using RandomForestClassifiers and Isotonic Regression. This snippet shows how to set up the classifier with a specific calibration method and probability combiner, then train, calibrate, and predict probabilities. ```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) ``` -------------------------------- ### get_metadata_routing Source: https://hiclass.readthedocs.io/en/latest/api/classifiers.html Get metadata routing of this object. This method is used to manage how metadata is passed to the estimator's methods. ```APIDOC ## get_metadata_routing() ### Description Get metadata routing of this object. ### Returns * **routing** - A `MetadataRequest` encapsulating routing information. ### Return type MetadataRequest ``` -------------------------------- ### Create Virtual Environment (Windows) Source: https://hiclass.readthedocs.io/en/latest/get_started/venv.html Creates a new virtual environment named 'hiclass-environment' within an 'env' subdirectory. This command is for Windows systems. ```powershell python -m venv env\hiclass-environment ``` -------------------------------- ### ExclusiveSiblingsPolicy Source: https://hiclass.readthedocs.io/en/latest/api/utilities.html Implements the exclusive siblings policy, a variation of the siblings policy. It focuses on examples related to a node and its direct siblings. ```APIDOC ## ExclusiveSiblingsPolicy ### Description Implement the exclusive siblings policy of the referenced paper. This policy considers examples related to a node and its direct siblings. ### Class Signature `BinaryPolicy.ExclusiveSiblingsPolicy(_digraph: DiGraph, _X: ndarray, _y: ndarray, _sample_weight=None)` ### Methods #### `__init__(_digraph: DiGraph, _X: ndarray, _y: ndarray, _sample_weight=None)` 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. #### `get_binary_examples(_node_) → tuple` 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_) → ndarray` 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. #### `positive_examples(_node_) → ndarray` 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. ``` -------------------------------- ### InclusivePolicy Source: https://hiclass.readthedocs.io/en/latest/api/utilities.html Implements the inclusive policy for binary classification. It allows gathering positive and negative examples based on a node and its descendants/successors. ```APIDOC ## InclusivePolicy ### Description Implements the inclusive policy of the referenced paper. This class is used for inferring node relationships and gathering examples based on a node and its descendants/successors. ### Class Signature `class BinaryPolicy.InclusivePolicy(_digraph : DiGraph_, _X : ndarray_, _y : ndarray_, _sample_weight =None_)` ### Methods #### `__init__(_digraph : DiGraph_, _X : ndarray_, _y : ndarray_, _sample_weight =None_)` 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. #### `get_binary_examples(_node_) → tuple` 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_) → ndarray` 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 #### `positive_examples(_node_) → ndarray` 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 ``` -------------------------------- ### Parallel Training with HiClass Source: https://hiclass.readthedocs.io/en/latest/_sources/auto_examples/plot_parallel_training.rst.txt This snippet demonstrates setting up and training a hierarchical classifier using all available CPU cores by setting `n_jobs` to -1. It requires the `hiclass` library and optionally `ray` for distributed training. ```python from hiclass import Constant, LogisticRegression from hiclass.datasets import make_dataset from hiclass.classification import HierarchicalTreeClassifier # Create a mock dataset X, y = make_dataset( n_samples=28000, n_features=10, n_classes=5, n_clusters=3, n_levels=3, random_state=42, n_jobs=-1 ) # Initialize a hierarchical classifier with parallel training hc = HierarchicalTreeClassifier( local_classifier=LogisticRegression(), n_jobs=-1, random_state=42 ) # Train the classifier hc.fit(X, y) ``` -------------------------------- ### Siblings Binary Policy Source: https://hiclass.readthedocs.io/en/latest/auto_examples/plot_binary_policies.html Sets the binary policy to 'siblings'. This policy considers sibling nodes when selecting training examples. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf, binary_policy="siblings") ``` -------------------------------- ### SiblingsPolicy Source: https://hiclass.readthedocs.io/en/latest/api/utilities.html Implements the siblings policy for inferring node relationships. It provides methods to retrieve positive and negative examples for a given node. ```APIDOC ## SiblingsPolicy ### Description Implements the siblings policy of the referenced paper. This class is used for inferring node relationships and retrieving examples based on these relationships. ### Class Signature `BinaryPolicy.SiblingsPolicy(_digraph: DiGraph, _X: ndarray, _y: ndarray, _sample_weight=None)` ### Methods #### `__init__(_digraph: DiGraph, _X: ndarray, _y: ndarray, _sample_weight=None)` 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. #### `get_binary_examples(_node_) → tuple` 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_) → ndarray` 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. #### `positive_examples(_node_) → ndarray` 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. ``` -------------------------------- ### Parallel Training with Joblib Source: https://hiclass.readthedocs.io/en/latest/_sources/auto_examples/plot_parallel_training.rst.txt Illustrates using `joblib.Parallel` and `joblib.delayed` for custom parallel execution, which is useful for more complex workflows or when estimators don't directly support `n_jobs`. ```python import numpy as np from sklearn.svm import SVC from sklearn.model_selection import train_test_split from sklearn.datasets import load_digits from joblib import Parallel, delayed # Load a sample dataset X, y = load_digits(return_X_y=True) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # Define a function to train an SVC model def train_svc(X_train, y_train, C_value): model = SVC(C=C_value, random_state=42) model.fit(X_train, y_train) return model # Define the range of C values to test C_values = [0.1, 1, 10] # Use joblib.Parallel to train multiple SVC models in parallel # n_jobs=-1 uses all available CPU cores results = Parallel(n_jobs=-1)(delayed(train_svc)(X_train, y_train, C) for C in C_values) # 'results' now contains a list of trained SVC models print(f"Trained {len(results)} models in parallel.") # Example: Evaluate the first trained model first_model = results[0] y_pred = first_model.predict(X_test) from sklearn.metrics import accuracy_score accuracy = accuracy_score(y_test, y_pred) print(f"Accuracy of the first model (C={C_values[0]}): {accuracy:.2f}") ``` -------------------------------- ### LessInclusivePolicy Source: https://hiclass.readthedocs.io/en/latest/api/utilities.html Implements the less inclusive policy for binary classification. It allows gathering positive and negative examples based on a node and its children. ```APIDOC ## LessInclusivePolicy ### Description Implement the less inclusive policy of the referenced paper. This class is used for inferring node relationships and gathering examples based on a node and its children. ### Class Signature `class BinaryPolicy.LessInclusivePolicy(_digraph : DiGraph_, _X : ndarray_, _y : ndarray_, _sample_weight =None_)` ### Methods #### `__init__(_digraph : DiGraph_, _X : ndarray_, _y : ndarray_, _sample_weight =None_)` 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. #### `get_binary_examples(_node_) → tuple` 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_) → ndarray` 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 #### `positive_examples(_node_) → ndarray` 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 ``` -------------------------------- ### Exclusive Siblings Binary Policy Source: https://hiclass.readthedocs.io/en/latest/auto_examples/plot_binary_policies.html Sets the binary policy to 'exclusive_siblings'. This policy strictly excludes training examples from sibling nodes. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf, binary_policy="exclusive_siblings") ``` -------------------------------- ### Less Exclusive Binary Policy Source: https://hiclass.readthedocs.io/en/latest/auto_examples/plot_binary_policies.html Sets the binary policy to 'less_exclusive'. This policy offers a less strict exclusion of training examples. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf, binary_policy="less_exclusive") ``` -------------------------------- ### Initialize LocalClassifierPerNode Source: https://hiclass.readthedocs.io/en/latest/api/classifiers.html Instantiate a LocalClassifierPerNode object. This classifier fits one binary classifier for each node in the hierarchy, except for the root. It accepts numerous parameters to customize the training process, including the choice of local classifier, binary classification policy, and probability handling. ```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) ``` -------------------------------- ### Exclusive Siblings Policy Example Source: https://hiclass.readthedocs.io/en/latest/_sources/auto_examples/plot_binary_policies.rst.txt Implement the 'exclusive_siblings' binary policy for training binary classifiers. This policy is a more restrictive version of the 'siblings' policy. ```python rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf, binary_policy="exclusive_siblings") ``` -------------------------------- ### LessExclusivePolicy Source: https://hiclass.readthedocs.io/en/latest/api/utilities.html Implements the less exclusive policy, building upon ExclusivePolicy. It defines how to gather positive and negative examples for a given node, considering node relationships. ```APIDOC ## Class: LessExclusivePolicy ### Description Implement the less exclusive policy of the referenced paper. This policy is a subclass of `ExclusivePolicy` and modifies the behavior for gathering negative examples. ### Methods #### `__init__(_digraph: DiGraph, _X: ndarray, _y: ndarray, _sample_weight=None)` 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. #### `get_binary_examples(_node_) → tuple` 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_) → ndarray` 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. #### `positive_examples(_node_) → ndarray` 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** – A mask for which examples are included (True) and which are not. ``` -------------------------------- ### Binary Policies Source: https://hiclass.readthedocs.io/en/latest/_sources/api/utilities.rst.txt Provides documentation for various binary policy classes used in data classification. ```APIDOC ## Binary Policies This section details the available binary policy classes. ### ExclusivePolicy **Description:** Represents an exclusive policy for binary classification. ### LessExclusivePolicy **Description:** Represents a less exclusive policy for binary classification. ### InclusivePolicy **Description:** Represents an inclusive policy for binary classification. ### LessInclusivePolicy **Description:** Represents a less inclusive policy for binary classification. ### SiblingsPolicy **Description:** Represents a policy considering sibling relationships in classification. ### ExclusiveSiblingsPolicy **Description:** Represents an exclusive policy considering sibling relationships. ``` -------------------------------- ### Train and Predict with Inclusive Binary Policy Source: https://hiclass.readthedocs.io/en/latest/_downloads/f879c30c3cb546984e18821ff19c74e0/plot_binary_policies.ipynb This code snippet demonstrates training a `LocalClassifierPerNode` using a `RandomForestClassifier` and the 'inclusive' binary policy, followed by making predictions. ```python from sklearn.ensemble import RandomForestClassifier from hiclass import LocalClassifierPerNode # 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 # And inclusive policy to select training examples for binary classifiers. rf = RandomForestClassifier() classifier = LocalClassifierPerNode(local_classifier=rf, binary_policy="inclusive") # Train local classifier per node classifier.fit(X_train, Y_train) # Predict predictions = classifier.predict(X_test) print(predictions) ```