Analysis and visualization of the first and second 2020 Debates

mitra mirshafiee
Analytics Vidhya
Published in
9 min readNov 3, 2020

As I was watching the first and second debate I was fascinated by how each speaker tends to use certain words or behave in a specific way throughout the talk. Just like so many others, I’m not a politician. But I know I can understand the character or personality of people by analyzing their talks, seeing how much they use certain words and how much they tend to interrupt or talk in general. So I went on the internet and found scripts of both debates and started exploring. At first, I was just brainstorming and plotting whatever came into my mind, but I found that it can be an amazing opportunity to learn more about some famous packages in natural language processing and writing an article to summarize my journey. Here I mostly want to review what I’ve visualized and tell you about the characters, but if you want to see the code and packages I used, you can simply visit my Kaggle or Colab notebook.

Storyline

In this article, we’re going to start by analyzing paragraphs, then move on to sentences, and at last dive into the words. Hopefully, the dataset is ready for us to use in Kaggle and you can use different types of the transcript as you want. In the CSV file that I used for this analysis, each row is specified to one speaker and the sentences they used, and the timestamp of their utterance. After going over the datasets and filling null values and removing or recovering some discrepancies, we get something like what we have below. By using the minute column we can calculate another column specified to seconds. Because each debate has two parts, each starting from 00:00, this column can be of great help to simply add the time values of the first part to the second.

middle of the second debate

Paragraph level

The Heat of the Discussion!

One of the things you probably have noticed while watching the debates(especially the first one) is how much each interrupts in the talk of the other! So I wanted to see if I can plot some graph that would show how many times and where each starts talking the same time the other is trying to say something and how many times the mediator is trying to calm them down.

So I thought I can plot a graph that shows the number of times each one starts talking in one minute. If the person is not an interrupter they probably don’t tend to talk more than 4 or five times as it is normal if you’re letting the other person talk too.

heatmap of the first debate
heatmap of the second debate

Generally speaking, in both debates, there are three parts where the candidates start firing at each other, one after the introductions and warming up, another in the middle, and one around 15 minutes before the end where they try to prove their points by talking faster and more to finish off strong!

Also after each heated discussion, we can see that they cool down and talk normally for about 20 minutes.

Sentence Level

Who dominates the discussion?

Now let’s use a sentence tokenizer and analyze the sentences used in each debate. I used NLTK sent_detector for this task and added one another column to count the number of sentences, each time they start talking.

Number of sentences used by debaters in total

By plotting the number of sentences we can see that in both debates Trump is the one talking more(using more sentences to be precise) and dominates more than 40 percent of the conversation.

while analyzing this plot, I thought the number of sentences is not showing all the characteristics of the debaters. If we want to see whether the person is really interrupting, we have to count the number of sentences they use, each time they start talking. So below I plotted a graph that shows in the first debate, Trump and Biden each started talking and said only one sentence about 190 and 150 times. This shows that for 40 times, Trump was either interrupting or simply answering shortly to the mediator(which I don’t think is the case, given that Trump is the one showing the most propensity to talk using more sentences, too.) But we can see a massive improvement in the second debaters as both debaters dropped their number by 100, reaching around 70 and 50 times. (Although this can be also a little because of the length of the discussions too, as the first debate is about 20 minutes shorter than the first.)

Sentence Level

Most Common words

To explore the lexicon used by candidates, we simply take all the text and try to clean the unnecessary words and punctuations like ‘is’, ‘in’, ‘at’, etc. as much as possible to get to the gist of what they’re trying to say. I didn’t remove all the stopwords as you may notice, because I wanted to see whether there is a pronoun that one person uses more than the other (like ‘I’.)

most common words used by each

If you look closer to the treemap above, you’ll notice that each person has a different way of referring to the other. Trump is more likely to address his opponent directly, while Biden tends to talk more to the mediator and use the pronoun ‘he’ to address Trump. From what I see, they mostly are using the same words, but in different ways. For example, of all the top 60 words Trump uses, around 10 percent is the pronoun ‘I’ (nearly twice as much as Biden does), another 10 percent is the pronoun ‘you’, while Biden tends to use words more broadly than Trump by not having any word that dominates more than 7 percent of his commonly used words.

☁️WordCloud

Having all these words, we can use the most common words used by each candidate to make a word cloud for each. For a word cloud in a picture(or a mask) you have to have two things:1. Words 2. Mask(or the outline picture. Just remember that the picture has to have a transparent background.)

👈 Polarity and Subjectivity with TextBlob👉

Polarity is a value between -1 and 1 that shows how positive or negative the sentiment of a sentence is. What TextBlob does is that it first identifies the words that are in its lexicon and then averages the polarity of all different meanings of one word and then multiplies these average with each other and gives us that number as the polarity of the whole sentence. This is a great blog post where you can read more about how it is calculated.

But because polarity is a value and not a concrete label, I decided to divide the spectrum into 5 different groups and give each a label:

0: negative, 1: somewhat negative, 2: neutral, 3: somewhat positive, 4: positive

Subjectivity is a factor between 0 and 1, showing how subjective a word or phrase is. (Pretty obvious! I know.)

Although TextBlob is one of the most famous libraries for processing text, I tend not to use it a lot or trust it as much as I trust deep learning models in sentiment analysis. That is because it uses a naive approach to analyze sentences and if not in a very obvious case, it may not always get the right answer. So here I’ll only plot the subjectivity and polarity of debaters talk and won’t go into detail of what we see on each graph, I’ll leave the room for our next step which is sentiment analysis with 🤗 Transformers.

subjectivity
polarity

Transfer learning with🤗 Transformers

With Transformers we have a variety of tools and models in front of us to do whatever task we want with only a few lines of code. In this article, I’ll quickly introduce Pipelines for those who just want a quick analysis, but for the classification, I’ll use a RoBERTa model trained on a dataset with 5 different labels.

Pipelines:

Advantages: Pipelines are tools provided by the transformers library to help us do a wide range of tasks with our textual data with only one or two lines of code! For example by using the pipeline function and just inputting our data and the name of the task we want to perform we can simply get the result we want without worrying about constructing a model or training it. Take a look at the code below to get the intuition.

You may wonder this function performs. well, the default model that is used by the transformers pipeline for sentiment analysis is a pretrained “distilbert-base-uncased” model that has been trained on sst2 (movie reviews dataset). So you are actually using transfer learning and predict new sets of data based on the weights that were trained by another person or organization.

You can see a live demo of sentiment classification here. The models are very varied and you can both use them here or see the live demo of every and each one on the Huggingface website.

Disadvantages: All these models are pretty useful and good but remember all models are not as accurate or good as their other fellows! For example, if you go with the default(distillbert model which is not the most accurate or best among transformers models).

Pipeline models were trained on a certain dataset with specified labels, so you can’t get anything different from what they can offer you. Like in this example if we want to have 5 labels, but because the sst2 dataset was based on a dataset with 3 different labels: Negative, Neutral, and positive, we can’t get what we exactly want. And although there are a few flaws when you classify your text with distillbert, it is still a great way (I think better than polarity) for sentiment analysis.

RoBERTa

So as I said I really wanted to get the best and most accurate results and have 5 labeled outputs to identify different sentiments as best as I could. So after searching a bit, I found this dataset on Kaggle. Although it contains classified text based on movie reviews, it’s training set includes all different words and phrases separately so we can identify and learn the sentiment of each word, phrase, and sentence using a partly customized model!

The best model that we can get our hands on, is a Large Roberta model that outperforms Large Bert, XLNet, and DistillBert. (I actually experimented with all these models to see if it really is the best model and it turns out that it can give us great results with even a few numbers of epochs)

Here is the Colab notebook in which I included all you need to use any Tensorflow transformers model. You can see how I trained these models and how you can use it to train your own models too!

sentiment analysis

As you can see our model is identifying sentiments better and can give us more concrete results. Interestingly, we can see how Trump is ahead of Biden when it comes to expressing emotion in his sentences. Although one time with a small margin, Biden is using more ‘somewhat positive’ sentences in the first debate. Also by comparing the Etwo debates we can see how the first debate has not only more neutral sentences but also sentences with more negative sentiment. So I think overall, both candidates did their best in the second debate to keep hostility and interruptions out of their debate.

Endnote

All that you read in this notebook was what I could understand from what I saw from the data. I do not oppose or confirm any candidate or political group or offend any individual. I’d love to hear from you guys and hear what you think from all the plots above and what your analysis of each plot is! Make sure to leave your comments below and also leave a clap on this article!

source:https://medium.com/hackernoon/how-i-implemented-the-medium-clap-from-scratch-4a16ac90ad3b

--

--