-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentiment_analysis_using_random_forest.py
145 lines (78 loc) · 2.59 KB
/
sentiment_analysis_using_random_forest.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
#!/usr/bin/env python
# coding: utf-8
# In[37]:
import pandas as pd
# Local directory
Reviewdata = pd.read_csv('train_data.csv')
#data taken from kaggle
# In[38]:
# Apply first level cleaning
import re
import string
#This function converts to lower-case, removes square bracket, removes numbers and punctuation
def text_clean_1(text):
text = text.lower()
text = re.sub('\[.*?\]', '', text)
text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
text = re.sub('\w*\d\w*', '', text)
return text
cleaned1 = lambda x: text_clean_1(x)
# In[39]:
Reviewdata.columns
# In[40]:
Reviewdata['cleaned_description'] = pd.DataFrame(Reviewdata.review.apply(cleaned1))
Reviewdata.head(5)
# In[41]:
# Apply a second round of cleaning
def text_clean_2(text):
text = re.sub('[‘’“”…]', '', text)
text = re.sub('\n', '', text)
return text
cleaned2 = lambda x: text_clean_2(x)
# Let's take a look at the updated text
Reviewdata['cleaned_description_new'] = pd.DataFrame(Reviewdata['cleaned_description'].apply(cleaned2))
Reviewdata.head(5)
# In[42]:
#remove unnecessary columns
Reviewdata.drop(columns = ['review','cleaned_description'], inplace = True)
Reviewdata.head(4)
# In[43]:
from sklearn.model_selection import train_test_split
Independent_var = Reviewdata.cleaned_description_new
Dependent_var = Reviewdata.type
IV_train, IV_test, DV_train, DV_test = train_test_split(Independent_var, Dependent_var, test_size = 0.2, random_state = 225)
# In[44]:
#vectorizeing
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.ensemble import RandomForestClassifier
tvec = TfidfVectorizer()
clf2 = RandomForestClassifier()
# In[45]:
#using pipeline pass data to ran
from sklearn.pipeline import Pipeline
model = Pipeline([('vectorizer',tvec),('classifier',clf2)])
model.fit(IV_train, DV_train)
# In[46]:
from sklearn.metrics import confusion_matrix
predictions = model.predict(IV_test)
confusion_matrix(predictions, DV_test)
# In[47]:
from sklearn.metrics import accuracy_score, precision_score, recall_score
print("Accuracy : ", accuracy_score(predictions, DV_test))
print("Precision : ", precision_score(predictions, DV_test, average = 'weighted'))
print("Recall : ", recall_score(predictions, DV_test, average = 'weighted'))
# In[51]:
ex=[input(("enter a string: "))]
data=model.predict(ex)
if(data==0):
print("negative review")
elif data==1:
print("positive review")
# In[52]:
ex=[input(("enter a string: "))]
data=model.predict(ex)
if(data==0):
print("negative review")
elif data==1:
print("positive review")
# In[ ]: