-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsent_model.py
377 lines (209 loc) · 8 KB
/
sent_model.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
368
369
370
371
372
#!/usr/bin/env python
# coding: utf-8
# In[54]:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import math
import warnings
warnings.filterwarnings('ignore') # Hides warning
warnings.filterwarnings("ignore", category=DeprecationWarning)
warnings.filterwarnings("ignore",category=UserWarning)
sns.set_style("whitegrid") # Plotting style
#get_ipython().run_line_magic('matplotlib', 'inline # Plots show up in notebook')
np.random.seed(7) # seeding random number generator
# In[55]:
df = pd.read_csv("./SIH/1429_1.csv")
df.head()
print(df.shape)
# In[7]:
data = df.copy()
#data.describe()
# In[8]:
#data.info()
# In[10]:
#data["asins"].unique()
# In[11]:
#asins_unique = len(data["asins"].unique())
#print("Number of Unique ASINs: " + str(asins_unique))
# In[12]:
#data.hist(bins=50, figsize=(20,15)) # builds histogram and set the number of bins and fig size (width, height)
#plt.show()
# In[56]:
from sklearn.model_selection import StratifiedShuffleSplit
#print("Before {}".format(len(data)))
dataAfter = data.dropna(subset=["reviews.rating"]) # removes all NAN in reviews.rating
#print("After {}".format(len(dataAfter)))
dataAfter["reviews.rating"] = dataAfter["reviews.rating"].astype(int)
# In[57]:
split = StratifiedShuffleSplit(n_splits=5, test_size=0.2)
for train_index, test_index in split.split(dataAfter, dataAfter["reviews.rating"]):
strat_train = dataAfter.reindex(train_index)
strat_test = dataAfter.reindex(test_index)
# In[58]:
#len(strat_train)
# In[59]:
#strat_train["reviews.rating"].value_counts()/len(strat_train) # value_count() counts all the values based on column
# In[60]:
#len(strat_test)
# In[61]:
#strat_test["reviews.rating"].value_counts()/len(strat_test)
# In[62]:
reviews = strat_train.copy()
#reviews.head(2)
# In[63]:
#reviews.to_csv(r"/home/shamanth/Downloads/start_train.csv", index=False)
# In[64]:
#n=len(reviews["name"].unique()), len(reviews["asins"].unique())
#print(n)
#reviews.info()
# In[23]:
#reviews.groupby("asins")["name"].unique()
# In[65]:
# Lets see all the different names for this product that have 2 ASINs
#different_names = reviews[reviews["asins"] == "B00L9EPT8O,B01E6AO69U"]["name"].unique()
#for name in different_names:
# print(name)
# In[25]:
#reviews[reviews["asins"] == "B00L9EPT8O,B01E6AO69U"]["name"].value_counts()
# In[66]:
#fig = plt.figure(figsize=(16,10))
#ax1 = plt.subplot(211)
#ax2 = plt.subplot(212, sharex = ax1)
#reviews["asins"].value_counts().plot(kind="bar", ax=ax1, title="ASIN Frequency")
#np.log10(reviews["asins"].value_counts()).plot(kind="bar", ax=ax2, title="ASIN Frequency (Log10 Adjusted)")
##plt.show()
# In[67]:
# Entire training dataset average rating
#reviews["reviews.rating"].mean()
# In[68]:
#asins_count_ix = reviews["asins"].value_counts().index
#plt.subplots(2,1,figsize=(16,12))
#plt.subplot(2,1,1)
#reviews["asins"].value_counts().plot(kind="bar", title="ASIN Frequency")
#plt.subplot(2,1,2)
#sns.pointplot(x="asins", y="reviews.rating", order=asins_count_ix, data=reviews)
#plt.xticks(rotation=90)
#plt.show()
# In[69]:
#plt.subplots (2,1,figsize=(16,12))
#plt.subplot(2,1,1)
#reviews["asins"].value_counts().plot(kind="bar", title="ASIN Frequency")
#plt.subplot(2,1,2)
#sns.pointplot(x="asins", y="reviews.doRecommend", order=asins_count_ix, data=reviews)
#plt.xticks(rotation=90)
#plt.show()
# In[70]:
#corr_matrix = reviews.corr()
#corr_matrix
# Here we can analyze reviews.ratings with asins
# In[71]:
#reviews.info()
# # MODEL IMPLEMENTATION
# In[72]:
def sentiments(rating):
if (rating == 5) or (rating == 4):
return "Positive"
elif rating == 3:
return "Neutral"
elif (rating == 2) or (rating == 1):
return "Negative"
# Add sentiments to the data
strat_train["Sentiment"] = strat_train["reviews.rating"].apply(sentiments)
strat_test["Sentiment"] = strat_test["reviews.rating"].apply(sentiments)
#strat_train["Sentiment"][:20]
# In[73]:
X_train = strat_train["reviews.text"]
X_train_targetSentiment = strat_train["Sentiment"]
X_test = strat_test["reviews.text"]
X_test_targetSentiment = strat_test["Sentiment"]
#print(len(X_train), len(X_test))
# In[74]:
# Replace "nan" with space
X_train = X_train.fillna(' ')
X_test = X_test.fillna(' ')
X_train_targetSentiment = X_train_targetSentiment.fillna(' ')
X_test_targetSentiment = X_test_targetSentiment.fillna(' ')
# Text preprocessing and occurance counting
from sklearn.feature_extraction.text import CountVectorizer
count_vect = CountVectorizer()
X_train_counts = count_vect.fit_transform(X_train)
#X_train_counts.shape
# In[75]:
from sklearn.feature_extraction.text import TfidfTransformer
tfidf_transformer = TfidfTransformer(use_idf=False)
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)
X_train_tfidf.shape
# # NAIVE BAYES
# In[76]:
#from sklearn.naive_bayes import MultinomialNB
#from sklearn.pipeline import Pipeline
#clf_multiNB_pipe = Pipeline([("vect", CountVectorizer()), ("tfidf", TfidfTransformer()), ("clf_nominalNB", MultinomialNB())])
#clf_multiNB_pipe.fit(X_train, X_train_targetSentiment)
# In[77]:
#import numpy as np
#predictedMultiNB = clf_multiNB_pipe.predict(X_test)
#np.mean(predictedMultiNB == X_test_targetSentiment)
# # LOGISTIC REGRESSION
# In[78]:
#from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
#clf_logReg_pipe = Pipeline([("vect", CountVectorizer()), ("tfidf", TfidfTransformer()), ("clf_logReg", LogisticRegression())])
#clf_logReg_pipe.fit(X_train, X_train_targetSentiment)
#import numpy as np
#predictedLogReg = clf_logReg_pipe.predict(X_test)
#np.mean(predictedLogReg == X_test_targetSentiment)
# # SUPPORT VECTOR MACHINE
# In[79]:
from sklearn.svm import LinearSVC
clf_linearSVC_pipe = Pipeline([("vect", CountVectorizer()), ("tfidf", TfidfTransformer()), ("clf_linearSVC", LinearSVC())])
clf_linearSVC_pipe.fit(X_train, X_train_targetSentiment)
predictedLinearSVC = clf_linearSVC_pipe.predict(X_test)
print("Accuracy:",np.mean(predictedLinearSVC == X_test_targetSentiment))
# In[80]:
from sklearn.model_selection import GridSearchCV
parameters = {'vect__ngram_range': [(1, 1), (1, 2)],
'tfidf__use_idf': (True, False),
}
gs_clf_LinearSVC_pipe = GridSearchCV(clf_linearSVC_pipe, parameters, n_jobs=-1)
gs_clf_LinearSVC_pipe = gs_clf_LinearSVC_pipe.fit(X_train, X_train_targetSentiment)
# In[81]:
#X_train
# In[53]:
#check=["I thought I owned every kindle model and was surprised to find this one. I loved the shape of this devise and was totally fascinated by it.","lost interest didnt exactly flow with the previous four books"]
#pre=gs_clf_LinearSVC_pipe.predict(check)
#print(pre1)
#READING THE FILE FROM USER (VISHESH)
def gen_file(file_path):
df_in=pd.read_csv(file_path)
s_inf=df_in['reviews.text'] # Chanage reviews.text to Reviews based on the format
#df2.loc[df2['TotalCharges'].isna()==True]
#print(df.loc[df['reviews.text']].isna()==True)
#print(s_inf)
s_pred=np.asarray(s_inf);
#print(s_pred)
#print(s_pred.shape)
# PREDICTING ON THE GIVEN REVIEWS
pred=gs_clf_LinearSVC_pipe.predict(s_pred)
# CREATING SUBMITTIG FILE
st=df_in['reviews.text']
df_sub=pd.DataFrame(data=st)
id1=df_in['id']
df_sub.insert(0,"prod_ID",id1,allow_duplicates=False)
name=df_in['name']
df_sub.insert(1,"Name",name,allow_duplicates=False)
df_sub.insert(2,"pre",pred,allow_duplicates=False)
df_sub.insert(3,"Positive",0,allow_duplicates=False)
df_sub.insert(4,"Negative",0,allow_duplicates=False)
df_sub.insert(5,"Neutral",0,allow_duplicates=False)
for i in range(len(st)):
if df_sub['pre'][i]=="Positive":
df_sub['Positive'][i]=1;
elif df_sub['pre'][i]=="Negative":
df_sub['Negative'][i]=1;
else:
df_sub['Neutral'][i]=1;
df_sub.rename(columns={'reviews.text':'Reviews'},inplace =True)
del df_sub['pre']
df_sub.to_csv(r"./SIH/a.csv", index=False)