This repository has been archived by the owner on Oct 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter_8.py
196 lines (157 loc) · 5.02 KB
/
chapter_8.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
"""
Copyright Jeremy Nation <[email protected]>.
Licensed under the MIT license.
Almost entirely copied from code created by Sebastian Raschka, also licensed under the MIT license.
"""
import os
import re
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
import numpy as np
import pandas as pd
from sklearn.feature_extraction.text import (
CountVectorizer,
HashingVectorizer,
TfidfTransformer,
TfidfVectorizer,
)
from sklearn.grid_search import GridSearchCV
from sklearn.linear_model import (
LogisticRegression,
SGDClassifier,
)
from sklearn.pipeline import Pipeline
def preprocessor(text):
text = re.sub('<[^>]*>', '', text)
emoticons = re.findall('(?::|;|=)(?:-)?(?:\)|\(|D|P)', text)
text = (
re.sub('[\W]+', ' ', text.lower()) +
' '.join(emoticons).replace('-', '')
)
return text
def tokenizer(text):
return text.split()
def tokenizer_porter(text):
return [PorterStemmer().stem(word) for word in text.split()]
def run_grid_search():
df = pd.read_csv(os.path.join('datasets', 'movie_data.csv'))
X_train = df.loc[:25000, 'review'].values
y_train = df.loc[:25000, 'sentiment'].values
X_test = df.loc[25000:, 'review'].values
y_test = df.loc[25000:, 'sentiment'].values
stop = stopwords.words('english')
tfidf = TfidfVectorizer(
strip_accents=None,
lowercase=None,
preprocessor=None,
)
param_grid = [
{
'vect__ngram_range': [(1, 1)],
'vect__stop_words': [stop, None],
'vect__tokenizer': [tokenizer, tokenizer_porter],
'clf__penalty': ['l1', 'l2'],
'clf__C': np.logspace(0, 2, num=3),
},
{
'vect__ngram_range': [(1, 1)],
'vect__stop_words': [stop, None],
'vect__tokenizer': [tokenizer, tokenizer_porter],
'vect__use_idf': [False],
'vect__norm': [None],
'clf__penalty': ['l1', 'l2'],
'clf__C': np.logspace(0, 2, num=3),
},
]
lr_tfidf = Pipeline([
('vect', tfidf),
('clf', LogisticRegression(random_state=0)),
])
gs_lr_tfidf = GridSearchCV(
lr_tfidf,
param_grid,
scoring='accuracy',
cv=5,
verbose=1,
n_jobs=-1,
)
gs_lr_tfidf.fit(X_train, y_train)
print(gs_lr_tfidf)
print("Best parameter set: %s" % gs_lr_tfidf.best_params_)
print("CV accuracy: %.3f" % gs_lr_tfidf.best_score_)
clf = gs_lr_tfidf.best_estimator_
print("Test accuracy: %.3f" % clf.score(X_test, y_test))
def get_minibatch(doc_stream, size):
docs = []
y = []
try:
for _ in range(size):
text, label = next(doc_stream)
docs.append(text)
y.append(label)
except StopIteration:
docs = None
y = None
return docs, y
def stream_docs(path):
with open(path, 'r', encoding='utf-8') as csv:
next(csv)
for line in csv:
text = line[:-3]
label = int(line[-2])
yield text, label
def tokenizer_streaming(text):
text = preprocessor(text)
stop = stopwords.words('english')
tokenized = [w for w in text.split() if w not in stop]
return tokenized
def run_online_classifier():
vect = HashingVectorizer(
decode_error='ignore',
n_features=2**21,
preprocessor=None,
tokenizer=tokenizer_streaming,
)
clf = SGDClassifier(loss='log', random_state=1, n_iter=1)
csv_filename = os.path.join('datasets', 'movie_data.csv')
doc_stream = stream_docs(path=csv_filename)
classes = np.array([0, 1])
for _ in range(45):
X_train, y_train = get_minibatch(doc_stream, size=1000)
if X_train is None:
break
else:
X_train = vect.transform(X_train)
clf.partial_fit(X_train, y_train, classes=classes)
X_test, y_test = get_minibatch(doc_stream, size=5000)
X_test = vect.transform(X_test)
print("Test accuracy: %.3f" % clf.score(X_test, y_test))
clf = clf.partial_fit(X_test, y_test)
def work_with_simple_bag_of_words():
count = CountVectorizer()
docs = np.array([
'The sun is shining',
'The weather is sweet',
'The sun is shining and the weather is sweet',
])
bag = count.fit_transform(docs)
print(count.vocabulary_)
print(bag.toarray())
np.set_printoptions(precision=2)
tfidf = TfidfTransformer(use_idf=True, norm='l2', smooth_idf=True)
print(tfidf.fit_transform(bag).toarray())
tf_is = 2
n_docs = 3
idf_is = np.log((n_docs+1) / (3+1))
tfidf_is = tf_is * (idf_is + 1)
print("tf-idf of term 'is' = %.2f" % tfidf_is)
tfidf = TfidfTransformer(use_idf=True, norm=None, smooth_idf=True)
raw_tfidf = tfidf.fit_transform(bag).toarray()[-1]
print(raw_tfidf)
l2_tfidf = raw_tfidf / np.sqrt(np.sum(raw_tfidf**2))
print(l2_tfidf)
if __name__ == '__main__':
np.random.seed(0)
# work_with_simple_bag_of_words()
# run_grid_search()
run_online_classifier()