Skip to content

Commit 6115ca7

Browse files
authored
initial readme
1 parent b771d00 commit 6115ca7

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,51 @@ Individual feature importances from random forests.
55
[![Build Status](https://travis-ci.org/dssg/lorax.svg?branch=master)](https://travis-ci.org/dssg/lorax)
66
[![codecov](https://codecov.io/gh/dssg/lorax/branch/master/graph/badge.svg)](https://codecov.io/gh/dssg/lorax)
77
[![codeclimate](https://codeclimate.com/github/dssg/lorax.png)](https://codeclimate.com/github/dssg/lorax)
8+
9+
Understanding the top features contributing to an individual example's model score can be important both for people building models to understand, debug, and improve their predictions and for people applying those models to trust and take appropriate actions based on their outputs. Overall, model-level feature importances can give a sense of what the modeling algorithm is surfacing as important in general but may have only mimimal relevance to what actually drives the score of any given individual. The Lorax helps to solve this problem by measuring feature contributions on the level of individual scores, currently developed and tested for the context of binary classification problems using Random Forest classifiers.
10+
11+
## Usage
12+
13+
### Basic Usage
14+
15+
`TheLorax` can be initialized with a trained random forest classifier (specifically, of class `sklearn.ensemble.RandomForestClassifier` and test set matrix (a pandas `DataFrame`), for instance:
16+
```
17+
lrx = TheLorax(rf, test_mat)
18+
```
19+
20+
The resulting object can be used calculate feature contributions to an individual example's score using:
21+
```
22+
lrx.explain_example(example_id, pred_class=1, num_features=10, graph=True)
23+
```
24+
25+
Using `graph=False` will simply return a dataframe with the feature contributions and distributions (for any feature with non-zero contribution to the score). For non-graphical output, `num_features` is ignored and the resulting dataframe contains all feature contributions.
26+
27+
When graphing, the output will contain two components:
28+
1. A bar graph of the top `num_features` contributions to the example's score
29+
1. For each of these features, a graph showing the percentile for the feature's mean across the entire test set (gray dot), the percentile of the feature value for the example being explained (orange dot) and the z-score for that value
30+
31+
### Regex Pattern Usage
32+
33+
You may want to look at the contributions of groups of features, which can be accomplished by providing regular expression patterns to combine feature names into sets:
34+
```
35+
lrx = TheLorax(rf, test_mat, name_patterns=list_of_regex)
36+
```
37+
38+
Where `list_of_regex` could either by string regex patterns or compiled regex objects (each feature name must match one and only one pattern in the list). These patterns can also be called after creating a Lorax object with `lrx.set_name_patterns(list_of_regex)`.
39+
40+
Then the explanations can make use of these patterns by calling:
41+
```
42+
lrx.explain_example(example_id, pred_class=1, num_features=10, graph=True, how='patterns')
43+
```
44+
45+
Explaining examples using name patterns will provide contributions grouped to the pattern level but note that feature distribution information isn't provided when aggregating to pattern levels.
46+
47+
## Feature Contribution Calculations
48+
49+
The basic method used here for calculating feature contributions is:
50+
51+
1. Traverse the trees with an individual example’s feature values (just as with making a prediction)
52+
1. At every split, that example’s score changes based on the value it has for the feature split on
53+
1. Keep track of these changes, associating them with the splitting feature
54+
1. Sum the changes associated with each feature and divide by the number of trees, giving the contribution of that feature
55+
1. The sum of these contributions will (roughly) be the difference between the example’s score and overall mean (roughly because of bootstrapping, assuming no balancing done in the bootstrap)

0 commit comments

Comments
 (0)