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
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 | def get_lr_metrics(
lr_model: LogisticRegression, X_train_binned, X_test_binned, y_train, y_test, N_feat, trial=None
):
"""
> This function takes a logistic regression model, the training and test data, and the number of
features, and returns a dictionary of metrics for the model
:param lr_model: LogisticRegression
:type lr_model: LogisticRegression
:param X_train_binned: the training data, binned
:param X_test_binned: the test set, binned
:param y_train: the training labels
:param y_test: the test set labels
:param N_feat: number of features to use in the model
:param trial: if you're running this in a hyperparameter tuning context, this is the trial object
:return: A dictionary of metrics for the training and test sets.
"""
N_feat = N_feat + 1
# shap
explainer = shap.Explainer(lr_model, X_train_binned)
expected_value = explainer.expected_value
explainer(X_test_binned)
with tempfile.TemporaryDirectory() as dp:
# histogram of proba from base EN model
y_pred = lr_model.predict_proba(X_test_binned)[:, 1]
plt.hist(y_pred)
plt.xlabel("Probability of escalation")
plt.savefig(Path(dp, "logistic_escalation_probability.png"))
plt.close()
# decision path for >= 10% probability
T1 = X_test_binned[y_pred >= 0.1]
sh1 = explainer.shap_values(T1) # [1]
shap.decision_plot(
expected_value,
sh1,
T1,
# feature_order="hclust",
feature_display_range=slice(None, -len(config.NUMERIC_VARIABLES) + 1, -1),
link="logit",
show=False,
)
plt.savefig(Path(dp, "decision_path_gt10pct_all.png"), bbox_inches="tight")
plt.close()
# decision path for >= 10% probability -- just top 10
# TODO: play with Top X #
shap.decision_plot(
expected_value,
sh1,
T1,
# TODO: make sure top 1 isn't left out - python "clopen" ranges
feature_display_range=slice(None, -N_feat, -1), # selects top 10
link="logit",
show=False,
)
plt.savefig(Path(dp, "decision_path_gt10percent_topN.png"), bbox_inches="tight")
plt.close()
# decision path for >= 20% probability
# TODO: play with Top X #
T2 = X_test_binned[y_pred >= 0.2]
sh2 = explainer.shap_values(T2) # [1]
shap.decision_plot(
expected_value,
sh2,
T2,
# feature_order="hclust",
# TODO: make sure top 1 isn't left out - python "clopen" ranges
feature_display_range=slice(None, -len(config.NUMERIC_VARIABLES) + 1, -1),
link="logit",
show=False,
)
plt.savefig(Path(dp, "decision_path_gt20percent_all.png"), bbox_inches="tight")
plt.close()
# decision path for >= 20% probability -- just top 10
shap.decision_plot(
expected_value,
sh2,
T2,
feature_display_range=slice(None, -N_feat, -1),
link="logit",
show=False,
)
plt.savefig(Path(dp, "decision_path_gt20percent_topN.png"), bbox_inches="tight")
plt.close()
if not trial:
mlflow.log_artifacts(dp)
mlflow.sklearn.log_model(lr_model, "lr model")
metrics_train = mlflow.sklearn.eval_and_log_metrics(
lr_model, X_train_binned, y_train, prefix="lr_train_"
)
metrics_test = mlflow.sklearn.eval_and_log_metrics(
lr_model, X_test_binned, y_test, prefix="lr_test_"
)
mlflow.log_params(lr_model.get_params())
mlflow.sklearn.log_model(lr_model, "LR Model")
else:
metrics_train = classification_report(
y_train, lr_model.predict(X_train_binned), output_dict=True
)
metrics_test = classification_report(
y_test, lr_model.predict(X_test_binned), output_dict=True
)
metrics = {"lr_train": metrics_train, "lr_test": metrics_test}
return metrics
|