openavmkit.shap_analysis
SHAP-value computation and reporting for tree-based models.
Computes SHAP (Shapley) values across all subsets (test, train, sales,
universe) for XGBoost, LightGBM, and CatBoost models. Used by
:mod:openavmkit.modeling to produce the per-feature params and
contributions outputs that mirror what linear models produce as
coefficients and coefficient×value contributions.
See Also
openavmkit.modeling : Calls into this module to build params/contribs files for tree-based models.
explanation_from_contributions
explanation_from_contributions(df_contrib, df_features, key_col='key_sale')
Rebuild a plottable shap.Explanation from a contributions_* table.
This is the inverse of :func:make_shap_table. It is used for models whose
SHAP values exist only as on-disk contributions (e.g. the ensemble, whose
contributions are assembled as a weighted combination of its members rather
than computed from a live estimator), so there is no explainer to re-run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df_contrib
|
DataFrame
|
A contributions table: a |
required |
df_features
|
DataFrame
|
Raw feature values for the same rows, used as the beeswarm |
required |
key_col
|
str
|
The column shared by both frames used to align rows. Defaults to
|
'key_sale'
|
Returns:
| Type | Description |
|---|---|
Explanation
|
With |
Source code in openavmkit/shap_analysis.py
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 | |
get_full_layeredcomp_shaps
get_full_layeredcomp_shaps(model, X_train, X_test, X_sales, X_univ, verbose=False)
Compute exact SHAP Explanations for all subsets of a LayeredComp model.
Mirrors :func:get_full_model_shaps / :func:get_full_ngboost_shaps for the
LayeredComp engine's folded-tree decomposition.
Source code in openavmkit/shap_analysis.py
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 | |
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 "universe" |
Source code in openavmkit/shap_analysis.py
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 | |
get_full_ngboost_shaps
get_full_ngboost_shaps(model, X_train, X_test, X_sales, X_univ, param_index=0, verbose=False)
Compute exact SHAP Explanations for all subsets for one NGBoost distribution parameter.
Mirrors :func:get_full_model_shaps but for NGBoost's additive tree
decomposition. param_index selects loc (0, the mean) or logscale
(1, log predictive std).
Source code in openavmkit/shap_analysis.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
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
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 | |
ngboost_internals_ok
ngboost_internals_ok(regressor)
Return True if an NGBRegressor exposes the additive-ensemble internals SHAP needs.
NGBoost's exact tree-SHAP decomposition relies on per-stage base_models,
scalings, col_idxs, learning_rate and init_params. This guards
against future NGBoost layout changes — callers should skip SHAP (rather than
crash) when it returns False.
Source code in openavmkit/shap_analysis.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
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
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 | |