A Python package for hierarchical decision tree and random forest classifiers
Project description
HierarTree
HierarTree is a Python package for building and utilizing hierarchical decision tree classifiers. It is designed to handle datasets with hierarchical labels, providing robust classification and interpretability.
Features
- Train hierarchical decision trees tailored for data with nested labels.
- Support for both decision tree and random forest models.
- Implements advanced scoring metrics for hierarchical data, including hierarchical accuracy and Brier scores.
- Visualize decision trees with detailed information on splits and classification metrics.
- Flexible configuration with options for maximum depth, minimum samples per leaf, and more.
Installation
You can install HierarTree by cloning this repository and running:
pip install .
Requirements
- Python >= 3.6
- numpy
- pandas
- matplotlib
- joblib
- sklearn
Usage
Basic Example
from HierarTree import HierarchicalLabelDecisionTreeClassifier
# Example hierarchical label structure
hierarchy = {
"Animal": {
"Mammal": ["Dog", "Cat"],
"Bird": ["Sparrow", "Pigeon"]
}
}
# Initialize the classifier
classifier = HierarchicalLabelDecisionTreeClassifier(hierarchy=hierarchy)
# Training data
data = [
{"feature1": 1.2, "feature2": 3.4, "label": ["Animal", "Mammal", "Dog"]},
{"feature1": 2.3, "feature2": 1.4, "label": ["Animal", "Bird", "Sparrow"]},
]
# Fit the model
classifier.fit(data, response="label")
# Predictions
samples = [
{"feature1": 1.5, "feature2": 3.2},
{"feature1": 2.0, "feature2": 1.2},
]
predictions = classifier.predict(samples)
print(predictions)
API Reference
HierarchicalLabelDecisionTreeClassifier
Initialization
HierarchicalLabelDecisionTreeClassifier(
hierarchy,
max_features=None,
random_state=None,
min_samples_leaf=1,
min_samples_split=2,
max_class_proportion=1,
max_leaf_nodes=None,
max_depth=None,
no_values_iter=None,
secondary_score='accuracy'
)
hierarchy: Dictionary defining the hierarchical label structure.max_features: Number of features to consider for splits.min_samples_leaf: Minimum samples required to form a leaf node.max_depth: Maximum depth of the tree.
Methods
fit(data, response): Train the model.predict(samples): Predict labels for given samples.predict_terminal_probabilities(samples): Get probability distributions at terminal nodes.plot_tree(node): Visualize the decision tree.
HierarchicalLabelRandomForestClassifier
Provides ensemble learning with multiple hierarchical decision trees.
Initialization
HierarchicalLabelRandomForestClassifier(
hierarchy,
n_trees=100,
max_features='sqrt',
random_state=None,
min_samples_leaf=1,
min_samples_split=2,
max_class_proportion=1,
max_leaf_nodes=None,
max_depth=None,
no_values_iter=None,
secondary_score='accuracy'
)
n_trees: Number of trees in the forest.
Contributing
Contributions are welcome! Please fork the repository and submit a pull request with your changes.
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file HierarTree-0.1.tar.gz.
File metadata
- Download URL: HierarTree-0.1.tar.gz
- Upload date:
- Size: 12.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.8.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b225226197921893bc3008623631e8a96ad2fa85f7ee7bf272e584442cc7d0a8
|
|
| MD5 |
db508f5555f2620b3b4a3c7daeac70d7
|
|
| BLAKE2b-256 |
7504f180219ab345c79b6a3a2d945764b18cc45718c37c15769e7d98f24824bc
|
File details
Details for the file HierarTree-0.1-py3-none-any.whl.
File metadata
- Download URL: HierarTree-0.1-py3-none-any.whl
- Upload date:
- Size: 12.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.8.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eca3d76b7b9da2f5e6978f2fbfe5569217d48631645dee1cd1af7238a98e52bd
|
|
| MD5 |
86bd3dda8d38ba956e23084159a72d74
|
|
| BLAKE2b-256 |
7500df8002631d410e81af51a3432dbcda449c6c92662e70f788aceabd497b16
|