Path: blob/master/Natural Language Processing with Classification and Vector Spaces/Week 2 - Sentiment Analysis with Naive Bayes/NLP_C1_W2_lecture_nb_01.ipynb
14375 views
Visualizing Naive Bayes
In this lab, we will cover an essential part of data analysis that has not been included in the lecture videos. As we stated in the previous module, data visualization gives insight into the expected performance of any model.
In the following exercise, you are going to make a visual inspection of the tweets dataset using the Naïve Bayes features. We will see how we can understand the log-likelihood ratio explained in the videos as a pair of numerical features that can be fed in a machine learning algorithm.
At the end of this lab, we will introduce the concept of confidence ellipse as a tool for representing the Naïve Bayes model visually.
Calculate the likelihoods for each tweet
For each tweet, we have calculated the likelihood of the tweet to be positive and the likelihood to be negative. We have calculated in different columns the numerator and denominator of the likelihood ratio introduced previously.
We did not include the code because this is part of this week's assignment. The 'bayes_features.csv' file contains the final result of this process.
The cell below loads the table in a dataframe. Dataframes are data structures that simplify the manipulation of data, allowing filtering, slicing, joining, and summarization.
Using Confidence Ellipses to interpret Naïve Bayes
In this section, we will use the confidence ellipse to give us an idea of what the Naïve Bayes model see.
A confidence ellipse is a way to visualize a 2D random variable. It is a better way than plotting the points over a cartesian plane because, with big datasets, the points can overlap badly and hide the real distribution of the data. Confidence ellipses summarize the information of the dataset with only four parameters:
Center: It is the numerical mean of the attributes
Height and width: Related with the variance of each attribute. The user must specify the desired amount of standard deviations used to plot the ellipse.
Angle: Related with the covariance among attributes.
The parameter n_std stands for the number of standard deviations bounded by the ellipse. Remember that for normal random distributions:
About 68% of the area under the curve falls within 1 standard deviation around the mean.
About 95% of the area under the curve falls within 2 standard deviations around the mean.
About 99.7% of the area under the curve falls within 3 standard deviations around the mean.
In the next chart, we will plot the data and its corresponding confidence ellipses using 2 std and 3 std.
In the next cell, we will modify the features of the samples with positive sentiment (1), in a way that the two distributions overlap. In this case, the Naïve Bayes method will produce a lower accuracy than with the original data.
Now let us plot the two distributions and the confidence ellipses
To give away: Understanding the data allows us to predict if the method will perform well or not. Alternatively, it will allow us to understand why it worked well or bad.