-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNBClassifier.py
341 lines (158 loc) · 4.82 KB
/
NBClassifier.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
#!/usr/bin/env python
# coding: utf-8
#
# # Naive Bayes Text Classifier for Amazon Mobile Phone Reviews
# This data set contains 400 thousand reviews of unlocked mobile phones sold on Amazon.com.
# ATTRIBUTE LIST
# 1.Product Title
# 2.Brand
# 3.Price
# 4.Rating
# 5.Review text
# 6.Number of people who found the review helpful
# In[31]:
#Importing packages
import numpy as np
import pandas as pd
# In[32]:
#Reading dataset using pandas
df=pd.read_csv("C://Users//Butterfly//Documents//Data//Amazon_Unlocked_Mobile.csv")
# In[33]:
#Getting idea about the dataset
print(df.info())
print(df.describe())
print(pd.isnull(df).sum())
# In[34]:
#Removing unwanted columns from the imported dataset
df1=df.drop(columns=['ProductName','Price','ReviewVotes'])
print(df1.head())
# In[35]:
#Removing null values from the dataset
df_new=df1.dropna(axis=0,how='any')
print(pd.isnull(df_new).sum())
print(df_new.shape)
# In[36]:
#Getting count of the different brands
df_new['BrandName']=df_new['BrandName'].str.lower()
print(df_new['BrandName'].value_counts())
# In[37]:
#Subsetting the dataset into two sets
#one for samsung mobile phones and the other for apple phones
Samsung=df_new[df_new['BrandName']=='samsung']
Apple=df_new[df_new['BrandName']=='apple']
# In[41]:
#Plotting count of ratings for samsung
import seaborn as sns
sns.countplot(x='Rating',hue='Rating',data=Samsung)
# In[42]:
#Plotting count of ratings for apple
sns.countplot(x='Rating',hue='Rating',data=Apple)
# In[38]:
#Gathering packages and tools required for preprocessing the text
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import CountVectorizer
# In[39]:
#Choosing the instances which are having either one or five rating in the samsung brand
Sam_data=Samsung[(Samsung['Rating']==1) | (Samsung['Rating']==5)]
print(Sam_data.shape)
print(Sam_data.describe())
# In[43]:
#Function for preprocessing the text inputs
import string
def text_process(text):
nopunc = [char for char in text if char not in string.punctuation]
nopunc = ''.join(nopunc)
return [word for word in nopunc.split() if word.lower() not in stopwords.words('english')]
# In[44]:
#Choosing target and predictor variables
SY=Sam_data['Rating']
SX=Sam_data['Reviews']
# In[45]:
#Converting the predictor data(reviews) into vector and fitting
count_matrix = CountVectorizer(analyzer=text_process).fit(SX)
# In[47]:
SX=count_matrix.transform(SX)
# In[49]:
print('Shape of Sparse Matrix: ', SX.shape)
print('Amount of Non-Zero occurrences: ', SX.nnz)
# In[50]:
#Splitting data as train and test set and fitting the model
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
# In[51]:
X_train, X_test, Y_train, Y_test = train_test_split(SX,SY, test_size=0.2, random_state=101)
# In[56]:
print(X_train.shape,Y_train.shape)
print(X_test.shape,Y_test.shape)
# In[59]:
nb = MultinomialNB()
nb.fit(X_train, Y_train)
# In[60]:
preds = nb.predict(X_test)
preds
# In[87]:
post="i hate it"
rate=nb.predict(X_test[1000])
print(rate)
# In[61]:
#Model Evaluation
from sklearn.metrics import confusion_matrix, classification_report
# In[64]:
print(confusion_matrix(Y_test, preds))
print('\n')
print(classification_report(Y_test, preds))
# In[66]:
metrics.accuracy_score(Y_test, preds)
# In[67]:
# examine class distribution
print(Y_test.value_counts())
# In[71]:
#vocabulary for the test data
X_train_tokens=count_matrix.get_feature_names()
len(X_train_tokens)
print(X_train_tokens[0:200])
# In[70]:
print(X_train_tokens[-200:])
# In[72]:
nb.feature_count_.shape
#2 classes and 33965 tokens
# In[90]:
from sklearn.linear_model import LogisticRegression
# In[ ]:
# In[88]:
App_data=Apple[(Apple['Rating']==1) | (Apple['Rating']==5)]
App_data.shape
# In[13]:
n=len(App_data['Reviews'])
n
# In[14]:
import string
def text_process(text):
nopunc = [char for char in text if char not in string.punctuation]
nopunc = ''.join(nopunc)
return [word for word in nopunc.split() if word.lower() not in stopwords.words('english')]
# In[17]:
AY=App_data['Rating']
AX=App_data['Reviews']
# In[18]:
count_matrix1 = CountVectorizer(analyzer=text_process).fit(AX)
# In[21]:
AX=count_matrix1.transform(AX)
# In[22]:
print('Shape of Sparse Matrix: ', AX.shape)
print('Amount of Non-Zero occurrences: ', AX.nnz)
# In[23]:
from sklearn.model_selection import train_test_split
X_train1, X_test1, Y_train1, Y_test1 = train_test_split(AX,AY, test_size=0.3, random_state=101)
# In[26]:
nb1 = MultinomialNB()
nb1.fit(X_train1, Y_train1)
# In[28]:
preds1 = nb1.predict(X_test1)
# In[29]:
from sklearn.metrics import confusion_matrix, classification_report
# In[30]:
print(confusion_matrix(Y_test1, preds1))
print('\n')
print(classification_report(Y_test1, preds1))
# In[ ]: