openavmkit.shap_analysis
get_full_model_shaps
get_full_model_shaps(model, X_train, X_test, X_sales, X_univ, verbose=False)
Calculates shaps for all subsets (test, train, sales, universe) of one model run
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
XGBoostModel | LightGBMModel | CatBoostModel
|
A trained prediction model |
required |
X_train
|
DataFrame
|
2D array of independent variables' values from the training set |
required |
X_test
|
DataFrame
|
2D array of independent variables' values from the testing set |
required |
X_sales
|
DataFrame
|
2D array of independent variables' values from the sales set |
required |
X_univ
|
DataFrame
|
2D array of independent variables' values from the universe set |
required |
verbose
|
bool
|
Whether to print verbose information. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
dict
|
A dict containing shap.Explanation objects keyed to "train", "test", "sales", and "univ" |
Source code in openavmkit/shap_analysis.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
make_shap_table
make_shap_table(expl, list_keys, list_vars, list_keys_sale=None, include_pred=True)
Convert a shap explanation into a dataframe breaking down the full contribution to value
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expl
|
Explanation
|
Output of your _xgboost_shap (values: (n,m), base_values: scalar or (n,)). |
required |
list_keys
|
list[str]
|
Primary keys in the same row order as X_to_explain |
required |
list_vars
|
list[str]
|
Feature names in the order used for training (your canonical order). |
required |
list_keys_sale
|
list[str] | None
|
Optional. Transaction keys in the same row order as X_to_explain. Default is None. |
None
|
include_pred
|
bool
|
Optional. Add a column that reconstructs the model output on the explained scale: base_value + sum(shap_values across features). Default is True. |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
Source code in openavmkit/shap_analysis.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
plot_full_beeswarm
plot_full_beeswarm(explanation, title='SHAP Beeswarm', save_path=None, save_kwargs=None, wrap_width=20)
Plot a full SHAP beeswarm for a tree-based model with wrapped feature names.
This function wraps long feature names, auto-scales figure size to the number of features, and renders a beeswarm plot with rotated, smaller y-axis labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
explanation
|
Explanation
|
SHAP Explanation object with |
required |
title
|
str
|
Title of the plot. Defaults to "SHAP Beeswarm". |
'SHAP Beeswarm'
|
wrap_width
|
int
|
Maximum character width for feature name wrapping. Defaults to 20. |
20
|
save_path
|
str
|
If provided, save the figure to this path (format inferred from extension). e.g., 'beeswarm.png', 'beeswarm.pdf', 'figs/beeswarm.svg'. |
None
|
save_kwargs
|
dict
|
Extra kwargs passed to |
None
|
Source code in openavmkit/shap_analysis.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |