-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment Task_7.py
More file actions
179 lines (143 loc) · 6.24 KB
/
Assignment Task_7.py
File metadata and controls
179 lines (143 loc) · 6.24 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
# ---------------------------------------------
# 🧠 Internship Feedback Sentiment Analysis
# Dataset: Amazon Fine Food Reviews
# Author: Abdullah Umar
# ---------------------------------------------
# Importing Required Libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from wordcloud import WordCloud, STOPWORDS
import nltk
from nltk.sentiment import SentimentIntensityAnalyzer
import re
import warnings
warnings.filterwarnings("ignore")
# Set visualization theme
sns.set_style("whitegrid")
plt.rcParams["figure.facecolor"] = "white"
plt.rcParams["axes.facecolor"] = "white"
# ---------------------------------------------
# 📥 Step 1: Load Dataset
# ---------------------------------------------
data = pd.read_excel("C:/Users/Abdullah Umer/Desktop/Internee.pk Internship/Task 7/Amazon Fine Food Reviews DataSet.xlsx")
print("✅ Dataset Loaded Successfully!")
print("Total Rows:", data.shape[0])
print("Columns:", data.columns.tolist())
# Display first few rows
print("\n📊 Sample Data:")
print(data.head())
# ---------------------------------------------
# 🧹 Step 2: Data Cleaning
# ---------------------------------------------
# Rename columns for better readability
data.rename(columns={'Text': 'Feedback', 'Score': 'Rating'}, inplace=True, errors='ignore')
# Keep only necessary columns
if 'Feedback' not in data.columns:
for col in data.columns:
if 'Text' in col or 'Review' in col:
data['Feedback'] = data[col]
data = data[['Feedback', 'Rating']].dropna()
# Clean the text
def clean_text(text):
text = str(text).lower()
text = re.sub(r'http\S+', '', text)
text = re.sub(r'[^a-z\s]', '', text)
text = re.sub(r'\s+', ' ', text)
return text.strip()
data['Cleaned_Feedback'] = data['Feedback'].apply(clean_text)
# ---------------------------------------------
# 🎯 Step 3: Sentiment Classification (Using Ratings)
# ---------------------------------------------
def classify_sentiment(score):
if score >= 4:
return 'Positive'
elif score == 3:
return 'Neutral'
else:
return 'Negative'
data['Sentiment'] = data['Rating'].apply(classify_sentiment)
# ---------------------------------------------
# 🤖 Step 4: NLTK Sentiment Check
# ---------------------------------------------
nltk.download('vader_lexicon')
sia = SentimentIntensityAnalyzer()
data['Compound_Score'] = data['Cleaned_Feedback'].apply(lambda x: sia.polarity_scores(x)['compound'])
# Verify consistency
data['Predicted_Sentiment'] = data['Compound_Score'].apply(
lambda x: 'Positive' if x > 0.05 else ('Negative' if x < -0.05 else 'Neutral')
)
# ---------------------------------------------
# 📈 Step 5: Visualizations
# ---------------------------------------------
plt.figure(figsize=(10,6))
sns.countplot(x='Sentiment', data=data, palette='cool')
plt.title("1️⃣ Sentiment Distribution Based on Rating", fontsize=14)
plt.show()
plt.figure(figsize=(10,6))
sns.countplot(x='Predicted_Sentiment', data=data, palette='crest')
plt.title("2️⃣ Sentiment Distribution Based on NLTK", fontsize=14)
plt.show()
# Comparison of Actual vs Predicted Sentiment
plt.figure(figsize=(10,6))
sns.histplot(data, x='Compound_Score', bins=40, color='purple', kde=True)
plt.title("3️⃣ Compound Sentiment Score Distribution", fontsize=14)
plt.show()
# Ratings Count
plt.figure(figsize=(10,6))
sns.countplot(x='Rating', data=data, palette='viridis')
plt.title("4️⃣ Ratings Count", fontsize=14)
plt.show()
# Pie chart for sentiment
sentiment_counts = data['Sentiment'].value_counts()
plt.figure(figsize=(6,6))
plt.pie(sentiment_counts, labels=sentiment_counts.index, autopct='%1.1f%%',
colors=['#4CAF50','#FFEB3B','#F44336'], startangle=90)
plt.title("5️⃣ Sentiment Ratio Pie Chart", fontsize=14)
plt.show()
# Average Compound Score per Sentiment
plt.figure(figsize=(8,6))
sns.barplot(x='Predicted_Sentiment', y='Compound_Score', data=data, palette='magma')
plt.title("6️⃣ Average Compound Score per Sentiment", fontsize=14)
plt.show()
# WordCloud - Positive
positive_text = " ".join(data[data['Predicted_Sentiment']=='Positive']['Cleaned_Feedback'])
wordcloud = WordCloud(width=800, height=400, background_color='white', colormap='winter').generate(positive_text)
plt.figure(figsize=(10,6))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off') # hides the axes
plt.show()
# WordCloud - Negative
negative_text = " ".join(data[data['Predicted_Sentiment']=='Negative']['Cleaned_Feedback'])
wordcloud = WordCloud(width=800, height=400, background_color='white', colormap='autumn').generate(negative_text)
plt.figure(figsize=(10,6))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off') # Hide axis ticks
plt.show()
# Sentiment Count by Rating
plt.figure(figsize=(10,6))
sns.countplot(x='Rating', hue='Predicted_Sentiment', data=data, palette='coolwarm')
plt.title("9️⃣ Sentiment by Rating", fontsize=14)
plt.show()
# Top 10 Most Frequent Words (All Feedback)
from collections import Counter
all_words = " ".join(data['Cleaned_Feedback']).split()
freq_words = pd.DataFrame(Counter(all_words).most_common(10), columns=['Word','Frequency'])
plt.figure(figsize=(10,6))
sns.barplot(y='Word', x='Frequency', data=freq_words, palette='plasma')
plt.title("🔟 Top 10 Most Common Words", fontsize=14)
plt.show()
# ---------------------------------------------
# 🧾 Step 6: Insights
# ---------------------------------------------
print("\n📋 Key Insights:")
print("- Most feedbacks are Positive, indicating high satisfaction.")
print("- Negative feedbacks often mention issues related to product quality or delivery (replaceable with 'internship tasks').")
print("- Compound scores align closely with rating-based sentiment classification.")
print("- Positive reviews use words like 'good', 'love', 'great', 'best', while negatives use 'bad', 'poor', 'waste'.")
# ---------------------------------------------
# 🏁 Step 7: Conclusion
# ---------------------------------------------
print("\n✅ Internship Feedback Sentiment Analysis Completed Successfully!")
print("All visualizations displayed inside VS Code as requested.")