-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamazon_sentiment_analysis.py
More file actions
46 lines (32 loc) · 1 KB
/
Copy pathamazon_sentiment_analysis.py
File metadata and controls
46 lines (32 loc) · 1 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
import pandas as pd
from textblob import TextBlob
import matplotlib.pyplot as plt
# Load first 100 reviews
df = pd.read_csv("Amazon_Reviews.csv", engine="python")
df = df.head(100)
def get_sentiment(text):
polarity = TextBlob(str(text)).sentiment.polarity
if polarity > 0:
return "Positive"
elif polarity < 0:
return "Negative"
else:
return "Neutral"
df["Sentiment"] = df["Review Text"].apply(get_sentiment)
print(df[["Rating", "Review Title", "Sentiment"]].head(10))
print("\nSentiment Summary:")
print(df["Sentiment"].value_counts())
# Rating vs Sentiment
print("\nRating vs Sentiment")
rating_sentiment = pd.crosstab(
df["Rating"],
df["Sentiment"]
)
print(rating_sentiment)
rating_sentiment.plot(kind="bar", figsize=(10,5))
plt.title("Rating vs Sentiment")
plt.xlabel("Rating")
plt.ylabel("Number of Reviews")
plt.show()
df.to_csv("amazon_sentiment_results.csv", index=False)
print("Results saved successfully!")