# Logistic Regression - SHAP

[Code Implementation Here](https://colab.research.google.com/drive/1MYFSGZuw5Q1-jPEPs29aP-9uXX3--mLb?usp=sharing)

### What is Logistic Regression?

* Logistic Regression a machine learning algorithms from the field of statistics&#x20;
* A Logistic regression model gives the probabilities of the K classes via linear functions while at the same, sum to one, and remains in \[0, 1]
*   The name Logistic Regression comes from the use of a logistic function within the algorithm



To read more about Logistic Regression this [link](https://web.stanford.edu/~hastie/ElemStatLearn/).

### **Making the Model**&#x20;

**Data set:** Sales Opportunity Size; **Target:** DEAL SIZE (Small, Medium and Large).

The data is passed through a pre-processing stage which contains handling missing values, one-hot encoding, and other steps required. Then it is trained using by calling a function from the Scikit-learn library.

**Accuracy:** 99.24%&#x20;

![ Confusion matrix](https://1181649628-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M5Vxy9PzQFOZjhyK8YP%2F-ME_wTINDy5Se61V5aVT%2F-MEaIxquk1dLdqez6CyO%2Fimage.png?alt=media\&token=c4f9a68e-7029-4e42-a21d-648c3e435f85)

### **Implementation of Interpretability**

We are using the SHAP library created by [Scott Lundberg](https://scottlundberg.com/) for generating SHAP and providing interpretation for the model trained.

**Kernel Shap** is one of the SHAP explainers that can be used to generate SHAP values. Kernel SHAP can be applied to any model for SHAP generation. Kernel SHAP uses a specially-weighted local linear regression to estimate SHAP values for any model.

![](https://1181649628-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M5Vxy9PzQFOZjhyK8YP%2F-MEfFiBUWbNGfRkYgRsj%2F-MEfSuOcKi0RMP5cTdKI%2Fimage.png?alt=media\&token=3b116cbc-dd41-49be-9a43-3cb572c4ce3c)

We have used kmeans on the entire data set before feeding it to the SHAP explainer to reduce computation time. Essentially we want to be able to get the predicted values for the data set by the model.

### **Visualization**

#### **Summary Plot**

* Can be plotted after generating SHAP values for the entire dataset
* Tells us the effect of each attribute on the predictions
* Better than regular variable importance because of the coalition effect involved in the SHAP calculation

![Summary Plot](https://1181649628-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M5Vxy9PzQFOZjhyK8YP%2F-ME_wTINDy5Se61V5aVT%2F-MEaTDbANlABIG5RmwYm%2Fimage.png?alt=media\&token=87313f58-c04c-40cd-b348-3a3d346c0192)

**Interpreting Summary Plot:** From the above summary plot, it is clear that SALES has the highest impact on the output of the prediction - Deal Size. It is the most important attribute and MSRP is the least important attribute. These help companies decide what is affecting their target and help them make strategies easily.

![plot](https://1181649628-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M5Vxy9PzQFOZjhyK8YP%2F-ME_wTINDy5Se61V5aVT%2F-MEaUdetBPhR3_mhsUph%2Fimage.png?alt=media\&token=6c3830e4-49c6-4a0c-9e1b-963f28aea0e8)

The above visualization gives more information compared a summary plot as it shows the distribution of the shap values. If SHAP values are negative then they have a negative impact on the output.

