# Explainability

Hello again! This is the second part of a tutorial on predicting the quality of red wine. If you haven't completed the first part yet, we highly recommend you do that first. If you have, great! Let's continue.

In this tutorial we will learn how to use the `EXPLAIN`

function to understand our predictive model and get insights on how important each feature is to the quality of wine.

## What is Explainability?

Explainable AI (XAI) is a set of methods that allows us to peer 👀 into the inner workings of complicated machine-learning algorithms, to more easily interpret and understand the relationships that they have learned in order to make accurate predictions.

This allows us to say which features are most important to the outcome, e.g. alcohol % 🍸 is the most important feature in deciding the quality of the wine.

At Infer, our Explainable AI uses Shapley Additive Explanations (SHAP). In essence, this method uses Game Theory to explain how features 'cooperate' to reach their best possible guess of the outcome.

## Want to know more about SHAP?

If you'd like to learn more about the SHAP algorithm, we highly recommend these snazzy explanations:

https://christophm.github.io/interpretable-ml-book/shapley.html

https://christophm.github.io/interpretable-ml-book/shap.html

`EXPLAIN`

Explanations

`EXPLAIN`

returns the feature importance of each of your features.
Feature importance is a ** global** explanation, where the explanation represents the impact over all data points that a feature has on the output.

Global explanations are a nice way to quickly summarise the importance of your features. Our global importances are derived from aggregated SHAP explanations. Global explanations sum to one, so that it is easy to see the relative importance between features.

## Using SQL-inf for Explanations

SQL-inf makes using XAI extremely simple. We access XAI outputs by chaining the `PREDICT`

command with `EXPLAIN`

, like so:

`SELECT * FROM vino_veritas EXPLAIN(PREDICT(quality, model='reg'))`

and we get our first XAI results - another table and a graph! Nice.

### The output of EXPLAIN

`EXPLAIN`

has slightly different outputs depending on the exact `PREDICT`

function used.
For regression (`model='reg'`

), the output is always a single value, and so the `EXPLAIN`

will return one (1) line of feature importances.
The feature importances (the relative importance of each feature on the predicted outcome) are ordered and visualised on a bar chart as shown below:

For classification (`model='clf'`

), the output will have *N* classes, and so we have *N* lines of feature importance.
Each line will describe each class. In the special case where the output is binary (e.g. Yes/No), the feature importances of the two classes will be the same.
This is because what is important for one binary class, will be important in the opposite direction for the other binary class. For example,
if we are trying to predict if someone will eat ice cream today, and more people eat ice cream when it is sunny, then less people will eat ice cream when it is not sunny. The sun is equally important for both cases, but in the opposite way.

Because of these multiple classes, we end up with multiple bars on a barchart, one for each class. We can then decipher which feature is important for which class. In the plot below, we can see how sulphates, alcohol, and volatile acidity are important in different ways for each class: higher scores tend to depend more on sulphates and alcohol content, while lower scores depend more on other features.

### Metrics

`EXPLAIN`

also returns a number of `inf`

columns prefixed with `metrics_`

. They explain how well the model is actually performing the predictions on a held-out testing set of data, not used for training the model.
These metrics depend on the type of model used.

The better these metrics are, the more faith you can have in the feature importances found via `EXPLAIN`

.

For `reg`

:

`metrics_r2`

: The R-squared value, known as the coefficient of determination, a score between 0 and 1. This is a measure of how well the model actually predicts the variation in the outcome. A score of 1 is a perfect predictor, 0 is the same as just predicting a constant value (not good). A score of 0.3 indicates that 30% of the variation in the outcome has been explained by the model.`metrics_mae`

: MAE is the mean absolute error, i.e.`average(true value - predicted value)`

. Basically how far off is the prediction, on average. Smaller is better.`metrics_mse`

: MSE is the mean squared error, i.e.`average( (true value - predicted value)^2 )`

. Similarly, smaller is better.

For `clf`

:

`metrics_class`

: Name of the class.`metrics_precision`

: The precision of the class, i.e. the ratio of the number of true positives to the total number of positive predictions (false positives + true positives). The precision is intuitively the ability of the classifier not to label as positive a sample that is negative.`metrics_recall`

: The recall of the class, i.e. the ratio of the number of true positives to the total number of true positives and false negatives. The recall is intuitively the ability of the classifier to find all the positive samples.`metrics_f1-score`

: The F1-score combines the precision and recall of a classifier into a single metric by taking their harmonic mean. This makes it easy to compare the performance of two classes.`metrics_support`

: The support is the number of validation points that were used to calculate the metrics (15% of the data) for each class.`metrics_accuracy`

: The accuracy of the model for each class.

## Wrapping Up: Insights

Let's put it all together to summarise our insights in this project.

- We used
`EXPLAIN`

and`model='reg'`

to find that there are three main features that predict ~40% of the variation (`metrics_R2 = 0.39`

) of the quality of wine: 1) alcohol, 2) sulphates, and 3) volatile acidity. 📊 - We used
`EXPLAIN`

and`model=clf`

to look at the effects of the features in more granular detail, and found each quality score is affected slightly different by the wine's features, but in general the previous trends still stand: alcohol, sulphates, and volatile acidity are all king for predicting wine quality. 👑 - We used
`PREDICT`

and its visualisations to see how the relationship between the alcohol % affects the predicted wine quality - generally, larger amounts of alcohol are associated with good wines. 🍷

This concludes our prediction and explainability tutorial. We hope it was insightful! Now go and use them on your own data. 😄