-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathml_models.py
More file actions
202 lines (166 loc) · 7.32 KB
/
Copy pathml_models.py
File metadata and controls
202 lines (166 loc) · 7.32 KB
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
#!/usr/bin/env python3
import itertools
from collections import defaultdict
import numpy as np
from gensim.models.word2vec import Word2Vec
from sklearn import preprocessing, svm
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.ensemble import (AdaBoostClassifier, ExtraTreesClassifier,
RandomForestClassifier)
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.feature_selection import SelectKBest, chi2
from sklearn.linear_model import LogisticRegression, LogisticRegressionCV
from sklearn.linear_model.passive_aggressive import PassiveAggressiveClassifier
from sklearn.naive_bayes import GaussianNB, MultinomialNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import FeatureUnion, Pipeline
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
from sklearn.svm import SVR, LinearSVC, NuSVC
from textblob import TextBlob
le = preprocessing.LabelEncoder()
use_glove = False
glove = {}
class FeaturesExtractor(BaseEstimator, TransformerMixin):
def fit(self, x, y=None):
return self
def transform(self, tweets):
features = {}
if use_glove:
create_glove(tweets, train=False)
features['text'] = [x for (x, x_high, sentence, sentiment, opinion, target) in tweets]
features['text_high'] = [x_high for (x, x_high, sentence, sentiment, opinion, target) in tweets]
features['sentence'] = [sentence for (x, x_high, sentence, sentiment, opinion, target) in tweets]
features['sentiment'] = [sentiment for (x, x_high, sentence, sentiment, opinion, target) in tweets]
features['sentence_length'] = [len(sentence) for (x, x_high, sentence, sentiment, opinion, target) in tweets]
features['opinion'] = [[opinion] for (x, x_high, sentence, sentiment, opinion, target) in tweets]
features['target'] = [[i] for i in le.fit_transform([target for (x, x_high, sentence, sentiment, opinion, target) in tweets])]
return features
class ItemSelector(BaseEstimator, TransformerMixin):
def __init__(self, key):
self.key = key
def fit(self, x, y=None):
return self
def transform(self, data_dict):
return data_dict[self.key]
def identity(x):
return x
def create_glove(tweets, train=False):
global glove
if train:
with open("data/glove.twitter.27B.200d.txt", "rb") as lines:
wvec = {line.split()[0].decode("utf-8"): np.array(line.split()[1:],dtype=np.float32)
for line in lines}
X = [x for (x, *_) in tweets]
model = Word2Vec(X, size=100, window=5, min_count=5, workers=2)
glove = {w: vec for w, vec in zip(model.wv.index2word, model.wv.syn0)}
else:
tweets = [x for (x, *_) in tweets]
all_words = set(itertools.chain.from_iterable(tweets))
with open("data/glove.twitter.27B.200d.txt", "rb") as infile:
for line in infile:
parts = line.split()
word = parts[0].decode("utf-8")
if (word in all_words):
nums=np.array(parts[1:], dtype=np.float32)
glove[word] = nums
class TfidfEmbeddingVectorizer(object):
def __init__(self, word2vec):
self.word2vec = word2vec
self.word2weight = None
if len(word2vec)>0:
self.dim=len(word2vec[next(iter(glove_small))])
else:
self.dim=0
def fit(self, X, y):
tfidf = TfidfVectorizer(analyzer=lambda x: x)
tfidf.fit(X)
max_idf = max(tfidf.idf_)
self.word2weight = defaultdict(
lambda: max_idf,
[(w, tfidf.idf_[i]) for w, i in tfidf.vocabulary_.items()])
return self
def transform(self, X):
return np.array([
np.mean([self.word2vec[w] * self.word2weight[w]
for w in words if w in self.word2vec] or
[np.zeros(self.dim)], axis=0)
for words in X
])
class SentimentContinuous(BaseEstimator, TransformerMixin):
def fit(self, x, y=None):
return self
def transform(self, tweets):
sentiment = []
for tweet in tweets:
blob = TextBlob(tweet)
sentiment.append([blob.sentiment.polarity])
return sentiment
def model_words():
'''
The model + pipeline for features extracted from the text
'''
clfs = [LinearSVC(),
svm.SVC(kernel='linear', C=1.0),
PassiveAggressiveClassifier(C=1, max_iter=1000, tol=1e-3, n_jobs=-1, class_weight="balanced"),
PassiveAggressiveClassifier(C=0.1, max_iter=1500, tol=0.01, n_jobs=-1, class_weight="balanced", fit_intercept=False, loss="squared_hinge"),
AdaBoostClassifier(n_estimators=200),
MultinomialNB(),
]
classifier = Pipeline([
# Extract the features
('features', FeaturesExtractor()),
# Use FeatureUnion to combine the features from subject and body
('union', FeatureUnion(
transformer_list = [
('text_high', Pipeline([
('selector', ItemSelector(key='text_high')),
('tfidf', TfidfVectorizer(preprocessor = identity, tokenizer = identity,
max_df = .2)),
])),
('word_n_grams', Pipeline([
('selector', ItemSelector(key='sentence')),
('tfidf', TfidfVectorizer(analyzer='word', ngram_range=(1,5)))
])),
('char_n_grams', Pipeline([
('selector', ItemSelector(key='sentence')),
('tfidf', TfidfVectorizer(analyzer='char', ngram_range=(2,5)))
])),
('sentiment', Pipeline([
('selector', ItemSelector(key='sentiment')),
('tfidf', TfidfVectorizer(analyzer='char'))
])),
('opinion_towards', Pipeline([
('selector', ItemSelector(key='opinion')),
])),
('target', Pipeline([
('selector', ItemSelector(key='target')),
])),
#### FEATURES THAT DO NOT WORK ####
# ('sentiment_cont', Pipeline([
# ('selector', ItemSelector(key='sentence')),
# ('feature', SentimentContinuous())
# ])),
# ('glove', Pipeline([
# ('selector', ItemSelector(key='sentence')),
# ('tfidf', TfidfEmbeddingVectorizer(glove))
# ])),
# ('sentence_length', Pipeline([
# ('selector', ItemSelector(key='sentence_length')),
# ('scaler', MinMaxScaler())
# ])),
],
# weight components in FeatureUnion
transformer_weights = {
'text_high': 1,
'word_n_grams': .8,
'char_n_grams': .8,
'sentiment': .8,
'opinion_towards': 1,
'target': 1,
},
)),
# Use a classifier on the combined features
('clf', clfs[2]),
])
return classifier