Step 1: Setting Up
Import Required Libraries
The script starts by importing the following libraries:
pandas: For data manipulation and analysis.TextBlob: To analyze the sentiment of text data.matplotlib.pyplot: For plotting graphs.seaborn: To create enhanced data visualizations.
pythonCopyEditimport pandas as pd
from textblob import TextBlob
import matplotlib.pyplot as plt
import seaborn as sns
Step 2: Load Dataset
The dataset is loaded into a DataFrame using the pandas.read_csv() function. In this example, the dataset is located at: C:\Users\90545\Downloads\IMDB_Dataset.csv.
pythonCopyEditdf = pd.read_csv(r"C:\\Users\\90545\\Downloads\\IMDB_Dataset.csv")
Step 3: Sentiment Analysis
Adding Sentiment Scores
The script calculates the sentiment polarity of each review in the dataset using the TextBlob library. Polarity values range from -1 (negative) to 1 (positive).
pythonCopyEditdf["sentiment"] = df["review"].apply(lambda x: TextBlob(x).sentiment.polarity)
Assigning Sentiment Labels
Based on the polarity scores, the script assigns a label (positive, negative, or neutral) to each review.
pythonCopyEditdf["sentiment_label"] = df["sentiment"].apply(lambda x: "positive" if x > 0 else "negative" if x < 0 else "neutral")
Step 4: Data Preview
The head() method is used to display the first few rows of the DataFrame for validation:
pythonCopyEditprint(df.head())
Step 5: Visualizing Sentiments
Three types of visualizations are created:
1. Distribution of Sentiment Labels
A count plot shows the number of reviews for each sentiment label.
pythonCopyEditplt.figure(figsize=(8, 6))
sns.countplot(x="sentiment_label", data=df, palette="viridis")
plt.title("Distribution of Sentiment Labels")
plt.xlabel("Sentiment Label")
plt.ylabel("Count")
plt.show()
Visual Output:
The chart displays three bars representing positive, negative, and neutral reviews with their respective counts.
2. Distribution of Sentiment Polarity Scores
A histogram shows the distribution of polarity scores for the reviews.
pythonCopyEditplt.figure(figsize=(8, 6))
sns.histplot(df["sentiment"], bins=30, kde=True, color="blue")
plt.title("Distribution of Sentiment Polarity Scores")
plt.xlabel("Sentiment Polarity")
plt.ylabel("Frequency")
plt.show()
Visual Output:
The histogram depicts the density and frequency of sentiment polarity values, highlighting whether reviews tend to be positive or negative.
3. Sentiment Polarity by Sentiment Label
A box plot provides insights into the spread and distribution of polarity scores across sentiment labels.
pythonCopyEditplt.figure(figsize=(8, 6))
sns.boxplot(x="sentiment_label", y="sentiment", data=df, palette="Set3")
plt.title("Sentiment Polarity by Sentiment Label")
plt.xlabel("Sentiment Label")
plt.ylabel("Sentiment Polarity")
plt.show()
Visual Output:
This plot showcases the range of polarity scores within each sentiment label (positive, negative, neutral).
Key Points from Images
- The first image visualizes the Distribution of Sentiment Labels, showing counts of
positive,neutral, andnegativereviews. - The second image displays the code implementation in Python, explaining how the dataset is processed, analyzed, and visualized.
Conclusion
This tutorial demonstrates how to perform sentiment analysis using Python and visualize the results effectively. You can customize this script to analyze and visualize sentiment data from other datasets or adjust parameters (e.g., bins in the histogram) for more insights.
