Predict US Elections with Python

In this article, I will take you through how we can predict the US presidential elections with Python. Here, I will not train any machine learning model. I will analyze the sentiments of people for the candidates and then at the end, I will conclude based on the most number of positive and negative tweets against the candidates.

The datasets that I am using in this task to predict the US Elections are collected from twitter by the official twitter handles of Donald Trump and Joe Biden. You can download the datasets that I am using from here.

Also, Read – Machine Learning Full Course For Free.

Predict US Elections with Python

Now without wasting any time let’s get started with this task to predict the US Elections with Python by importing the necessary libraries and the datasets:

Now let’s have a quick look at the first 5 rows from both the datasets:

print(trump_reviews.head()) print(biden_reviews.head())
         user          text
0      manny_rosen   @sanofi please tell us how many shares the Cr...
1        osi_abdul   https://t.co/atM98CpqF7  Like, comment, RT #P...
2          Patsyrw   Your AG Barr is as useless & corrupt as y...
3  seyedebrahimi_m   Mr. Trump! Wake Up!  Most of the comments bel...
4    James09254677   After 4 years you think you would have figure...
       user            text
0   MarkHodder3    @JoeBiden And we’ll find out who won in 2026...
1    K87327961G  @JoeBiden Your Democratic Nazi Party cannot be...
2      OldlaceA                        @JoeBiden So did Lying Barr
3    penblogger  @JoeBiden It's clear you didnt compose this tw...
4  Aquarian0264         @JoeBiden I will vote in person thank you.

Sentiment Analysis

Now, I will get started with sentiment analysis. I will do it by using the Textblob package in Python. Here I will use this package to perform simple text classification in either positive or negative on the basis of sentiment analysis:

Trump : Sentiment(polarity=0.15, subjectivity=0.3125)
Biden : Sentiment(polarity=0.6, subjectivity=0.9)

              user  ...        Sentiment Polarity
2783          4diva63  ...              0.000
2784         hidge826  ...              0.000
2785     SpencerRossy  ...              0.225
2786  ScoobyMcpherson  ...              0.000
2787          bjklinz  ...             -0.500
           user  ...       Sentiment Polarity
2535    meryn1977  ...               0.15
2536  BSNelson114  ...               0.00
2537     KenCapel  ...               0.00
2538   LeslyeHale  ...               0.10
2539     rerickre  ...               0.20

Now before moving forward let’s understand what is Polarity. Polarity ranges from -1 to +1(negative to positive) and tells whether the text has negative sentiments or positive sentiments. Polarity tells about factual information.

Sentiment Polarity on Both the candidates:

Now I will add a new attribute in both the datasets by the name of “Expression Label”:

Now I will drop all the tweets with neutral polarity from both the datasets to balance the data equally. I will also perform some data cleaning operations so that at the can we can easily predict the US Elections:

Now, before moving forward we need to balance both the datasets:

Now let’s analyze the data to predict the US Elections, by analyzing the number of positive and negative sentiments in both the accounts:

US Elections Prediction

From the above figure, it is very clear that Joe Biden is getting more Positive tweets and less negative tweets as compared to Donald Trump. So it will not be wrong to conclude that Joe Bined is more prefered by the people to win the US Presidential Elections than Donald Trump.

I hope you liked this article on how to predict the US Elections winner. The analysis is totally based on the twitter data. Feel free to ask your valuable questions in the comments section below.

Follow Us:

Default image
Aman Kharwal

I am a programmer from India, and I am here to guide you with Data Science, Machine Learning, Python, and C++ for free. I hope you will learn a lot in your journey towards Coding, Machine Learning and Artificial Intelligence with me.

Leave a Reply