How to Do Sentiment Analysis on a Twitter Account

Figure out ahead of time if this is a person you want to follow

randerson112358
Feb 4, 2020 · 7 min read
Image for post
Image for post

In this article, I’ll show you how to get and analyze the sentiment of tweets from a Twitter user using sentiment analysis.

“Sentiment analysis is the measurement of neutral, negative, and positive language. It is a way to evaluate spoken or written language to determine if the expression is favorable (positive), unfavorable (negative), or neutral, and to what degree.” — Clarabridge

“Sentiment analysis: the process of computationally identifying and categorizing opinions expressed in a piece of text, especially in order to determine whether the writer’s attitude towards a particular topic, product, etc. is positive, negative, or neutral.”— Oxford English Dictionary

There are many methods and algorithms to implement sentiment-analysis systems, which can be classified as:

  • Rule-based systems that perform sentiment analysis based on a set of manually crafted rules
  • Automatic systems that rely on machine learning techniques to learn from data
  • Hybrid systems that combine both rule-based and automatic approaches

Before you can start analyzing tweets and getting their sentiments from Twitter, you’ll first need a Twitter account yourself. You’ll need to create a Twitter application to get your keys.

The Twitter user whose tweets I’ll be analyzing is none other than Microsoft co-founder Bill Gates. I chose Bill Gates because he’s trying to make a positive impact on the world, so I suspect his tweets will also be mostly positive.

If you’d prefer not to read this article and would like a video representation of it, you can check out the YouTube video below. It goes through everything in this article with a little more detail and will help make it easy for you to start developing your own program — or you can use both as supplementary materials for learning.

Start Programming

I’ll start by stating what I want this program to do. This program will analyze the sentiment of a Twitter account. More specifically, it’ll analyze the tweets/posts of one of Microsoft founders, Bill Gates.

Import The Libraries and Packages

Next, import some of the libraries that’ll be used within this program.

Load the Data and Authenticate to Twitter

I’m using Google’s website to write this program, so I’ll be using Google’s library to upload the CSV file that contains my Twitter app keys. You can just input your keys directly into the variables if you want.

Next, I’ll store the Twitter keys/API credentials in variables.

Now, it’s time to create the authentication object. Set the access token and access token secret, and authenticate to Twitter.

Extract and Analyze the Data

It’s time to extract the tweets of a Twitter user. First, I’ll get the last 100 posts for the Twitter user Bill Gates and show only the five most recent tweets.

Image for post
Image for post
The five most recent tweets

Create a DataFrame with a column called Tweets that’ll contain the posts from the Twitter user, and then show the first five rows.

Image for post
Image for post

The data is a little dirty because it contains the @ symbol, hyperlinks, RTs, and may contain hash tags, so I’ll clean it up by creating a function to remove these symbols from the tweets. Then, I’ll apply that function to the tweets and show the results.

Image for post
Image for post

I want to add the tweets’ subjectivity and polarity to the DataFrame. In order to do this, I’ll create two functions: one to get the tweets called Subjectivity (how subjective or opinionated the text is — a score of 0 is fact, and a score of +1 is very much an opinion) and the other to get the tweets called Polarity (how positive or negative the text is, — score of -1 is the highest negative score, and a score of +1 is the highest positive score).

Next I’ll store the results into two columns — one called Subjectivity and the other called Polarity — and show the results.

Image for post
Image for post

Let’s see how well the sentiments are distributed. A good way to accomplish this task is by understanding the common words by plotting word clouds.

A word cloud (also known as text clouds or tag clouds) is a visualization, the more a specific word appears in the text, the bigger and bolder it appears in the word cloud.

Let’s visualize all the words in the data using the word-cloud plot. It looks like the word “health” appears a lot in Bill Gates past 100 tweets.

Image for post
Image for post

Create a function to compute the negative (-1), neutral (0), and positive (+1) analysis, and add the information to a new column called Analysis. Then, show the results.

Image for post
Image for post

Print the positive tweets in ascending order. The most positive tweet is the #1 tweet.

Image for post
Image for post

Print the negative tweets in descending order. The most negative tweet is the #1 tweet.

Image for post
Image for post

Plot the polarity and subjectivity as a scatter plot. It looks like the majority of the tweets are positive, as many of the points are on the right side of the polarity at value 0.00.

Image for post
Image for post

Print the percentage of positive tweets. About 81/100 of the tweets are considered positive tweets.

Image for post
Image for post

Print the percentage of negative tweets. About 9/100 tweets are considered negative tweets.

Image for post
Image for post

Show the value counts. Now we can see 81 tweets are positive, 10 are neutral, and 9 are negative.

Image for post
Image for post

Visually show the value counts.

Image for post
Image for post

Conclusion

That’s all folks!

If you’re also interested in reading more on machine learning to immediately get started with problems and examples, then I strongly recommend you check out “Hands-on Machine Learning with Scikit-Learn and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems.”

It’s a great book for helping beginners learn how to write machine learning programs and for understanding machine learning concepts.

Thanks for reading this article. I hope it’s helpful to you all!

Better Programming

Advice for programmers.

Sign up for The Best of Better Programming

By Better Programming

A weekly newsletter sent every Friday with the best articles we published that week. Code tutorials, advice, career opportunities, and more! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

randerson112358

Written by

A programmer that loves Computer Science: https://www.youtube.com/user/randerson112358 https://www.youtube.com/channel/UCbmb5IoBtHZTpYZCDBOC1

Better Programming

Advice for programmers.

randerson112358

Written by

A programmer that loves Computer Science: https://www.youtube.com/user/randerson112358 https://www.youtube.com/channel/UCbmb5IoBtHZTpYZCDBOC1

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app