Imagine that I have a weight-of-evidence scorecard that has the following properties. I have an intercept value of -3.15 and three coefficients: -1.01, -1.00, -0.93. I would like to convert my model predictions in logit form to scorecard points (ideally maximum of 100 points).
Unlike typical scorecards where more points is better, I want to do the opposite. In case a customer received more points, I would be able to say that 30 points because feature A is bad, another 40 points is because feature B is bad, and remaining 30 are due to feature 3. This would be similar to Shapley values where total prediction is decomposed into elements.
How can I achieve this preferably in Python or even SQL?
I use formula ratio goods to ratio bads in my WOE formula such that a WOE of -3 would correspond to higher risk than on average.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To convert your model predictions in logit form to scorecard points with a maximum of 100 points, where higher points indicate higher risk, you can follow these steps:
First, you need to convert the logit predictions to probability scores. You can use the expit function from the scipy.special module to do this. Here's a Python function to convert logit to probability:
python
from scipy.special import expit
def logit_to_prob(logit):
return expit(logit)
Next, you need to create a function to convert the probability scores to scorecard points. Since you want higher points to indicate higher risk, you can use a linear transformation that maps the probability range [0, 1] to the scorecard points range [0, 100]. Here's a Python function to do this:
To decompose the total prediction into individual feature contributions, you can use SHAP (SHapley Additive exPlanations) values. Here's an example using the shap library:
First, install the shap library if you haven't already:
bash
pip install shap
Then, you can use the following code to compute SHAP values for your model:
python
import shap
# Assuming `model` is your trained model and `X` is your feature matrix
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X)
# Convert SHAP values to scorecard points
shap_points = shap_values * 100 / shap_values.sum(axis=1)[:, np.newaxis]
Finally, you can create a function to decompose the total prediction into individual feature contributions in terms of scorecard points:
# Assuming `logit_pred` is your logit prediction and `shap_points` is the SHAP values matrix
feature_points = decompose_points(logit_pred, shap_points)
print(f"Total points: {logit_to_points(logit_pred)}")
print(f"Feature A points: {feature_points[0, 0]}")
print(f"Feature B points: {feature_points[0, 1]}")
print(f"Feature C points: {feature_points[0, 2]}")
For SQL, you can implement the logit-to-probability and probability-to-points conversions using SQL functions, but computing SHAP values in SQL is more complex and might require using external tools or libraries.