A comprehensive sentiment analysis project that analyzes and visualizes sentiment patterns in social media comments using machine learning techniques.
This project performs sentiment analysis on social media data (Reddit and Twitter comments) to understand the emotional tone and sentiment distribution across different categories. The analysis includes data preprocessing, exploratory data analysis (EDA), and visualization of sentiment patterns.
- Data Preprocessing: Clean and prepare social media comments for analysis
- Exploratory Data Analysis: Comprehensive statistical analysis of the dataset
- Sentiment Distribution Analysis: Visualize sentiment categories across comments
- Character Frequency Analysis: Analyze character usage patterns in text data
- Word Frequency by Category: Examine most common words across different sentiment categories
- Interactive Visualizations: Generate insightful plots and charts
Youtube_sentimental_analysis/
β
βββ data/
β βββ Reddit_Data.csv # Reddit comments dataset
β βββ Twitter_Data.csv # Twitter comments dataset
β
βββ Preprocessing_and_eda.ipynb # Main analysis notebook
βββ README.md # Project documentation
- Python 3.x
- Pandas - Data manipulation and analysis
- NumPy - Numerical computing
- Matplotlib - Data visualization
- Seaborn - Statistical data visualization
- Collections - Counter for frequency analysis
- Jupyter Notebook - Interactive development environment
The project uses two main datasets:
- Reddit_Data.csv: Contains Reddit comments with sentiment labels
- Twitter_Data.csv: Contains Twitter comments with sentiment labels
clean_comment: Preprocessed comment textcategory: Sentiment label (-1: Negative, 0: Neutral, 1: Positive)
-
Clone the repository:
git clone <repository-url> cd Youtube_sentimental_analysis
-
Install required packages:
pip install pandas numpy matplotlib seaborn jupyter
-
Launch Jupyter Notebook:
jupyter notebook
-
Open the analysis notebook:
- Navigate to
Preprocessing_and_eda.ipynb
- Navigate to
- Dataset information and structure analysis
- Missing value detection and handling
- Duplicate data identification
- Statistical summary of the data
- Category distribution visualization using count plots
- Analysis of comment distribution across sentiment categories
- Character frequency analysis across all comments
- Word frequency analysis by sentiment category
- Text length distribution analysis
- Count Plots: Show distribution of sentiment categories
- Bar Charts: Display word frequency by category
- Statistical Plots: Character and text length distributions
Generates a stacked horizontal bar chart showing the most frequent words across different sentiment categories.
Parameters:
data: DataFrame containing the comment datan: Number of top words to display (default: 20)start: Starting index for word selection (default: 0)
# Load the data
import pandas as pd
data = pd.read_csv("data/Reddit_Data.csv")
# Basic data exploration
print(data.info())
print(data['category'].value_counts())
# Visualize sentiment distribution
import matplotlib.pyplot as plt
import seaborn as sns
plt.figure(figsize=(10, 7))
sns.countplot(data=data, x='category')
plt.title("Comment Distribution by Category")
plt.show()
# Analyze word frequency by category
plot_word_by_category(data, n=20)The analysis provides insights into:
- Sentiment Balance: Distribution of positive, negative, and neutral comments
- Text Patterns: Common words and characters used in different sentiment categories
- Data Quality: Missing values, duplicates, and data consistency
- Language Usage: Frequency patterns of words across sentiment categories
-
Model Selection & Training:
Multiple machine learning algorithms were explored for sentiment classification, including:- Logistic Regression: A linear model suitable for multiclass classification.
- Support Vector Machine (SVM): Effective for high-dimensional text data.
- Random Forest: An ensemble method that improves robustness and accuracy.
- Naive Bayes: A probabilistic model often used for text classification.
-
Feature Engineering:
The following features were engineered and used for model training:clean_comment: The preprocessed text of each comment.word_count: The number of words in each comment.- (Optionally)
num_stop_words: The number of stopwords in each comment.
-
Pipeline Construction:
scikit-learn'sPipelineandColumnTransformerwere used to:- Apply text vectorization (TF-IDF) to the
clean_commentcolumn. - Scale numeric features (like
word_count). - Chain preprocessing and model training into a single, reproducible workflow.
- Apply text vectorization (TF-IDF) to the
-
Model Evaluation:
Each model was evaluated using metrics such as accuracy and F1-score on a held-out test set. The best-performing pipeline was selected for deployment. -
Serialization:
The final pipeline (including all preprocessing steps and the trained model) was saved asmodel/pipeline.pk1using Python'spicklemodule. This allows for easy reuse and deployment without retraining.
-
API Deployment:
The trained pipeline is deployed using a FastAPI application (app.py). This enables real-time sentiment prediction via a simple web API. -
Endpoints:
POST /predict:
Accepts a JSON payload with a comment and returns the predicted sentiment.
Example request:Example response:{ "text": "I absolutely loved this video, it was very helpful and well explained!" }{ "prediction": "Positive" }GET /health:
Returns a simple status message to verify that the API is running.
-
Input Handling:
The API automatically computes required features (such asword_count) from the input text, ensuring compatibility with the trained pipeline. -
Error Handling:
The API is designed to handle invalid input gracefully and will return informative error messages if the input format is incorrect.
-
Install FastAPI and Uvicorn (if not already installed):
pip install fastapi uvicorn
-
Start the API server:
uvicorn app:app --reload
-
Test the API:
- Open your browser and go to http://localhost:8000/docs for an interactive Swagger UI.
- Or use
curl:curl -X POST "http://localhost:8000/predict" -H "Content-Type: application/json" -d "{\"text\": \"This is a great video!\"}"
-
Example Python Request:
import requests response = requests.post( "http://localhost:8000/predict", json={"text": "This is a great video!"} ) print(response.json())
- All trained models and pipelines are stored in the
model/directory for reproducibility and deployment. - The main file for inference is
model/pipeline.pk1, which contains both preprocessing and the trained model.
Note:
-
Ensure the pipeline file (
model/pipeline.pk1) exists and matches the features expected by the code. -
The API will not work if the pipeline file is missing or if the input features do not match those used
- Fork the repository
- Create a feature branch (
git checkout -b feature/new-analysis) - Commit your changes (
git commit -am 'Add new analysis feature') - Push to the branch (
git push origin feature/new-analysis) - Create a Pull Request
- Implement advanced NLP techniques (TF-IDF, Word2Vec)
- Create interactive dashboards
This project is licensed under the MIT License - see the LICENSE file for details.
For questions or suggestions, please open an issue in the repository.
Note: Make sure to have the required datasets in the data/ folder before running the analysis notebook.