|
102 | 102 | by="importance", ascending=False |
103 | 103 | ) |
104 | 104 |
|
105 | | -# Shapes for all term affiliations in the model. For each term affiliation, contains relevant predictor values and the corresponding |
106 | | -# contributions to the linear predictor. |
107 | | -# This is probably the most useful method to use for understanding how the model works. |
108 | | -# Plots are created for main effects and two-way interactions. |
| 105 | +# Shapes for all term affiliations in the model. For each term affiliation, contains predictor values and the corresponding |
| 106 | +# contributions to the linear predictor. Plots are created for main effects and two-way interactions. |
| 107 | +# This is probably the most useful method to use for understanding how the model works but it is currently very memory intensive when |
| 108 | +# handling interactions and may crash without warning on larger models. Consider using either of the calculate_local_feature_contribution |
| 109 | +# or calculate_local_contribution_from_selected_terms methods to interpret interactions on larger models. |
109 | 110 | shapes: Dict[str, pd.DataFrame] = {} |
110 | 111 | predictors_in_each_affiliation = ( |
111 | 112 | best_model.get_base_predictors_in_each_unique_term_affiliation() |
|
161 | 162 | best_model.calculate_local_feature_contribution(data_train[predictors]), |
162 | 163 | columns=best_model.get_unique_term_affiliations(), |
163 | 164 | ) |
| 165 | +# Combining predictor values with local feature contribution for the second feature in best_model.get_unique_term_affiliations(). |
| 166 | +# This can be visualized if it is a main effect or a two-way interaction. |
| 167 | +unique_term_affiliation_index = 1 |
| 168 | +predictors_in_the_second_feature = [ |
| 169 | + predictors[predictor_index] |
| 170 | + for predictor_index in best_model.get_base_predictors_in_each_unique_term_affiliation()[ |
| 171 | + unique_term_affiliation_index |
| 172 | + ] |
| 173 | +] |
| 174 | +data_to_visualize = pd.DataFrame( |
| 175 | + np.concatenate( |
| 176 | + ( |
| 177 | + data_train[predictors_in_the_second_feature].values, |
| 178 | + local_feature_contribution[ |
| 179 | + [ |
| 180 | + best_model.get_unique_term_affiliations()[ |
| 181 | + unique_term_affiliation_index |
| 182 | + ] |
| 183 | + ] |
| 184 | + ], |
| 185 | + ), |
| 186 | + axis=1, |
| 187 | + ), |
| 188 | + columns=predictors_in_the_second_feature |
| 189 | + + [ |
| 190 | + f"contribution from {best_model.get_unique_term_affiliations()[unique_term_affiliation_index]}" |
| 191 | + ], |
| 192 | +) |
164 | 193 |
|
165 | 194 | # Local (observation specific) contribution to the linear predictor from selected interacting predictors. |
166 | 195 | # In this example this concerns two-way interaction terms in the model where the fourth and the seventh predictors in X interact. |
|
0 commit comments