Sentiment Analysis Using Machine Learning
1. Introduction
Sentiment Analysis is a Natural Language Processing (NLP) task that is used to identify the emotional tone or opinion expressed in a piece of text. It allows computers to analyze human language and determine whether a particular statement expresses a positive, negative, or neutral opinion.
Sentiment Analysis is widely used in many real-world applications. Companies can use it to understand customer opinions about their products and services. Social media platforms can analyze comments and posts to understand public opinions. Businesses can also use sentiment analysis to analyze customer feedback and identify areas that need improvement.
Common applications of Sentiment Analysis include:
Customer reviews
Social media comments
Product feedback
Survey responses
Movie reviews
Online shopping reviews
Customer support messages
A sentiment analysis system can classify text into different categories such as:
Positive 😊
Negative 😞
Neutral 😐
This project focuses on classifying reviews into Positive and Negative sentiments.
This is a Supervised Machine Learning problem because the model is trained using text data that already contains sentiment labels. The model learns patterns from the labeled reviews and uses those patterns to classify new reviews.
In this project, two important techniques are used:
TF-IDF Vectorization – Converts text into numerical features that can be processed by a Machine Learning algorithm.
Logistic Regression – Classifies the reviews into Positive or Negative sentiment categories.
2. Objective
The main objective of this project is to build a Machine Learning model that learns from customer reviews and predicts whether a new review expresses a positive or negative sentiment.
The project demonstrates several important concepts of Natural Language Processing and Machine Learning, including:
Creating a text dataset
Text feature extraction
TF-IDF vectorization
Selecting input and target variables
Training a classification model
Predicting sentiment
Interpreting the prediction
3. Technologies Used
The following technologies and libraries are used:
Python – Main programming language.
Pandas – Used to create and manage the review dataset.
Scikit-learn – Used for TF-IDF vectorization and Logistic Regression.
TF-IDF Vectorizer – Converts text into numerical feature values.
Logistic Regression – Used to classify the sentiment of reviews.
4. Dataset
The dataset contains customer reviews and their corresponding sentiment labels.
Each review is assigned one of two sentiment categories:
Positive
Negative
The sample dataset contains eight reviews, with four positive reviews and four negative reviews.
Dataset
Review Sentiment
0 I love this product, it is amazing Positive
1 The service was excellent and fast Positive
2 This movie was fantastic Positive
3 I am very happy with this purchase Positive
4 The product is terrible Negative
5 I hate this service Negative
6 The quality is very bad Negative
7 This was a disappointing experience Negative
The model uses these labeled examples to learn the relationship between words in the review and the sentiment category.
5. How Sentiment Analysis Works
The system follows several steps to classify a new review.
First, a dataset containing reviews and sentiment labels is created.
Next, the review text is converted into numerical values using TF-IDF Vectorization. Machine Learning algorithms cannot directly process normal sentences, so the text must first be transformed into numerical features.
After converting the reviews into numerical vectors, a Logistic Regression model is created.
The model is then trained using the vectorized reviews and their corresponding sentiment labels.
When a new review is provided, the same TF-IDF vectorizer converts the review into numerical values. The trained Logistic Regression model then analyzes these values and predicts whether the review is Positive or Negative.
6. What is TF-IDF?
TF-IDF stands for Term Frequency-Inverse Document Frequency. It is a commonly used technique for converting text into numerical features.
TF-IDF considers how important a word is within a particular document compared with a collection of documents.
For example, words such as:
excellent
amazing
fantastic
happy
may be useful for identifying positive reviews.
Similarly, words such as:
terrible
hate
bad
disappointing
may provide useful information for identifying negative reviews.
TF-IDF assigns numerical values to words based on their importance in the dataset.
The resulting numerical representation can then be given to a Machine Learning algorithm.
7. Python Implementation
The following Python program implements the Sentiment Analysis system.
# Import required libraries
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
# --------------------------------------------
# Step 1: Create sentiment dataset
# --------------------------------------------
data = {
"Review": [
"I love this product, it is amazing",
"The service was excellent and fast",
"This movie was fantastic",
"I am very happy with this purchase",
"The product is terrible",
"I hate this service",
"The quality is very bad",
"This was a disappointing experience"
],
"Sentiment": [
"Positive",
"Positive",
"Positive",
"Positive",
"Negative",
"Negative",
"Negative",
"Negative"
]
}
# Convert dictionary into DataFrame
df = pd.DataFrame(data)
print("Dataset:")
print(df)
# --------------------------------------------
# Step 2: Select input and target
# --------------------------------------------
X = df["Review"]
y = df["Sentiment"]
# --------------------------------------------
# Step 3: Convert text into numbers
# --------------------------------------------
vectorizer = TfidfVectorizer()
X_vectorized = vectorizer.fit_transform(X)
# --------------------------------------------
# Step 4: Create Machine Learning model
# --------------------------------------------
model = LogisticRegression()
# --------------------------------------------
# Step 5: Train the model
# --------------------------------------------
model.fit(
X_vectorized,
y
)
# --------------------------------------------
# Step 6: Predict new review sentiment
# --------------------------------------------
new_review = [
"The product quality is excellent and I really like it"
]
# Convert review into numerical format
new_review_vector = vectorizer.transform(
new_review
)
prediction = model.predict(
new_review_vector
)
# --------------------------------------------
# Step 7: Display prediction
# --------------------------------------------
print("\nSentiment Prediction:")
print(prediction[0])
8. Explanation of the Python Program
The program begins by importing Pandas and the required Scikit-learn classes.
Pandas is used to create the review dataset, while TfidfVectorizer is used to convert text into numerical features. LogisticRegression is used as the classification algorithm.
Step 1: Create the Dataset
A dictionary containing customer reviews and their sentiment labels is created.
For example:
"I love this product, it is amazing" → Positive
"The product is terrible" → Negative
The dictionary is then converted into a Pandas DataFrame.
Step 2: Select Input and Target
The review column is selected as the input:
X = df["Review"]
The sentiment column is selected as the target:
y = df["Sentiment"]
The model will learn how the words and phrases in the reviews are related to their sentiment labels.
Step 3: Convert Text into Numbers
The TF-IDF vectorizer is created:
vectorizer = TfidfVectorizer()
The training reviews are converted into numerical vectors:
X_vectorized = vectorizer.fit_transform(X)
This allows the Machine Learning algorithm to process the text.
Step 4: Create the Model
A Logistic Regression classifier is created:
model = LogisticRegression()
Logistic Regression is commonly used for classification problems where the output belongs to one or more categories.
Step 5: Train the Model
The model is trained using:
model.fit(X_vectorized, y)
During training, the model learns patterns from the labeled reviews.
Step 6: Predict a New Review
The new review is:
The product quality is excellent and I really like it
The review is first converted into the same numerical representation used during training.
The trained model then predicts its sentiment.
9. Prediction
Input
The product quality is excellent and I really like it
Output
Sentiment Prediction:
Positive
The model predicts that the review expresses a Positive sentiment.
This prediction is based on the patterns learned from the training dataset. Words such as excellent and phrases expressing satisfaction contribute to the positive classification.
10. How Logistic Regression Works
Logistic Regression is a classification algorithm that estimates the probability of an input belonging to a particular class.
In this project, the model learns to distinguish between two classes:
Positive
Negative
After the review is converted into numerical TF-IDF features, Logistic Regression analyzes those features and calculates the most likely sentiment class.
For example, a review containing words associated with positive training examples may receive a higher probability of being classified as Positive.
Similarly, reviews containing words commonly associated with negative examples may be classified as Negative.
11. Advantages
The Sentiment Analysis system has several advantages:
Simple and beginner-friendly.
Can process customer reviews automatically.
Uses a popular NLP technique.
Easy to implement using Python.
Can classify large amounts of text automatically.
Can be used for customer feedback analysis.
Can help businesses understand customer opinions.
Can be extended to support additional sentiment categories.
12. Limitations
The main limitation of this project is the very small training dataset. Only eight reviews are used, which is not enough for a real-world sentiment analysis system.
Real-world language can also be more complex than the examples used in this project. For example, sarcasm, slang, spelling mistakes, emojis, and context can make sentiment classification difficult.
Consider the sentence:
"Great, another problem with this product."
Although the word "Great" appears positive, the overall meaning may be negative.
The current model may also have difficulty understanding context, negation, or sentences that contain both positive and negative opinions.
13. Future Improvements
The system can be improved by using a much larger dataset containing thousands or millions of labeled reviews.
Additional preprocessing techniques can also be introduced, such as:
Removing unnecessary characters
Converting text to lowercase
Removing stop words
Stemming
Lemmatization
Handling emojis
Handling spelling variations
The system can also be extended from binary classification to three classes:
Positive
Negative
Neutral
More advanced Machine Learning and Deep Learning models can also be tested, including:
Naive Bayes
Support Vector Machine
Random Forest
Recurrent Neural Networks
LSTM
Transformer-based models
A train-test split can also be used to properly evaluate the model using metrics such as Accuracy, Precision, Recall, and F1-Score.
14. Real-World Applications
Sentiment Analysis has many practical applications.
Customer Feedback
Companies can automatically analyze customer reviews to determine whether customers are satisfied or dissatisfied.
Social Media Monitoring
Businesses can analyze social media comments to understand how people feel about their products or brands.
Product Reviews
Online shopping platforms can analyze thousands of customer reviews and identify overall customer sentiment.
Survey Analysis
Organizations can process large numbers of survey responses and determine general opinions.
Movie Reviews
Movie platforms can analyze audience reviews to determine whether viewers generally liked or disliked a movie.
15. Conclusion
The Sentiment Analysis Using Machine Learning project demonstrates how Natural Language Processing and Machine Learning can be combined to automatically identify the sentiment of text.
The project uses TF-IDF Vectorization to convert customer reviews into numerical features and Logistic Regression to classify the reviews as Positive or Negative.
For the new review:
"The product quality is excellent and I really like it"
the trained model predicts:
Positive
This project provides a practical introduction to text classification, NLP, feature extraction, supervised learning, and Machine Learning prediction. With a larger dataset and more advanced NLP techniques, it can be developed into a powerful system for analyzing customer feedback, social media opinions, product reviews, and other types of text.
