Scikit-Learn-compatible implementation of Adaptive Hierarchical Shrinkage
This directory contains an implementation of Adaptive Hierarchical Shrinkage that is compatible with Scikit-Learn. It exports 2 classes:
ShrinkageClassifierShrinkageRegressor
Installation
adhs Package
The adhs package, which contains the implementations of
Adaptive Hierarchical Shrinkage, can be installed from PyPI:
$ pip install adhs
Alternatively, the package can be installed by cloning:
$ git clone git@github.com:arnegevaert/adaptive-hierarchical-shrinkage.git
$ cd adaptive-hierarchical-shrinkage
$ pip install .
Note that this will install the version corresponding to the latest commit on the master branch, which may or may not be stable.
Experiments
To be able to run the scripts in the experiments directory, some extra
requirements are needed. These can be installed in a new conda
environment as follows:
$ git clone git@github.com:arnegevaert/adaptive-hierarchical-shrinkage.git
$ cd adaptive-hierarchical-shrinkage
$ conda create -n shrinkage python=3.10
$ conda activate shrinkage
$ pip install .[experiments]
For more info on reproducing the experiments, see experiments/README.md.
Basic API
This package exports 2 classes and 1 method:
ShrinkageClassifierShrinkageRegressorcross_val_shrinkage
ShrinkageClassifier and ShrinkageRegressor
Both classes inherit from ShrinkageEstimator, which extends sklearn.base.BaseEstimator. Adaptive hierarchical shrinkage can be summarized as follows:
\hat{f}(\mathbf{x}) = \mathbb{E}_{t_0}[y] + \sum_{l=1}^L\frac{\mathbb{E}_{t_l}[y] - \mathbb{E}_{t_{l-1}}[y]}{1 + \frac{g(t_{l-1})}{N(t_{l-1})}}
where $g(t_{l-1})$ is some function of the node $t_{l-1}$. Classical hierarchical shrinkage (Agarwal et al. 2022) corresponds to $g(t_{l-1}) = \lambda$, where $\lambda$ is a chosen constant.
__init__()parameters:base_estimator: the estimator around which we "wrap" hierarchical shrinkage. This should be a tree-based estimator:DecisionTreeClassifier,RandomForestClassifier, ... (analogous forRegressors)lmb: $\lambda$ hyperparameterrandom_state: random state for reproducibilityshrink_mode: 6 options:"no_shrinkage": dummy value. This setting will not influence thebase_estimatorin any way, and is equivalent to just using thebase_estimatorby itself. Added for easy comparison between different modes of shrinkage and no shrinkage at all."hs": classical Hierarchical Shrinkage (from Agarwal et al. 2022): $g(t_{l-1}) = \lambda$."hs_entropy": Adaptive Hierarchical Shrinkage with added entropy term: $g(t_{l-1}) = \lambda H(t_{l-1})$."hs_log_cardinality": Adaptive Hierarchical Shrinkage with log of cardinality term: $g(t_{l-1}) = \lambda \log C(t_{l-1})$ where $C(t)$ is the number of unique values in $t$."hs_global_permutation": Same as"hs_permutation", but the data is permuted only once for the full dataset rather than once in each node."hs_permutation": Adaptive Hierarchical Shrinkage with:
\begin{aligned}
g(t_{l-1}) &= \frac{1}{\alpha(t_{l-1})}\\
\alpha(t_{l-1}) &= 1 - \frac{\Delta_\mathcal{I}(t_{l-1}, { }_\pi x(t_{l-1})) + \epsilon}{\Delta_\mathcal{I}(t_{l-1}, x(t_{l-1}))+ \epsilon}
\end{aligned}
reshrink(shrink_mode, lmb, X): changes the shrinkage mode and/or lambda value in the shrinkage process. Callingreshrinkwith a given value ofshrink_modeand/orlmbon an existing model is equivalent to fitting a new model with the same base estimator but the new, given values forshrink_modeand/orlmb. This method can avoid redundant computations in the shrinkage process, so can be more efficient than re-fitting a newShrinkageClassifierorShrinkageRegressor.- Other functions:
fit(X, y),predict(X),predict_proba(X),score(X, y)work just like with any othersklearnestimator.
cross_val_shrinkage
This method can be used to efficiently run cross-validation for the shrink_mode and/or lmb hyperparameters. As adaptive hierarchical shrinkage is a fully post-hoc procedure, cross-validation requires no retraining of the base model. This function exploits this property.
Tutorials
- General usage: Shows how to apply hierarchical shrinkage on a simple dataset and access feature importances.
- Cross-validating shrinkage parameters:
Hyperparameters for (augmented) hierarchical shrinkage (i.e.
shrink_modeandlmb) can be tuned using cross-validation, without having to retrain the underlying model. This is because (augmented) hierarchical shrinkage is a fully post-hoc procedure. As theShrinkageClassifierandShrinkageRegressorare valid scikit-learn estimators, you could simply tune these hyperparameters usingGridSearchCVas you would do with any other scikit-learn model. However, this will retrain the decision tree or random forest, which leads to unnecessary performance loss. This notebook shows how you can use our cross-validation function to cross-validateshrink_modeandlmbwithout this performance loss.
Experiments
To reproduce the experiments from the paper, see experiments/README.md.
Metadata
Release files for adhs 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| adhs-0.1.4.tar.gz | 12.8 kB | Without provenance | Details for adhs-0.1.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| adhs-0.1.4-py3-none-any.whl | Python 3 | none | any | Details for adhs-0.1.4-py3-none-any.whl |
Total release size: 24.6 kB
Release files / adhs-0.1.4.tar.gz
| Download URL | adhs-0.1.4.tar.gz |
|---|---|
| Size | 12.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a59115945d55723966231a8090dfc00c8d511ad722a438cfa26568816eea5ea5
|
|
BLAKE2b-256 checksum How to use checksums |
47934827eff034251ecb360d48ef7d17b6baa98b092874257e9732d30fa45f2e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"NixOS","version":"25.11","id":"xantusia","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / adhs-0.1.4-py3-none-any.whl
| Download URL | adhs-0.1.4-py3-none-any.whl |
|---|---|
| Size | 11.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
524a237f5e9b2e0d57c37a08d22455a35e3b95345a22cc6c303c903a6aacb379
|
|
BLAKE2b-256 checksum How to use checksums |
443279b3dae137ebaea18be1d714fa2fb13d2ad4f33776cca5a578a7791b2ddd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"NixOS","version":"25.11","id":"xantusia","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for adhs 0.1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.