Interpretation of Machine Learning Models

Think about the scenario, you are a machine learning engineer or data scientist and you are talking to your friends about an India vs Pakistan cricket match that is going to be held in world cup 2023. Based on various features you designed a machine learning model that is going to predict, who is going to win that match and which player will be the man of the match. If your prediction turns out to be correct, your friends will be impressed. If the prediction turns out to be incorrect it is just going to harm your reputation among your friends only. Now again think about a serious scenario, you have to build a machine learning model to predict whether some patient has cancer or not. Now in this scenario, if your model is going to give inaccurate results, it can be very dangerous. So to avoid such scenarios, the machine learning model should be able to tell what are the reasons, because of which model is giving some particular results. So that some doctor can validate, whether the model is accurate or not.  

Interpretable Machine Learning refers to methods and models that make the behavior and predictions of machine learning systems understandable to humans.

“Machine Learning models have been branded as ‘Black Boxes’ by many. This means that though we can get accurate predictions from them, we cannot clearly explain or identify the logic behind these predictions. But how do we go about extracting important insights from the models? What things are to be kept in mind and what features or tools will we need to achieve that? These are the important questions that come to mind when the issue of Model Explainability is raised.”

So in this article, we are going to talk about how we can interpret the results of various classical machine learning models such as Naive Bayes, Logistic Regression, Random Forest, etc using available trained models. Although there are some complex classical machine learning algorithms such as Support Vector Machine, etc, in which it is very difficult to find out the feature importance. 

You can download the dataset used in the post from kaggle. Data-Set is the containing reviews of various food products and its corresponding rating between 1-5. 

Steps:
  1. Import all the libraries.
  2. Read the data.
  3. Preprocess the Data-Set.
  4. Featurize the data using tf-idf.
  5. Train the model.
  6. Interpret the model or find out feature importance in the model.

import all the required libraries: First we will import all the required libraries to clean the dataset, vectorize the dataset and to make machine learning models.


Read the Data: We will read the dataset in a pandas data frame. To make the entire data preprocessing fast, we have worked with only 100k data points. You can work with the entire dataset. in the dataset, if some datapoint is having rating greater then 3, we are considering it as Positive and if less than 3, we are considering that datapoint as Negative.


Preprocess the Data-Set: In the Pre-Processing phase, we will do the following in the order given below. 
  1. Remove the HTML tags.
  2. Changin sort forms to full forms such as won’t to will not.
  3. Remove punctuations or some other special character.
  4. Remove all the Non-English words in the dataset.
  5. Remove all the words containing the alphanumeric characters.
  6. Convert all the words to lowercase
  7. Remove all the Stopwords from each data point.
  8. Finally, Snowball Stemming the word.


Divide the Data-Set into Train, Test, and Validation-Set: To find out the optimal set of hyperparameters in various machine learning models, and to test the model we have divided the entire dataset into three parts i.e., train, test, and cross-validation set.


Vectorize the Data-Set using TF-IDF: In this case study, we are using tf-idf to vectorize the data. To avoid the problem of data leaking we have first divided the dataset into train and test and then vectorizing the data.

We will show the model interpretability for the following three models.

  1. Logistic Regression
  2. Naive Bayes
  3. Random Forest
Logistic Regression: 
                              As we know Logistic Regression is a Parametric Machine Learning Algorithm. So if the features are not collinear (with the help of the Perturbation test, we can check whether the features are collinear or not. We will discuss it in some other post.), we can use weights to find out the feature importance in the model
So here we will find out the top-20 important features from both the classes, based on weights in the Logistic Regression model. Corresponding to those 10 features with the help of tf-idf value we will find out the corresponding word.

Training the Logistic Regression Model:


Find Top-20 Features from Positive Class Based on Weights and TF-IDF Values:

Weight  Top Positive Feature
1.48 delicious
1.34 perfect
1.33 amazing
1.32 excellent
1.17 not bitter
1.13 loves
1.13 best
1.12 wonderful
1.10 awesome
1.08 great
1.04 pleased
1.02 yummy
1.02 not bad
0.97 even better
0.95 glad
0.94 thank
0.93 happy
0.91 would buy
0.91 satisfied

While interprating the model outcome, If model is predicting some point to be positive, we will say that, datapoint is containing these positive words. This is the reason datapoint is positive.
Find the Top-20 Features from Negative Class Based on Weights and TF-IDF values:

Weight  Top Negative Feature
-1.74 not worth
-1.62 not recommend
-1.58 disappointed
-1.50 terrible
-1.50 disappointing
-1.40 awful
-1.34 not good
-1.26 rip
-1.22 not happy
-1.21 not buying
-1.17 never buy
-1.16 horrible
-1.14 definitely not
-1.14 disgusting
-1.14 disappointment
-1.13 unfortunately
-1.13 not great
-1.12 threw
-1.10 shame
While interprating the model outcome, If model is predicting some point to be Negative, we will say that, datapoint is containing these Negative words. This is the reason datapoint is Negative.
Naive Bayes: Most of the time, for text classification Naive Bayes is used as a baseline model. It is a powerful algorithm that is used in spam detection for a long time. Given a class, It gives the probability value to each feature or word available in the dataset. One of the biggest advantages is it is highly interpretable. It assumes that the features in the dataset are not related to each other, that is not true in real life. This is the reason it’s called the Naive algorithm.
In this section, we will try to find out the top-10 features in Amazon Fine Food Review data, based on which the Naive Bayes Algorithm is going to assign polarity to each review.
Training the Naive Bayes Model:

Find Top-10 Features from Positive and Negative Class Based on Probability and TF-IDF Values:

Top 10 Positive Features:-
best -3.717245
like -4.517867
good -4.653043
great -4.742373
one -4.882242
taste -4.959170
coffee -5.004192
would -5.064155
flavor -5.066249
love -5.068347
Name: 1, dtype: float64

Top 10 Negative Features:-
Bad -3.276828
disappointed -4.389544
terrible -4.655955
disgusting -4.691064
product -4.719342
unfortunately -4.887557
not good -5.102897
not great -5.146753
no -5.152748
flavor -5.172705
Name: 0, dtype: float64

If some of the reviews have been assigned Positive Polarity, we will say that review is containing words that are important in the positive class, that’s why the model is giving output as positive and vice-versa. In the positive feature set, we can see that most of the words are positive and in negative feature set most of the words are negative. These results may improve more if we use entire dataset for modeling.

Random Forest: Random Forest is one of the powerful ensemble models. It is a combination of many numbers of decision trees (a hyperparameter in Random Forest). Each decision tree in Random Forest is made by taking a subset of available features from the dataset. More diverse the features greater will be the model outcome in Random Forest. 

Image result for random forest
Random Forest, Combination of Decision Trees

It is less interpretable as compare to Decision Tree, Naive Bayes, Logistic Regression, etc. Here in this section, we will find out the top-20 features based on the decision tree built in the Random Forest model over Amazon Find Food Review dataset. 

Training the Random Forest Model:

Find Top-50 Features in Random Forest:

"converts, cove, dissapointed, shrimp, slaughterhouse, resealed, shriveled, conveniently, rosehips, vegetation, pcs, faery, tab, efficacious, labled, glycerin, farms, bonemeal, vowed, memories, dlavor, whatever, interested, blockhouse, dusted, bitayavon, ran, distinguish, quizzical, motel, toxicology, cosequin, crackerbread, spendy, tablets, website, waking, curios, america, stretching, ck, habenero, pricey, lay, versa, promote, grilling, relish, ferrets,"

Top Features and Corresponding tf-idf values:

[('shpping', 0.008022072803722455),
('converts', 0.0065286288965631075),
('cove', 0.005778462980163783),
('dissapointed', 0.005168943596882204),
('shrimp', 0.0045779930499818666),
('slaughterhouse', 0.0035609855991421417),
('resealed', 0.0035043498393616075),
('shriveled', 0.0031218170301554903),
('conveniently', 0.0029000940382685357),
('rosehips', 0.0026812509129975908),
('vegetation', 0.002494186471716803),
('pcs', 0.002014141323809826),
('faery', 0.0019714047091970457),
('tab', 0.0019169041997326775),
('efficacious', 0.0018296853663098695),
('labled', 0.0018082337442324734),
('glycerin', 0.0017687048472223182),
('farms', 0.0017085325270417811),
('bonemeal', 0.001657463131506311),
('vowed', 0.0015148608569398412)]

So in this post, we have seen, how we can interpret some machine learning models. In some of the fields such as Aviation, Medical Diagnosis, etc model interpretability is a must-have feature. Yeah, there are some fields where model interpretability is not that important such as Cricket Match Result Prediction,  Recommendation System (Partially Important), etc. Algorithms like XGboost, SVM are difficult to interpret. 


5 thoughts on Interpretation of Machine Learning Models

Comments are currently closed.