forked from Aravindh0112/Restaurant_Rating_Prediction_YELPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestaurant_Rating_Prediction_YELPS.py
367 lines (263 loc) · 10.1 KB
/
Restaurant_Rating_Prediction_YELPS.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# -*- coding: utf-8 -*-
"""STAT_ML_Restaurant_Rating.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/18DqI46EqWnFQtBJyeuEobZiWrORoOyfv
"""
import pandas as pd
!pip install -q kaggle
!mkdir ~/.kaggle
!cp kaggle.json ~/.kaggle/
!chmod 600 ~/.kaggle/kaggle.json
!kaggle datasets list
!kaggle datasets download -d yelp-dataset/yelp-dataset
!ls
!unzip \*.zip && rm *.zip
from google.colab import drive
drive.mount('/content/gdrive')
# Commented out IPython magic to ensure Python compatibility.
import os
os.environ['KAGGLE_CONFIG_DIR'] = '/content/gdrive/MyDrive/kaggle'
# %cd /content/gdrive/MyDrive/kaggle
from time import time
import json
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
def load_rows(file_path, nrows=None, only_return_count=False, verbose=True):
"""
Returns dataframe from json file
"""
tic = time()
with open(file_path) as json_file:
count = 0
objs = []
line = json_file.readline()
while (nrows is None or count<nrows) and line:
count += 1
if not only_return_count:
obj = json.loads(line)
objs.append(obj)
line = json_file.readline()
toc = time()
if verbose:
print(file_path.split('/')[-1], 'loaded. Count =', count, ', Time =', round(toc-tic,2), 'secs.')
if only_return_count:
return count
return pd.DataFrame(objs)
#data generator to load data in chunks
def load_rows_gen(file_path, nrows=1e6, verbose=True):
"""
Returns data in chunks
"""
with open(file_path) as json_file:
line = json_file.readline()
total = 0
while line:
count = 0
objs = []
tic = time()
while count<nrows and line:
count+=1
obj = json.loads(line)
objs.append(obj)
line = json_file.readline()
total += count
toc = time()
print('Loaded chunk of size:', count, ", Time =", round(toc-tic,2), 'secs.')
yield pd.DataFrame(objs)
"""***LOADING THE DATASET***"""
filename = 'yelp_academic_dataset_business.json'
biz_data = load_rows(filename, 50000)
biz_data.shape
biz_data.head()
biz_data.groupby('state')['business_id'].count()
filename = 'yelp_academic_dataset_review.json'
rev_data = load_rows(filename, 50000)
rev_data.shape
rev_data.head(10)
a = biz_data[biz_data['categories'].str.contains('Restaurant') == True]
rev = rev_data[rev_data.business_id.isin(a['business_id']) == True]
len(rev)
rev.head(10)
rev.shape
"""***VISUALISATION OF THE DATA***"""
sns.set(rc={'figure.figsize':(11.7,8.27)})
sns.countplot(x='City',data=dfs)
labels = rev['stars'].value_counts().index
sizes = rev['stars'].value_counts().values
fig1, ax1 = plt.subplots()
ax1.pie(sizes, labels=labels, autopct='%1.1f%%', shadow=True)
ax1.axis('equal')
plt.show()
from wordcloud import WordCloud
text = rev['text']
plt.subplots(figsize = (10,10))
wordcloud = WordCloud(background_color='black', width=1024, height=768).generate(' '.join(text))
plt.imshow(wordcloud)
plt.axis('off')
plt.show()
data = rev[['text','stars']]
data.shape
data.head(10)
data['stars'].value_counts()
"""***ADDING LABELS TO THE DATAFRAME***"""
labels = []
for star in data['stars']:
if star==1.0 or star==2.0:
labels.append(-1)
elif star==4.0 or star==5.0:
labels.append(1)
else:
labels.append(0)
data['new_stars'] = labels
data.head(10)
data = data.drop('stars', 1)
data.columns = ['text', 'stars']
data.head(20)
sample_size = 6000 # Above 20% of the original size
sample = data.sample(sample_size)
sample = sample.reset_index(drop=True)
plot_stars_frequencies(sample)
"""***PREPROCESSING WORK***"""
def remove_punctuation(text):
'''a function for removing punctuation'''
import string
translator = str.maketrans('', '', string.punctuation)
return text.translate(translator)
sample['text'] = sample['text'].apply(remove_punctuation)
import nltk
nltk.download('stopwords')
def remove_stopwords(text):
from nltk.corpus import stopwords
'''a function for removing the stopword'''
sw = stopwords.words('english')
# removing the stop words and lowercasing the selected words
text = [word.lower() for word in text.split() if word.lower() not in sw]
# joining the list of words with space separator
return " ".join(text)
sample['text'] = sample['text'].apply(remove_stopwords)
def stemming(text):
'''a function which stems each word in the given text'''
from nltk.stem.snowball import SnowballStemmer
stemmer = SnowballStemmer("english")
text = [stemmer.stem(word) for word in text.split()]
return " ".join(text)
sample['text'] = sample['text'].apply(stemming)
from sklearn.feature_extraction.text import TfidfVectorizer
# create a count vectorizer object
vectorizer = TfidfVectorizer(dtype=np.float32)
# fit the count vectorizer using the text data
tfidf_matrix = vectorizer.fit_transform(sample['text'].values)
print(len(vectorizer.get_feature_names()))
def get_word_freq(tfidf, vectorizer):
return sorted([(tfidf.getcol(idx).sum(), word)
for word, idx in vectorizer.vocabulary_.items()],
reverse=True)
words_tfidf_scores = get_word_freq(tfidf_matrix, vectorizer)
# transform tfidf scores matrix into pandas dataframe
words_tfidf_scores_df = pd.DataFrame(words_tfidf_scores, dtype=np.float32)
words_tfidf_scores_df.columns = ['tfidf_score', 'token']
words_tfidf_scores_df = words_tfidf_scores_df.set_index('token')
words_tfidf_scores_df.head()
words_tfidf_scores_df.info()
words_tfidf_scores_df.shape
"""***HISTOGRAM OF THE 100 HIGHEST WORDS***"""
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (20,3)
plt.rcParams['figure.figsize'] = (24,20)
plt.rcParams['font.size'] = 22
ax = words_tfidf_scores_df.head(50).plot(kind='barh')
ax.invert_yaxis()
"""***HISTOGRAM OF THE 100 LOWEST WORDS***"""
plt.rcParams['figure.figsize'] = (24,20)
plt.rcParams['font.size'] = 22
ax = words_tfidf_scores_df.tail(50).plot(kind='barh')
ax.invert_yaxis()
def getKey(dict, value):
'''get dictionnary key from its value'''
return [key for key in dict.keys() if (dict[key] == value)][0]
top_features_size = 1001
top_features_names = words_tfidf_scores_df.index.values[:top_features_size] # Extracting the top 20% features' names
# And their corresponding indices from the 'vectorizer.vocabulary_' dictionnary
top_features_indices = [
vectorizer.vocabulary_.get(top_features_names[i]) for i in range(len(top_features_names))
]
# Creating the final dataframe from the tfidf_matrix with respect to the top features
final_df = pd.DataFrame()
for top_feature_index in top_features_indices:
final_df[getKey(vectorizer.vocabulary_, top_feature_index)] = pd.Series(tfidf_matrix.getcol(top_feature_index).toarray().ravel())
final_df['stars'] = sample['stars']
final_df.head()
plt.rcParams["figure.figsize"] = (12,10)
plot_stars_frequencies(final_df)
def pretty_print_cm(cm, labels, normalize=False):
"""pretty print confusion matrices"""
import seaborn as sns
from sklearn.preprocessing import scale
title = 'Confusion matrix'
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
title = ' '.join(['Normalized', title])
conf_mat = pd.DataFrame(cm, columns=labels, index=labels)
conf_mat.index.name = 'True \ Predicted'
plt.figure(figsize=(16,10))
ax = sns.heatmap(conf_mat, annot=True, cmap='Blues', fmt='g')
ax.set_xlabel('Predicted')
ax.set_ylabel('True')
ax.set_title(title)
ax.margins(2,2)
plt.show()
def generate_prediction_results(y_true, y_pred):
"""generate classification report and confusion matrix on a given true and predicted values"""
from sklearn.metrics import classification_report, confusion_matrix
conf_mat = confusion_matrix(y_true, y_pred)
class_report = classification_report(y_true, y_pred, output_dict=True)
return conf_mat, class_report
from sklearn.model_selection import train_test_split
X = final_df.drop(['stars'], 1)
y = final_df['stars']
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2)
pd.DataFrame(y_train.value_counts()).plot(kind='bar')
"""***BALANCING THE DATASET***"""
from imblearn.over_sampling import SMOTE #Synthetic Minority OverSampling
sm = SMOTE()
x_cols = X_train.columns
X_train, y_train = sm.fit_sample(X_train, y_train)
X_train = pd.DataFrame(X_train, columns=x_cols)
y_train = pd.Series(y_train)
pd.DataFrame(y_train.value_counts()).plot(kind='bar')
"""***NAIVE BAYES***"""
from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
clf.fit(X_train, y_train)
NB_predictions_test = clf.predict(X_test)
NB_predictions_train = clf.predict(X_train)
NB_confusion_matrix_test, NB_classification_report_test = generate_prediction_results(y_test, NB_predictions_test)
NB_confusion_matrix_train, NB_classification_report_train = generate_prediction_results(y_train, NB_predictions_train)
labels = [-1, 0, 1]
pretty_print_cm(NB_confusion_matrix_train, labels)
pd.DataFrame(NB_classification_report_train)
labels = [-1, 0, 1]
pretty_print_cm(NB_confusion_matrix_test, labels)
print('Classification report: \n')
pd.DataFrame(NB_classification_report_test)
"""***MULTINOMIAL NAIVE BAYES***"""
from sklearn.naive_bayes import MultinomialNB
clf = MultinomialNB()
clf.fit(X_train, y_train)
MNB_predictions_train = clf.predict(X_train)
MNB_predictions_test = clf.predict(X_test)
MNB_confusion_matrix_train, MNB_classification_report_train = generate_prediction_results(y_train, MNB_predictions_train)
"""***CONFUSION MATRIX FOR TRAIN DATASET***"""
labels = [-1, 0, 1]
pretty_print_cm(MNB_confusion_matrix_train, labels)
pd.DataFrame(MNB_classification_report_train)
MNB_confusion_matrix_test, MNB_classification_report_test = generate_prediction_results(y_test, MNB_predictions_test)
"""***CONFUSION MATRIX ON TEST-SET***"""
# Confusion matrix
labels = [-1, 0, 1]
pretty_print_cm(MNB_confusion_matrix_test, labels)
pd.DataFrame(MNB_classification_report_test)