-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdataset_analysis.py
210 lines (167 loc) · 6.71 KB
/
dataset_analysis.py
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
import os
from collections import Counter
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from wordcloud import WordCloud
java_path = "C:/Program Files/Java/jdk1.8.0_161/bin/java.exe"
os.environ['JAVAHOME'] = java_path
def exploratory_data_analysis(df):
print("exploratory_data_analysis")
print("Number of reviews:", len(df))
# Score Distribution
ax = plt.axes()
sns.countplot(df.score, ax=ax)
ax.set_title('Score Distribution')
plt.show()
print("Average Score: ", np.mean(df.score))
print("Median Score: ", np.median(df.score))
# creo un nuovo attributo opinion
# negative = review(1-3), positive=(4-5)
df.ix[df.score == 3, 'opinion'] = "neutral"
df.ix[df.score > 3, 'opinion'] = "positive"
df.ix[df.score < 3, 'opinion'] = "negative"
## opinion distribution
ax = plt.axes()
sns.countplot(df.opinion, ax=ax)
ax.set_title('Sentiment Positive vs Negative Distribution')
plt.show()
## userid distribution
# ax = plt.axes()
# sns.countplot(df.userid, ax=ax)
# ax.set_title('UserId Distribution')
# plt.show()
#
# ## productid distribution
# ax = plt.axes()
# sns.countplot(df.userid, ax=ax)
# ax.set_title('ProductId Distribution')
# plt.show()
print("Proportion of positive review:", len(df[df.opinion == "positive"]) / len(df))
print("Proportion of neutral review:", len(df[df.opinion == "neutral"]) / len(df))
print("Proportion of negative review:", len(df[df.opinion == "negative"]) / len(df))
# 77% of the fine food reviews are considered as positive
# and 23% of them are considered as negative.
reviews = df.text.values
labels = df.opinion.values
### TEXT REVIEWS ###
if df.opinion[1] == "positive":
print("positive" + "\t" + reviews[1][:90] + "...")
else:
print("negative" + "\t " + reviews[1][:90] + "...")
### Exploratory Visualization ###
# mi creo un vocabolario di parole positive e negative
positive_reviews = [reviews[i] for i in range(len(reviews)) if labels[i] == "positive"]
negative_reviews = [reviews[i] for i in range(len(reviews)) if labels[i] == "negative"]
### positive
cnt_positve = Counter()
for row in positive_reviews:
cnt_positve.update(row.split(" "))
print("Vocabulary size for positve reviews:", len(cnt_positve.keys()))
### negative
cnt_negative = Counter()
for row in negative_reviews:
cnt_negative.update(row.split(" "))
print("Vocabulary size for positve reviews:", len(cnt_negative.keys()))
cnt_total = Counter()
for row in reviews:
cnt_total.update(row.split(" "))
pos_neg_ratio = Counter()
vocab_pos_neg = (set(cnt_positve.keys())).intersection(set(cnt_negative.keys()))
for word in vocab_pos_neg:
if cnt_total[word] > 100:
ratio = cnt_positve[word] / float(cnt_negative[word] + 1)
if ratio > 1:
pos_neg_ratio[word] = np.log(ratio)
else:
pos_neg_ratio[word] = -np.log(1 / (ratio + 0.01))
positive_dict = {}
for word, cnt in pos_neg_ratio.items():
if (cnt > 1):
positive_dict[word] = cnt
#### positive WORDCLOUD #####
# wordcloud = WordCloud()
# wordcloud.generate_from_frequencies(frequencies=positive_dict)
#
# plt.figure(figsize=(10, 10))
# plt.imshow(wordcloud, interpolation="bilinear")
# plt.axis("off")
# ax = plt.axes()
# ax.set_title('Word Cloud with the Highest Positive/Negative Ratio')
# plt.show()
negative_dict = {}
for word, cnt in pos_neg_ratio.items():
if (cnt < 1) & (cnt > 0):
negative_dict[word] = -np.log(cnt)
#### negative WORDCLOUD #####
wordcloud = WordCloud(background_color='black',
width=2500,
height=2000)
wordcloud.generate_from_frequencies(frequencies=negative_dict)
plt.figure(figsize=(10, 10))
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
ax = plt.axes()
ax.set_title('Word Cloud with the Lowest Positive/Negative Ratio')
plt.show()
print("END EXPLORATORY ANALYSIS")
# vocabulary reduction function to reduce
# the vocabulary based on min frequency or polarity.
def vocabulary_reduction(reviews, labels, min_freq=10, polarity_cut_off=0.1):
pos_count = Counter()
neg_count = Counter()
tot_count = Counter()
for i in range(len(reviews)):
for word in reviews[i].split():
tot_count[word] += 1
if labels[i] == 1:
pos_count[word] += 1
else:
neg_count[word] += 1
# Identify words with frequency greater than min_freq
vocab_freq = []
for word in tot_count.keys():
if tot_count[word] > min_freq:
vocab_freq.append(word)
# Use polarity to reduce vocab
pos_neg_ratio = Counter()
vocab_pos_neg = (set(pos_count.keys())).intersection(set(neg_count.keys()))
for word in vocab_pos_neg:
if tot_count[word] > 100:
ratio = pos_count[word] / float(neg_count[word] + 1)
if ratio > 1:
pos_neg_ratio[word] = np.log(ratio)
else:
pos_neg_ratio[word] = -np.log(1 / (ratio + 0.01))
mean_ratio = np.mean(list(pos_neg_ratio.values()))
vocab_polarity = []
for word in pos_neg_ratio.keys():
if (pos_neg_ratio[word] < (mean_ratio - polarity_cut_off)) or (
pos_neg_ratio[word] > (mean_ratio + polarity_cut_off)):
vocab_polarity.append(word)
vocab_rm_polarity = set(pos_neg_ratio.keys()).difference(vocab_polarity)
vocab_reduced = (set(vocab_freq)).difference(set(vocab_rm_polarity))
reviews_cleaned = []
for review in reviews:
review_temp = [word for word in review.split() if word in vocab_reduced]
reviews_cleaned.append(' '.join(review_temp))
return reviews_cleaned
if __name__ == "__main__":
df = pd.read_csv("Dataset/food.tsv", sep="\t", encoding='latin-1')
# recensioni negative
df = df.loc[df['score'] == (1 or 2)]
df = df.groupby('productid').agg(['mean', 'count'])
print("ciao")
asd = df.groupby('productid').score.mean().reset_index()
asd2 = df.productid.value_counts()
asd3 = asd.merge(asd2.to_frame(), left_on='productid', right_index=True)
asd4 = asd3.sort_values('score')
df1 = df.loc[df['productid'] == "B001E96JY2"]
# df = pd.read_csv("cleanedTextCSV.csv", sep="\t", encoding='latin-1')
exploratory_data_analysis(df)
# data_preprocessing(df)
# df_train = df = df.loc[df['productid'] == "B000DZFMEQ"]
# df_test = df[201:250]
# # aspect_based(df_train, df_test)
# aspect2(df_train, df_test)