Facebook is a very good platform to perform sentiment analysis task because users are free to express their opinions on any topic be it political or environmental, users are free to share their opinions. In this article, I will introduce you to Facebook Posts Sentiment Analysis with Machine Learning using Python.
Facebook Posts Sentiment Analysis with Machine Learning
For the Facebook posts sentiment analysis task, you need to extract your data from Facebook first, which is a very easy task, just follow the steps mentioned below:
- Go to settings & privacy
- Then go to settings
- From the left click on Your Facebook Information
- Click on view at Download your information
- Then only select posts and click on create file.
Facebook will send you a notification in the next 60 minutes to download your data. You have to search for “your_posts_1.json” file in the downloaded data, as we only need this data fro the task of Facebook posts sentiment analysis with Python.
Also, Read – 200+ Machine Learning Projects Solved and Explained.
Facebook Posts Sentiment Analysis with Python
Now let’s get started with the task of Facebook Posts Sentiment Analysis with Machine Learning using Python. I will start by importing the necessary Python libraries and the dataset:
<class 'list'> <class 'dict'> 5038
Now we need to extract the descriptions from the posts so that we can analyze the sentiments of the posts. I will store the textual data of our posts in a list:
Length of list: 2885 Krish Naik created a Video for meðð Must watch! Amazing Work by @[100010286773956:2048:Hritika Aggarwal]ðº .......
As you can see from the output above, we have a list containing the text I wrote on my Facebook posts. I posted only about data science and machine learning on Facebook so this data is not interesting, but you can implement the same code if you have posted political opinions. The code I wrote is in a form that can be implemented on any data type.
Data Preparation
Now I will perform tokenization on the textual data that we just stored in a list:
[nltk_data] Downloading package punkt to /root/nltk_data... [nltk_data] Unzipping tokenizers/punkt.zip. Flatten sentence token: 3923
Now we need to normalize the data, here I will create some functions for:
- deleting non-ASCII characters
- lowercase all words
- suppression of punctuation
- number replacement
- stop words
Let’s see how to create functions for performing all the tasks mentioned above for preparing the data for Facebook post sentiment analysis:
[nltk_data] Downloading package stopwords to /root/nltk_data... [nltk_data] Unzipping corpora/stopwords.zip. Length of sentences list: 3866
Now let’s see what we can visualize in this data. I will look at the most frequent sentences to see what I share the most on my Facebook posts:

Final Step: Facebook Posts Sentiment Analysis
Now the last step is to analyze the sentiments of the text written in the post description and create a Facebook post sentiment analysis report showing whether the sentiments are neutral, positive, or negative:
[nltk_data] Downloading package vader_lexicon to /root/nltk_data... First five rows of results: sentence numbers 0 krish naik created a video for me must watch {'neg': 0.0, 'neu': 0.778, 'pos': 0.222, 'comp... 1 amazing work by 1000102867739562048hritika agg... {'neg': 0.0, 'neu': 0.513, 'pos': 0.487, 'comp... 2 in this article iall walk you through how to c... {'neg': 0.0, 'neu': 0.833, 'pos': 0.167, 'comp... 3 machine learning full course with python for ... {'neg': 0.0, 'neu': 0.68, 'pos': 0.32, 'compou... 4 practice your skills in data science projects ... {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound... First five rows of results2: label values 0 neg 0.000 1 neu 0.778 2 pos 0.222 3 compound 0.250 4 neg 0.000
I hope you liked this article on Facebook Post sentiment analysis with Machine Learning using Python. Feel free to ask your valuable questions in the comments section below.