-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear_svc_sreamlit.py
196 lines (69 loc) · 2.07 KB
/
linear_svc_sreamlit.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
#!/usr/bin/env python
# coding: utf-8
# ### Only take screen shots of streamlit code instead of machine_learning model
# In[5]:
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import cross_val_score
from sklearn.svm import LinearSVC
from sklearn import metrics
# In[7]:
df=pd.read_csv('/Users/apple/Desktop/Machine_Learning/week_7/IMDB_movie_reviews_train.csv')
# /Users/apple/Desktop/Machine_Learning/week_7
# In[8]:
df.shape
# In[9]:
df.head(5)
# In[12]:
df.isna().sum()
# In[13]:
df.sentiment.value_counts()
# In[19]:
X=df.loc[:,['review']]
y=df.sentiment
# In[30]:
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3,stratify=y)
# In[31]:
y_train.value_counts()
# In[32]:
X_train_docs=[doc for doc in X_train.review]
# In[33]:
vect=CountVectorizer(ngram_range=(1,3),stop_words='english',max_features=1000).fit(X_train_docs)
# In[34]:
X_train_features=vect.transform(X_train_docs)
# In[35]:
print('X_train_features:\n{}'.format(repr(X_train_features)))
# In[36]:
feature_names=vect.get_feature_names()
# In[37]:
print("Number of features:{}".format(len(feature_names)))
print("First 100 features:\n{}".format(feature_names[:100]))
print("Every 100th feature:\n{}".format(feature_names[::100]))
# In[38]:
lin_svc=LinearSVC(max_iter=120000)
# In[40]:
scores=cross_val_score(lin_svc, X_train_features, y_train, cv=5)
print("Mean cross-validation accuracy:{:.2f}".format(np.mean(scores)))
# In[41]:
lin_svc.fit(X_train_features, y_train)
# In[43]:
X_test_docs=[doc for doc in X_test.review]
X_test_features=vect.transform(X_test_docs)
# In[44]:
y_test_pred=lin_svc.predict(X_test_features)
# In[45]:
metrics.accuracy_score(y_test, y_test_pred)
# In[46]:
import pickle
# In[47]:
pickle.dump(lin_svc,open('linear_svc_model','wb'))
# In[48]:
lin_svc=pickle.load(open('linear_svc_model','rb'))
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]: