Skip to content

Muhammet Dursun Şahin

tech,psychology

Menu
  • CX
  • UX/UI
  • Data
  • Genel
Menu

Sentiment Analysis and Visualization Using Python

Posted on 30 January 2025

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, and negative reviews.
  • 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.

Share this:

  • Share on X (Opens in new window) X
  • Share on Facebook (Opens in new window) Facebook

Like this:

Like Loading…

Related

Leave a Reply Cancel reply

Recent Posts

  • Getir Ödeme Sistemleri Vaka Çalışması | 6 Aşamalı İş Analizi
  • Arzum Okka: Müşteri Geri Bildirimlerine Dayalı Ürün Analizi
  • Apple Health Step Tracking Feature: A Product Case Study
  • User Registration & Onboarding Activity Diagram
  • NexaCRM

CATEGORY

Recent Posts

  • Getir Ödeme Sistemleri Vaka Çalışması | 6 Aşamalı İş Analizi 24 July 2026

How to Improve NPS Score: Strategies That Actually Work

©2026 Muhammet Dursun Şahin | Design: Newspaperly WordPress Theme
Loading Comments...
%d