-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4.2_Sentimental_Data_User_Reviews.py
More file actions
96 lines (82 loc) · 2.99 KB
/
4.2_Sentimental_Data_User_Reviews.py
File metadata and controls
96 lines (82 loc) · 2.99 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
"""
Sentiment Analysis on User Reviews (Google Play Store Translated Reviews)
"""
import pandas as pd
import re
import os
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, accuracy_score, confusion_matrix
# ------------------------------
# 1. Load dataset
# ------------------------------
df = pd.read_csv("data/user_reviews.csv") # adjust path if needed
print("✅ Data loaded successfully!")
print("Columns in dataset:", df.columns.tolist())
print(df.head())
# ------------------------------
# 2. Use correct columns
# ------------------------------
df = df[["Translated_Review", "Sentiment"]].dropna()
df = df.rename(columns={"Translated_Review": "text", "Sentiment": "label"})
# Map sentiment to numeric
sentiment_map = {"Negative": -1.0, "Neutral": 0.0, "Positive": 1.0}
df["label"] = df["label"].map(sentiment_map)
# ------------------------------
# 3. Clean Text
# ------------------------------
def clean_text(s):
s = str(s).lower()
s = re.sub(r"http\S+", "", s) # remove links
s = re.sub(r"[^a-z0-9\s]", "", s) # remove punctuation
return s
df["text"] = df["text"].apply(clean_text)
# ------------------------------
# 4. Train/Test Split
# ------------------------------
X = df["text"]
y = df["label"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# ------------------------------
# 5. TF-IDF + Model
# ------------------------------
vect = TfidfVectorizer(max_features=5000)
X_train_tf = vect.fit_transform(X_train)
X_test_tf = vect.transform(X_test)
model = LogisticRegression(max_iter=1000)
model.fit(X_train_tf, y_train)
preds = model.predict(X_test_tf)
# ------------------------------
# 6. Evaluation
# ------------------------------
print("\n✅ Model Evaluation")
print("Accuracy:", accuracy_score(y_test, preds))
print("\nClassification Report:\n", classification_report(y_test, preds))
# ------------------------------
# 7. Visualizations
# ------------------------------
os.makedirs("outputs", exist_ok=True)
# Distribution
plt.figure(figsize=(6,4))
df["label"].value_counts().sort_index().plot(kind="bar", color=["red","gray","green"])
plt.title("Sentiment Distribution (-1=Negative, 0=Neutral, 1=Positive)")
plt.xlabel("Sentiment")
plt.ylabel("Count")
plt.savefig("outputs/user_reviews_distribution.png", dpi=300)
plt.show()
# Confusion Matrix
cm = confusion_matrix(y_test, preds)
plt.figure(figsize=(6,5))
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues",
xticklabels=["Negative","Neutral","Positive"],
yticklabels=["Negative","Neutral","Positive"])
plt.title("Confusion Matrix - User Reviews")
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.savefig("outputs/user_reviews_confusion_matrix.png", dpi=300)
plt.show()