Twitter Sentiment Analysis - Learn Python for Data Science #2

Siraj Raval · Beginner ·🧠 Large Language Models ·9y ago

Key Takeaways

This video demonstrates Twitter sentiment analysis using the Twitter API, Tweepy, TextBlob, and Python, covering topics such as tokenization, bag of words model, and sentiment lexicon.

Full Transcript

[Music] Uh, check out the Two-Minut Papers YouTube channel for some cool research videos. Hello world. It's Siraj. In this episode, we're going to learn how to write a Python script that uses Twitter to understand how people are feeling about a topic that we choose using the natural language library text blob in just 14 lines of code. People really confuse me. Like, how does Donald Trump have so many supporters in 2016? And why are beers manly and fruity drinks girly? Oh, and why do people think anything is beautiful? Like what even is beauty? The soundtrack is beauty. Human preferences are practically unpredictable. And we've invented the sciences of psychology and sociology to help us study these things. Both are the scientific study of people and all the emotion and relationships and behaviors that they display. Traditionally, psychologists would formulate a hypothesis. Then to test it, they would find a subset of people that fit their category, bring them into the lab, then ask them to do some task while recording the results. But with data freely available, we data scientists can do the same thing with the best psychology tool out there, Twitter. Twitter is a treasure trove of sentiment. People around the world output thousands of reactions and opinions on every topic under the sun every second of every day. It's like one big psychological database that's constantly being updated. And we can use it to analyze millions of text snippets in seconds with the power of machine learning. So, how does sentiment analysis work? Well, when we receive some input text, like a tweet, we'll first want to split it up into several words or sentences. This process is called tokenization because we're creating small tokens from big text. We can just count the number of times each word shows up once the text is tokenized. This is called the bag of words model. Then we could look up the sentiment value for each word from a sentiment lexicon that has it all pre-recorded to classify the total sentiment value of our tweet. Okay, so our process will be to register to access Twitter's API, install our dependencies, then write our sentiment analyzer script. Let's start by signing up to use Twitter's API. An API or application programming interface is a gateway that lets you access some servers internal functionality. In our case, Twitter. So, we'll be able to read and write tweets right from our app using Twitter's API. Let's sign up for it in the browser. First, we'll want to make sure that we have our own Twitter account set up with a verified phone number attached to it. Once we have that, we can create our first Twitter app. We'll select the create new app icon, then type in a name and description of our app, as well as a website. The website field is kind of like a liberal arts degree. It doesn't really matter. Then we can click the create button at the bottom, and our app is created. We can click on our app and go to the keys and access tokens tab. We're going to need to use these keys in our script to let us authenticate or verify our identity with Twitter. Okay, let's move on to installing our dependencies. Our first dependency is called Tweepy, which is our library for accessing the Twitter API. Our other dependency is called Text Blob, which will help us perform the actual sentiment analysis. Before we get to the code, let's do a little text blob demo right from the Python interpreter in terminal. We can access the interpreter by typing in Python. That's it. Now we can write Python directly into terminal. We'll start out by importing text blob. Now, let's create our first text blob. This is just a piece of text. We'll call our variable blob and assign it this value. We'll wrap a string using the text blob method and our string will be Siraj is angry. He never gets good matches on Tinder. Now that we have our blob variable, if we want to see parts of speech, we can just access the tags attribute. If we want to tokenize this text, we can just type in the words attribute. And we can see the sentiment of this blob easily by accessing the sentiment.polarity attribute. That's on a scale between negative 1 and 1. All right, first things first. We want to create four variables that authenticating with Twitter will require. We'll find these in the dashboard and we can copy and paste each of them as strings. Let's authenticate with Twitter which basically means login via code. To do that, we'll create a variable called O for authentication and use the OOTH handler method of Twei. This method takes two arguments, the consumer key and the consumer secret. This method was written inside of the Twe Pie library with a bunch of code that is hidden to us. We don't need to know the details for our use case. We can just name the method and all of its functionality is in our hands. The arguments are what the method uses to perform its internal calculation, whatever it is. We're halfway through authentication. The other half is to call the set access token method on the O variable, which takes two arguments, the access token and the access token secret. That's it. We've created our authentication variable. Now, we want to create our main variable from which we'll do all of our Twitter magic. We'll call it API and assign it a value from the Tweepi API method which takes a single authentication argument which we'll fill in right here. Now that we have our API variable, we can perform a whole bunch of different possible methods. But what we're going to do is search for tweets. For our use case, we want to collect tweets that contain a certain keyword. So to do that, let's create a public tweets variable that is going to store a list of tweets. To fill it, we'll call the search method of the API variable. The search method takes a single argument. Let's make it trump. This method is going to retrieve a bunch of tweets that contain the word Trump. From here, we can print them all out to terminal. To do that, we'll create a for loop. For tweet in public tweets colon a for loop increments through every value in a list. So, the question now is, what do we want to do with each [Music] tweet? Okay, let's print them out. Each tweet has a text attribute, which is the string version of it. So, we'll print tweet.ext. It's time to perform our sentiment analysis. We'll create an analysis variable that will store our analysis and call text blob with our tweet string as the only argument. Then we can print out the sentiment attribute of the analysis variable. We can see each tweet related to Trump as well as its sentiment analysis. Polarity measures how positive or negative some text is and subjectivity measures how much of an opinion it is versus how factual. So to break it down, sentiment analysis is the process of extracting and understanding human feelings from data. An API lets you access a server's internal functionality like Twitter right from your app. And the text block Python library lets you perform sentiment analysis in just a few lines of code. There's a lot more we can do with our sentiment analyzer. And the challenge for this week's video is to extend this code to make it create a labeled CSV file with the clean tweets of whatever topic selected. Details are in the code readme. Post your GitHub link in the comments and I'll announce the winner in next week's video. The winner for the gender classification challenge from the last episode is Naresh Nagubashan. I especially liked how he created a dictionary at the end to store the result from each of the three classifiers, then used the argmax function to print out the one with the best result rather than doing a bunch of if then statements. Badass of the week. Also, the runner up is Victor Matay. Very clean code. For more info, check out the links in the description and please subscribe for more programming videos. For now, I've got a Siraj, the president's on the line. You're our only hope. I've got to save the world. So thanks for watching.

Original Description

In this video we'll be building our own Twitter Sentiment Analyzer in just 14 lines of Python. It will be able to search twitter for a list of tweets about any topic we want, then analyze each tweet to see how positive or negative it's emotion is. The coding challenge for this video is here: https://github.com/llSourcell/twitter_sentiment_challenge Naresh's winning code from last episode: https://github.com/Naresh1318/GenderClassifier/blob/master/Run_Code.py Victor's Runner up code from last episode: https://github.com/Victor-Mazzei/ml-gender-python/blob/master/gender.py I created a Slack channel for us, sign up here: https://wizards.herokuapp.com/ More on TextBlob: https://textblob.readthedocs.io/en/dev/ Great info on Sentiment Analysis: https://www.quora.com/How-does-sentiment-analysis-work Great sentiment analysis api: http://www.alchemyapi.com/products/alchemylanguage/sentiment-analysis Read over these course notes if you wanna become an NLP god: http://cs224d.stanford.edu/syllabus.html Best book to become a Python god: https://learnpythonthehardway.org/ Please share this video, like, comment and subscribe! That's what keeps me going. Feel free to support me on Patreon: https://www.patreon.com/user?u=3191693 Two Minute Papers Link: https://www.youtube.com/playlist?list=PLujxSBD-JXgnqDD1n-V30pKtp6Q886x7e Follow me: Twitter: https://twitter.com/sirajraval Facebook: https://www.facebook.com/sirajology Instagram: https://www.instagram.com/sirajraval/ Instagram: https://www.instagram.com/sirajraval/ Signup for my newsletter for exciting updates in the field of AI: https://goo.gl/FZzJ5w Hit the Join button above to sign up to become a member of my channel for access to exclusive content! Join my AI community: http://chatgptschool.io/ Sign up for my AI Sports betting Bot, WagerGPT! (500 spots available): https://www.wagergpt.xyz
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Siraj Raval · Siraj Raval · 41 of 60

1 What is Bitcoin?
What is Bitcoin?
Siraj Raval
2 5 Ways to Use Bitcoin
5 Ways to Use Bitcoin
Siraj Raval
3 BTC Fever - Siraj [Music Video]
BTC Fever - Siraj [Music Video]
Siraj Raval
4 5 Reasons to Build Decentralized Apps
5 Reasons to Build Decentralized Apps
Siraj Raval
5 The Interplanetary File System
The Interplanetary File System
Siraj Raval
6 How to Build a Dapp in 3 min
How to Build a Dapp in 3 min
Siraj Raval
7 Life Before Smartphones
Life Before Smartphones
Siraj Raval
8 4 Ways to Use Smart Contracts
4 Ways to Use Smart Contracts
Siraj Raval
9 3 Dapps You HAVE to See
3 Dapps You HAVE to See
Siraj Raval
10 Char's Life as a BitTorrent Engineer
Char's Life as a BitTorrent Engineer
Siraj Raval
11 4 Reasons AlphaGo is a Huge Deal
4 Reasons AlphaGo is a Huge Deal
Siraj Raval
12 Build a Neural Net in 4 Minutes
Build a Neural Net in 4 Minutes
Siraj Raval
13 Sentiment Analysis in 4 Minutes
Sentiment Analysis in 4 Minutes
Siraj Raval
14 The Hackathon Life
The Hackathon Life
Siraj Raval
15 Your First ML App - Machine Learning for Hackers #1
Your First ML App - Machine Learning for Hackers #1
Siraj Raval
16 Build an AI Composer - Machine Learning for Hackers #2
Build an AI Composer - Machine Learning for Hackers #2
Siraj Raval
17 Build a Game AI - Machine Learning for Hackers #3
Build a Game AI - Machine Learning for Hackers #3
Siraj Raval
18 Build a Movie Recommender - Machine Learning for Hackers #4
Build a Movie Recommender - Machine Learning for Hackers #4
Siraj Raval
19 Build an AI Artist - Machine Learning for Hackers #5
Build an AI Artist - Machine Learning for Hackers #5
Siraj Raval
20 Build a Chatbot - ML for Hackers #6
Build a Chatbot - ML for Hackers #6
Siraj Raval
21 Build an AI Reader - Machine Learning for Hackers #7
Build an AI Reader - Machine Learning for Hackers #7
Siraj Raval
22 Build an AI Writer - Machine Learning for Hackers #8
Build an AI Writer - Machine Learning for Hackers #8
Siraj Raval
23 Build a Chatbot w/ an API - ML for Hackers #9
Build a Chatbot w/ an API - ML for Hackers #9
Siraj Raval
24 One-Shot Learning - Fresh Machine Learning #1
One-Shot Learning - Fresh Machine Learning #1
Siraj Raval
25 Generative Adversarial Nets - Fresh Machine Learning #2
Generative Adversarial Nets - Fresh Machine Learning #2
Siraj Raval
26 Tone Analysis - Fresh Machine Learning #3
Tone Analysis - Fresh Machine Learning #3
Siraj Raval
27 Generate Rap Lyrics - Fresh Machine Learning #4
Generate Rap Lyrics - Fresh Machine Learning #4
Siraj Raval
28 Build an Autoencoder in 5 Min - Fresh Machine Learning #5
Build an Autoencoder in 5 Min - Fresh Machine Learning #5
Siraj Raval
29 Build a Self Driving Car in 5 Min - Fresh Machine Learning #6
Build a Self Driving Car in 5 Min - Fresh Machine Learning #6
Siraj Raval
30 Build an Antivirus in 5 Min - Fresh Machine Learning #7
Build an Antivirus in 5 Min - Fresh Machine Learning #7
Siraj Raval
31 TensorFlow in 5 Minutes (tutorial)
TensorFlow in 5 Minutes (tutorial)
Siraj Raval
32 Build a Recurrent Neural Net in 5 Min
Build a Recurrent Neural Net in 5 Min
Siraj Raval
33 Build a Simulation in 5 Min
Build a Simulation in 5 Min
Siraj Raval
34 Build a TensorFlow Image Classifier in 5 Min
Build a TensorFlow Image Classifier in 5 Min
Siraj Raval
35 Tensorboard Explained in 5 Min
Tensorboard Explained in 5 Min
Siraj Raval
36 Generate Music in TensorFlow
Generate Music in TensorFlow
Siraj Raval
37 Build a Game Bot (LIVE)
Build a Game Bot (LIVE)
Siraj Raval
38 Deep Learning Frameworks Compared
Deep Learning Frameworks Compared
Siraj Raval
39 Introduction - Learn Python for Data Science #1
Introduction - Learn Python for Data Science #1
Siraj Raval
40 Build a Neural Network (LIVE)
Build a Neural Network (LIVE)
Siraj Raval
Twitter Sentiment Analysis - Learn Python for Data Science #2
Twitter Sentiment Analysis - Learn Python for Data Science #2
Siraj Raval
42 Recommendation Systems - Learn Python for Data Science #3
Recommendation Systems - Learn Python for Data Science #3
Siraj Raval
43 Predicting Stock Prices - Learn Python for Data Science #4
Predicting Stock Prices - Learn Python for Data Science #4
Siraj Raval
44 Pong Neural Network (LIVE)
Pong Neural Network (LIVE)
Siraj Raval
45 Deep Dream in TensorFlow - Learn Python for Data Science #5
Deep Dream in TensorFlow - Learn Python for Data Science #5
Siraj Raval
46 Visualizing Data with D3.js (LIVE)
Visualizing Data with D3.js (LIVE)
Siraj Raval
47 Genetic Algorithms - Learn Python for Data Science #6
Genetic Algorithms - Learn Python for Data Science #6
Siraj Raval
48 Enter Siraj [Music Video]
Enter Siraj [Music Video]
Siraj Raval
49 Build a Web Scraper (LIVE)
Build a Web Scraper (LIVE)
Siraj Raval
50 Why is P vs NP Important?
Why is P vs NP Important?
Siraj Raval
51 How to Make a Neural Network (LIVE)
How to Make a Neural Network (LIVE)
Siraj Raval
52 How to Make an Amazing Tensorflow Chatbot Easily
How to Make an Amazing Tensorflow Chatbot Easily
Siraj Raval
53 How to Make an Amazing Video Game Bot Easily
How to Make an Amazing Video Game Bot Easily
Siraj Raval
54 How to Make a Tensorflow Neural Network (LIVE)
How to Make a Tensorflow Neural Network (LIVE)
Siraj Raval
55 How to Make a Simple Tensorflow Speech Recognizer
How to Make a Simple Tensorflow Speech Recognizer
Siraj Raval
56 Joel Shor - Really Quick Questions with an Awesome Google Engineer
Joel Shor - Really Quick Questions with an Awesome Google Engineer
Siraj Raval
57 How to Make a Path Planning Algorithm Easily (LIVE)
How to Make a Path Planning Algorithm Easily (LIVE)
Siraj Raval
58 The Best Way to Prepare a Dataset Easily
The Best Way to Prepare a Dataset Easily
Siraj Raval
59 Catherine Olsson - Really Quick Questions with an OpenAI Engineer
Catherine Olsson - Really Quick Questions with an OpenAI Engineer
Siraj Raval
60 How to Make a Tic Tac Toe Neural Network Easily (LIVE)
How to Make a Tic Tac Toe Neural Network Easily (LIVE)
Siraj Raval

This video teaches viewers how to build a Twitter sentiment analyzer using Python and various libraries, covering key concepts such as sentiment analysis, natural language processing, and machine learning. Viewers will learn how to register for the Twitter API, install necessary libraries, and write a sentiment analyzer script.

Key Takeaways
  1. Register to access Twitter API
  2. Install Tweepy and TextBlob libraries
  3. Write sentiment analyzer script
  4. Create Twitter app
  5. Get API keys and access tokens
  6. Create a text blob and access its parts of speech, tokens, and sentiment
  7. Authenticate with Twitter using Tweepy and set access tokens
  8. Search for tweets containing a keyword using Tweepy's search method
  9. Perform sentiment analysis on each tweet using TextBlob
  10. Create a labeled CSV file with clean tweets of a selected topic
💡 The video highlights the importance of using a combination of libraries such as Tweepy and TextBlob to simplify the process of sentiment analysis on Twitter data.

Related Reads

Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →