WrappedClassificationModel() usage
lucazav opened this issue · comments
I'd like to have a clarification about the functioning of WrappedClassificationModel()
.
I have a prefitted binary classification model model1 and a function compute_prob
that calibrates the probabilities associated to class 1. (model1 has a predict_proba
method which isn't calibrated).
Through:
mymodel = WrappedClassificationModel(model1, compute_prob)
a new model is correctly generated (both methods predict
and predict_proba
work correctly).
It seems though that the predict
method uses the same exact method of my original model, while predict_proba
uses the wrapped function compute_prob
.
-
What I'd like to achieve is obtaining classification labels based on the probabilities computed through
compute_prob
(1 if probability > treshold, 0 otherwise) directly from thepredict
method. Is this possible? -
When using the new wrapped model with SHAP TabularExplainer to generate an explanation (which worked when using my original model), I get an error:
explainer = TabularExplainer(model1,
X_train_df,
#features=features,
classes=["Not Fraud","Fraud"])
global_explanation_test = explainer.explain_global(X_test_df)
The error I get is the following:
TypeError Traceback (most recent call last)
<ipython-input-328-0abd05dcccf3> in <module>
----> 1 global_explanation_test = explainer.explain_global(X_test_calibration)
[...]
/anaconda/envs/azureml_py36/lib/python3.6/site-packages/interpret_community/explanation/explanation.py in __init__(self, init_data, eval_data, eval_y_predicted, eval_y_predicted_proba, **kwargs)
1234 :type eval_y_predicted_proba: numpy.array or str
1235 """
-> 1236 super(_DatasetsMixin, self).__init__(**kwargs)
1237 self._init_data = init_data
1238 self._eval_data = eval_data
TypeError: object.__init__() takes no parameters
I also tried different eval_functions
to make sure the problem doesn't lie specifically in my compute_prob
function.
Is there a way to solve the issue and use the wrapped model to generate an explanation?
hi @lucazav , you can wrap your model directly, in a class similar to the WrappedClassificationModel(), and define the predict_proba and predict functions in the way you prefer. The model just needs to have these two functions defined to work with the blackbox explainers. The wrap_model function tries to intelligently figure this out (and may use WrappedClassificationModel or others internally), but it can't handle every possible scenario - just the most common ones from the most common frameworks used, so that the input and output follow the scikit-learn standard.
@lucazav I looked at the error above but I'm not sure why it is happening. Would you be able to send me a sample notebook with a dummy dataset that reproduces the error, and I can take a look at it and try to fix the issue?
Hi @imatiach-msft,
I used the wrapper example you suggested to me and now I can render the explainability dashboard without errors. Thanks.