|
| 1 | +# FIPE: Functionally Identical Pruning of Ensembles |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +This repository provides methods for Functionally-Identical Pruning of Tree Ensembles (FIPE). Given a trained scikit-learn model, FIPE provides a pruned model that is certified to be equivalent to the original model on the entire feature space. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +This project requires the gurobi solver. Free academic licenses are available. Please consult: |
| 10 | + |
| 11 | +- [Gurobi academic program and licenses](https://www.gurobi.com/academia/academic-program-and-licenses/) |
| 12 | +- [Gurobi academic license agreement](https://www.gurobi.com/downloads/end-user-license-agreement-academic/) |
| 13 | + |
| 14 | +Run the following commands from the project root to install the requirements. You may have to install python and venv before. |
| 15 | + |
| 16 | +```shell |
| 17 | + virtualenv -p python3.10 env |
| 18 | + pip install -e . |
| 19 | +``` |
| 20 | + |
| 21 | +The installation can be checked by running the test suite: |
| 22 | + |
| 23 | +```shell |
| 24 | + pip install pytest |
| 25 | + pytest |
| 26 | +``` |
| 27 | + |
| 28 | +The integration tests require a working Gurobi license. If a license is not available, the tests will pass and print a warning. |
| 29 | + |
| 30 | +### Getting started |
| 31 | + |
| 32 | +A minimal working example to prune an AdaBoost ensemble is presented below. |
| 33 | + |
| 34 | +```python |
| 35 | + from fipe import FIPE, FeatureEncoder |
| 36 | + import pandas as pd |
| 37 | + import numpy as np |
| 38 | + from sklearn.datasets import load_iris |
| 39 | + from sklearn.model_selection import train_test_split |
| 40 | + from sklearn.ensemble import AdaBoostClassifier |
| 41 | + |
| 42 | + |
| 43 | + # Load data encode features |
| 44 | + data = load_iris() |
| 45 | + X = pd.DataFrame(data.data) |
| 46 | + y = data.target |
| 47 | + |
| 48 | + encoder = FeatureEncoder(X) |
| 49 | + X = encoder.X.values |
| 50 | + |
| 51 | + # Train tree ensemble |
| 52 | + X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) |
| 53 | + base = AdaBoostClassifier(algorithm="SAMME", n_estimators=100) |
| 54 | + base.fit(X, y) |
| 55 | + |
| 56 | + # Read and normalize weights |
| 57 | + w = base.estimator_weights_ |
| 58 | + w = (w / w.max()) * 1e5 |
| 59 | + |
| 60 | + # Prune using FIPE |
| 61 | + norm = 1 |
| 62 | + print(f'Pruning model by minimizing l_{norm} norm.') |
| 63 | + pruner = FIPE(base=base, weights=w, encoder=encoder, eps=1e-6) |
| 64 | + pruner.build() |
| 65 | + pruner.set_norm(norm) |
| 66 | + pruner.add_samples(X_train) |
| 67 | + pruner.oracle.setParam('LogToConsole', 0) |
| 68 | + pruner.prune() |
| 69 | + print('\n Finished pruning.') |
| 70 | + |
| 71 | + # Read pruned model |
| 72 | + n_activated = pruner.n_activated |
| 73 | + print('The pruned ensemble has ', n_activated, ' estimators.') |
| 74 | + |
| 75 | + # Verify functionally-identical on test data |
| 76 | + y_pred = base.predict(X_test) |
| 77 | + y_pruned = pruner.predict(X_test) |
| 78 | + fidelity = np.mean(y_pred == y_pruned) |
| 79 | + print('Fidelity to initial ensemble is ', fidelity, '%.') |
| 80 | +``` |
0 commit comments