-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentiment_analysis.py
More file actions
281 lines (230 loc) · 10.9 KB
/
Copy pathsentiment_analysis.py
File metadata and controls
281 lines (230 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# -*- coding: utf-8 -*-
"""Sentiment Analysis.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1Tcy0Mn8Sv42LRVddOs0ufYTKx2ML-uev
"""
#CODTECH Internship - TASK 2
# Enhanced Sentiment Analysis with Comprehensive Visualizations and Metrics
# Dataset: Sample IMDb reviews (loaded via sklearn)
# ===============================================================
# Step 1: Import Libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import fetch_20newsgroups
from sklearn.model_selection import train_test_split, cross_val_score, validation_curve
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import (classification_report, confusion_matrix, accuracy_score,
precision_score, recall_score, f1_score, roc_auc_score,
roc_curve, precision_recall_curve, matthews_corrcoef,
balanced_accuracy_score, log_loss)
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from wordcloud import WordCloud
import warnings
warnings.filterwarnings('ignore')
# Set style for better plots
plt.style.use('seaborn-v0_8-darkgrid')
sns.set_palette("husl")
# Step 2: Load Dataset (Using 'rec.autos' vs 'sci.med' as proxy for sentiment)
categories = ['rec.autos', 'sci.med']
data = fetch_20newsgroups(subset='all', categories=categories, remove=('headers', 'footers', 'quotes'))
df = pd.DataFrame({'text': data.data, 'target': data.target})
df['target'] = df['target'].map({0: "Negative", 1: "Positive"})
print("✅ Sample data:")
print(df.head())
print(f"\n📊 Dataset shape: {df.shape}")
print(f"📊 Target distribution:\n{df['target'].value_counts()}")
# Step 3: Data Exploration and Visualization
fig, axes = plt.subplots(2, 2, figsize=(15, 12))
# Dataset distribution
df['target'].value_counts().plot(kind='bar', ax=axes[0,0], color=['#FF6B6B', '#4ECDC4'])
axes[0,0].set_title('Target Distribution', fontsize=14, fontweight='bold')
axes[0,0].set_xlabel('Sentiment')
axes[0,0].set_ylabel('Count')
axes[0,0].tick_params(axis='x', rotation=0)
# Text length distribution
df['text_length'] = df['text'].str.len()
df.boxplot(column='text_length', by='target', ax=axes[0,1])
axes[0,1].set_title('Text Length Distribution by Sentiment', fontsize=14, fontweight='bold')
axes[0,1].set_xlabel('Sentiment')
axes[0,1].set_ylabel('Text Length')
# Word count distribution
df['word_count'] = df['text'].str.split().str.len()
df.boxplot(column='word_count', by='target', ax=axes[1,0])
axes[1,0].set_title('Word Count Distribution by Sentiment', fontsize=14, fontweight='bold')
axes[1,0].set_xlabel('Sentiment')
axes[1,0].set_ylabel('Word Count')
# Histogram of text lengths
axes[1,1].hist(df[df['target']=='Negative']['text_length'], alpha=0.7, label='Negative', bins=30)
axes[1,1].hist(df[df['target']=='Positive']['text_length'], alpha=0.7, label='Positive', bins=30)
axes[1,1].set_title('Text Length Histogram', fontsize=14, fontweight='bold')
axes[1,1].set_xlabel('Text Length')
axes[1,1].set_ylabel('Frequency')
axes[1,1].legend()
plt.tight_layout()
plt.show()
# Step 4: Word Clouds
fig, axes = plt.subplots(1, 2, figsize=(16, 6))
# Negative sentiment word cloud
negative_text = ' '.join(df[df['target'] == 'Negative']['text'])
wordcloud_neg = WordCloud(width=800, height=400, background_color='white',
colormap='Reds').generate(negative_text)
axes[0].imshow(wordcloud_neg, interpolation='bilinear')
axes[0].set_title('Most Common Words - Negative Sentiment', fontsize=14, fontweight='bold')
axes[0].axis('off')
# Positive sentiment word cloud
positive_text = ' '.join(df[df['target'] == 'Positive']['text'])
wordcloud_pos = WordCloud(width=800, height=400, background_color='white',
colormap='Blues').generate(positive_text)
axes[1].imshow(wordcloud_pos, interpolation='bilinear')
axes[1].set_title('Most Common Words - Positive Sentiment', fontsize=14, fontweight='bold')
axes[1].axis('off')
plt.tight_layout()
plt.show()
# Step 5: Preprocessing
df.dropna(inplace=True)
X = df['text']
y = df['target']
# Step 6: TF-IDF Vectorization
tfidf = TfidfVectorizer(stop_words='english', max_df=0.7, min_df=2, max_features=5000)
X_tfidf = tfidf.fit_transform(X)
print(f"\n🔧 TF-IDF Matrix shape: {X_tfidf.shape}")
# Step 7: Train-Test Split
X_train, X_test, y_train, y_test = train_test_split(X_tfidf, y, test_size=0.2,
random_state=42, stratify=y)
# Step 8: Model Training and Comparison
models = {
'Logistic Regression': LogisticRegression(random_state=42, max_iter=1000),
'Naive Bayes': MultinomialNB(),
'SVM': SVC(probability=True, random_state=42),
'Random Forest': RandomForestClassifier(n_estimators=100, random_state=42)
}
model_results = {}
# Train and evaluate each model
for name, model in models.items():
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
y_pred_proba = model.predict_proba(X_test)[:, 1] if hasattr(model, 'predict_proba') else None
# Calculate comprehensive metrics
metrics = {
'Accuracy': accuracy_score(y_test, y_pred),
'Balanced Accuracy': balanced_accuracy_score(y_test, y_pred),
'Precision': precision_score(y_test, y_pred, pos_label='Positive'),
'Recall': recall_score(y_test, y_pred, pos_label='Positive'),
'F1-Score': f1_score(y_test, y_pred, pos_label='Positive'),
'Matthews Correlation': matthews_corrcoef(y_test, y_pred),
'AUC-ROC': roc_auc_score(y_test, y_pred_proba) if y_pred_proba is not None else 'N/A',
'Log Loss': log_loss(y_test, y_pred_proba) if y_pred_proba is not None else 'N/A'
}
model_results[name] = {
'model': model,
'metrics': metrics,
'predictions': y_pred,
'probabilities': y_pred_proba
}
# Step 9: Results Visualization
# Model comparison
metrics_df = pd.DataFrame({name: result['metrics'] for name, result in model_results.items()}).T
# Filter numeric metrics for plotting
numeric_metrics = ['Accuracy', 'Balanced Accuracy', 'Precision', 'Recall', 'F1-Score', 'Matthews Correlation']
numeric_df = metrics_df[numeric_metrics].astype(float)
fig, axes = plt.subplots(2, 2, figsize=(16, 12))
# Bar plot of metrics
numeric_df.plot(kind='bar', ax=axes[0,0], width=0.8)
axes[0,0].set_title('Model Performance Comparison', fontsize=14, fontweight='bold')
axes[0,0].set_xlabel('Models')
axes[0,0].set_ylabel('Score')
axes[0,0].legend(bbox_to_anchor=(1.05, 1), loc='upper left')
axes[0,0].tick_params(axis='x', rotation=45)
# Heatmap of metrics
sns.heatmap(numeric_df, annot=True, cmap='YlOrRd', ax=axes[0,1], fmt='.3f')
axes[0,1].set_title('Model Performance Heatmap', fontsize=14, fontweight='bold')
# Best model detailed analysis (Logistic Regression)
best_model = model_results['Logistic Regression']['model']
best_predictions = model_results['Logistic Regression']['predictions']
best_probabilities = model_results['Logistic Regression']['probabilities']
# Confusion Matrix
cm = confusion_matrix(y_test, best_predictions)
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', ax=axes[1,0],
xticklabels=['Negative', 'Positive'], yticklabels=['Negative', 'Positive'])
axes[1,0].set_title('Confusion Matrix - Logistic Regression', fontsize=14, fontweight='bold')
axes[1,0].set_xlabel('Predicted')
axes[1,0].set_ylabel('Actual')
# ROC Curve
fpr, tpr, _ = roc_curve(y_test, best_probabilities, pos_label='Positive')
auc_score = roc_auc_score(y_test, best_probabilities)
axes[1,1].plot(fpr, tpr, linewidth=2, label=f'ROC Curve (AUC = {auc_score:.3f})')
axes[1,1].plot([0, 1], [0, 1], 'k--', linewidth=1)
axes[1,1].set_xlabel('False Positive Rate')
axes[1,1].set_ylabel('True Positive Rate')
axes[1,1].set_title('ROC Curve - Logistic Regression', fontsize=14, fontweight='bold')
axes[1,1].legend()
axes[1,1].grid(True)
plt.tight_layout()
plt.show()
# Step 10: Additional Visualizations
fig, axes = plt.subplots(2, 2, figsize=(16, 12))
# Precision-Recall Curve
precision, recall, _ = precision_recall_curve(y_test, best_probabilities, pos_label='Positive')
axes[0,0].plot(recall, precision, linewidth=2, color='purple')
axes[0,0].set_xlabel('Recall')
axes[0,0].set_ylabel('Precision')
axes[0,0].set_title('Precision-Recall Curve', fontsize=14, fontweight='bold')
axes[0,0].grid(True)
# Prediction Probability Distribution
axes[0,1].hist(best_probabilities[y_test == 'Negative'], alpha=0.7, label='Negative', bins=30)
axes[0,1].hist(best_probabilities[y_test == 'Positive'], alpha=0.7, label='Positive', bins=30)
axes[0,1].set_xlabel('Prediction Probability')
axes[0,1].set_ylabel('Frequency')
axes[0,1].set_title('Distribution of Prediction Probabilities', fontsize=14, fontweight='bold')
axes[0,1].legend()
# Feature Importance (Top TF-IDF features)
feature_names = tfidf.get_feature_names_out()
coef = best_model.coef_[0]
top_positive_indices = np.argsort(coef)[-10:]
top_negative_indices = np.argsort(coef)[:10]
top_features = np.concatenate([coef[top_negative_indices], coef[top_positive_indices]])
feature_labels = [feature_names[i] for i in np.concatenate([top_negative_indices, top_positive_indices])]
colors = ['red'] * 10 + ['blue'] * 10
axes[1,0].barh(range(len(top_features)), top_features, color=colors, alpha=0.7)
axes[1,0].set_yticks(range(len(feature_labels)))
axes[1,0].set_yticklabels(feature_labels)
axes[1,0].set_xlabel('Coefficient Value')
axes[1,0].set_title('Top 20 Most Important Features', fontsize=14, fontweight='bold')
# Cross-validation scores
cv_scores = cross_val_score(best_model, X_tfidf, y, cv=5, scoring='accuracy')
axes[1,1].boxplot(cv_scores)
axes[1,1].set_ylabel('Accuracy Score')
axes[1,1].set_title(f'5-Fold Cross-Validation\nMean: {cv_scores.mean():.3f} (±{cv_scores.std():.3f})',
fontsize=14, fontweight='bold')
axes[1,1].grid(True)
plt.tight_layout()
plt.show()
# Step 11: Comprehensive Results Summary
print("\n" + "="*80)
print("🎯 COMPREHENSIVE RESULTS SUMMARY")
print("="*80)
for name, result in model_results.items():
print(f"\n📊 {name}:")
print("-" * 50)
for metric, value in result['metrics'].items():
if isinstance(value, float):
print(f"{metric:20}: {value:.4f}")
else:
print(f"{metric:20}: {value}")
print(f"\n🔄 Cross-Validation Results (Logistic Regression):")
print(f"CV Scores: {cv_scores}")
print(f"Mean CV Score: {cv_scores.mean():.4f} (±{cv_scores.std():.4f})")
print(f"\n📈 Best Model: Logistic Regression")
print(f"Test Accuracy: {model_results['Logistic Regression']['metrics']['Accuracy']:.4f}")
print(f"AUC-ROC Score: {model_results['Logistic Regression']['metrics']['AUC-ROC']:.4f}")
# Classification report for best model
print(f"\n📋 Detailed Classification Report (Logistic Regression):")
print(classification_report(y_test, best_predictions))
print("\n✅ Analysis Complete!")
print("="*80)