-
Notifications
You must be signed in to change notification settings - Fork 0
/
logreg.py
73 lines (64 loc) · 2.2 KB
/
logreg.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
"""
Author: Colin Swan
Description: Logistic Regression based classification of Yelp Reviews,
categorizing reviews into seasons.
"""
import json
import numpy as np
import stop_words
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.svm import LinearSVC
from sklearn.linear_model import RandomizedLogisticRegression
from sklearn.feature_selection import RFE
from sklearn.feature_selection import f_classif, SelectKBest
class LogReg:
"""
Initialization sets the objects model, vectorizer, labels, and corpus
variables. Initialization also performs the initial training for the model
and vectorizer using the given reviews.
"""
def __init__(
self,
reviews,
vectorizer = TfidfVectorizer(stop_words = 'english', max_df = 1,
ngram_range = (1, 2)),
model = LogisticRegression()
):
self.model = model
self.vectorizer = vectorizer
self.selector = RFE(self.model, step = 100, verbose = 100)
corpus = []
labels = []
for review in reviews:
corpus += [review[1]["text"]]
labels += [review[0]]
#setting variables for the object
self.corpus = corpus
self.labels = labels
self.reviews = reviews
X = self.vectorizer.fit_transform(self.corpus)
self.feature_names = self.vectorizer.get_feature_names()
y = self.labels
for string in self.feature_names:
print(string.encode("ascii", 'ignore'))
#Training the model
X_new = self.selector.fit_transform(X, self.labels)
self.model.fit(X_new, self.labels)
def classify_all(self, all_test_data):
test_corpus = []
y = []
for review in all_test_data:
test_corpus += [review[1]['text']]
y += [review[0]]
#Used transform instead of fit_transform
#for test data so number of features will match
X = self.vectorizer.transform(test_corpus)
X_new = self.selector.transform(X)
results = self.model.predict(X_new)
categories = ["spring", "summer", "fall", "winter"]
for i, category in enumerate(categories):
top10 = np.argsort(self.model.coef_[i])[-20:]
for j in top10:
print("%s: %s" % (category, "".join(self.feature_names[j])))
return results