Natural language processing tasks such as text classification and sentiment analysis can be explained using explainable AI libraries such as SHAP and ELI5. The objective of explaining the text classification tasks or sentiment analysis tasks is to let the user know how a decision was made. The predictions are generated using a supervised learning model for unstructured text data. The input is a text sentence or many sentences or phrases, and we train a machine learning model to perform text classification such as customer review classification, feedback classification, newsgroup classification, etc. In this chapter, we will be using explainable libraries to explain the predictions or classifications.
Document classification, where the input is a series of sentences extracted from a document, and the output is the label attached to the document. If a document is misclassified or someone wants to know why a document is being classified by the algorithm in a certain way, we need to explain why.
For named entity recognition tasks, we need to predict the entity to which a name belongs. If it is assigned to another entity, we need to explain why.
For sentiment analysis, if a sentiment category is wrongly assigned to another category, then we need to explain why.
Recipe 5-1. Explain Sentiment Analysis Text Classification Using SHAP
Problem
You want to explain sentiment analysis prediction using SHAP.
Solution
The solution takes into account the most common dataset available, which is the IMDB sentiment classification dataset from the SHAP library. It can be accessed using the SHAP dataset. We will be using the SHAP library to explain the predictions.
How It Works
00 | 000 | 007 | 01 | 02 | 05 | 06 | 10 | 100 | 1000 | ... | zombi | zombie | zombies | zone | zoo | zoom | zooms | zorro | zu | zucker | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.000000 | 0.0 | 0.0 | ... | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
1 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.000000 | 0.0 | 0.0 | ... | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
2 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.000000 | 0.0 | 0.0 | ... | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
3 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.000000 | 0.0 | 0.0 | ... | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
4 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.078969 | 0.0 | 0.0 | ... | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
Recipe 5-2. Explain Sentiment Analysis Text Classification Using ELI5
Problem
You want to explain sentiment analysis prediction using ELI5.
Solution
The solution takes into account the most common dataset available, which is the IMDB sentiment classification. We will be using the ELI5 library to explain the predictions.
How It Works
Weight? | Feature |
---|---|
+3.069 | x6530 |
+2.195 | x748 |
+1.838 | x1575 |
+1.788 | x5270 |
+1.743 | x8807 |
… 8173 more positive … | |
… 8234 more negative … | |
-1.907 | x15924 |
-1.911 | x1239 |
-2.027 | x9976 |
-2.798 | x16255 |
-3.643 | x1283 |
Weight? | Feature |
---|---|
+3.069 | great |
+2.195 | and |
+1.838 | best |
+1.788 | excellent |
+1.743 | love |
+1.501 | well |
+1.477 | wonderful |
+1.394 | very |
… 8170 more positive … | |
… 8227 more negative … | |
-1.391 | just |
-1.407 | plot |
-1.481 | poor |
-1.570 | even |
-1.589 | terrible |
-1.612 | nothing |
-1.723 | boring |
-1.907 | waste |
-1.911 | awful |
-2.027 | no |
-2.798 | worst |
-3.643 | bad |
Recipe 5-3. Local Explanation Using ELI5
Problem
You want to explain an individual review in the sentiment analysis prediction using ELI5.
Solution
The solution is takes into account the most common dataset available, which is the IMDB sentiment classification dataset. We will be using the ELI5 library to explain the predictions.
How It Works
Contribution? | Feature |
---|---|
+0.869 | Highlighted in text (sum) |
+0.174 | <BIAS> |
Contribution? | Feature |
---|---|
+0.935 | Highlighted in text (sum) |
-0.174 | <BIAS> |
Contribution? | Feature |
---|---|
+1.313 | Highlighted in text (sum) |
-0.174 | <BIAS> |
this movie was so poorly written and directed i fell asleep 30 minutes through the movie……………….
The green patches show positive features for the target class positive, and the red parts are negative features that correspond to the negative class. The feature value and the weight value indicate the relative importance of words as features in classifying sentiments. It is observed that many stop words or unwanted words are present in the tokenization process; hence, they are appearing as features in the feature importance. The way to clean it up is to use preprocessing steps such as applying stemming, removing stop words, performing lemmatization, removing numbers, etc. Once the text cleanup is completed, then the previous recipes can be used again to create a better model to predict the sentiments.
Conclusion
In this chapter, we covered how to interpret the text classification use cases such as sentiment analysis. However, for all such kinds of use cases, the process will remain same, and the same recipes can be used. The modeling technique selection may change as the features increase, and we can use complex models such as ensemble modeling techniques like random forest, gradient boosting techniques, and catboost techniques. Also, the preprocessing methods can change. For example, the count vectorizer, TF-IDF vectorizer, hashing vectorizer, etc., can be applied with stop word removal to clean the text to get better features. The recipes to run different variants of ensemble models were covered in the previous chapter. In the next chapter, we are going to cover times-series model explainability.