-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_prediction.py
148 lines (111 loc) · 4.03 KB
/
model_prediction.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
import os
from joblib import dump, load
from sklearn import svm
from sklearn.model_selection import GridSearchCV
from sklearn.naive_bayes import GaussianNB
from sklearn.neural_network import MLPClassifier
from definitions import ROOT_DIR
from parser import Parser
class ModelPrediction:
path_gend_mod = 'gender_model.joblib'
path_sent_mod = 'sentiment_model.joblib'
path_coun_mod = 'country_model.joblib'
def __init__(self, iterator, corpus=None, dir_path=os.path.join(ROOT_DIR, 'saved_models')):
"""
:type corpus: pandas.DataFrame
"""
self.dir_path = dir_path
self.tweets = corpus
self.iterator = iterator
self.X = []
self.gender = []
self.sentiment = []
self.country = []
for i in self.iterator:
vector, sentiment, gender, country = i
self.X.append(vector)
self.gender.append(gender)
self.sentiment.append(sentiment)
self.country.append(country)
self.gender_mod = None
self.sentiment_mod = None
self.country_mod = None
if not os.path.exists(dir_path):
os.mkdir(dir_path)
def load_corpus(self):
if self.tweets is None:
self.tweets = Parser.parsing_vector_corpus_pandas(os.path.join(ROOT_DIR, "corpus/iot-tweets-vector-v3.tsv"))
def gender_model(self, file_path=path_gend_mod):
"""Method to create model prediction user's gender using a SVM classifier"""
full_path = os.path.join(self.dir_path, file_path)
if not os.path.exists(full_path):
y = self.gender
self.gender_mod = svm.SVC(kernel='linear')
self.gender_mod.fit(self.X, y)
dump(self.gender_mod, full_path)
else:
if self.gender_mod is None:
self.gender_mod = load(full_path)
return self.gender_mod
def sentiment_model(self, file_path=path_sent_mod):
"""Method to create model prediction user's sentiment using a CNN"""
full_path = os.path.join(self.dir_path, file_path)
if not os.path.exists(full_path):
y = self.sentiment
self.sentiment_mod = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1)
self.sentiment_mod.fit(self.X, y)
dump(self.sentiment_mod, full_path)
else:
if self.sentiment_mod is None:
self.sentiment_mod = load(full_path)
return self.sentiment_mod
def country_model(self, file_path=path_coun_mod):
"""Method to create model prediction user's country using a Naive Bayes network"""
full_path = os.path.join(self.dir_path, file_path)
if not os.path.exists(full_path):
y = self.country
self.country_mod = GaussianNB()
self.country_mod.fit(self.X, y)
dump(self.country_mod, full_path)
else:
if self.country_mod is None:
self.country_mod = load(full_path)
return self.country_mod
def tweak_hyperparameters(self, type_model):
self.load_corpus()
if type_model == "SVM":
y = self.gender
model = svm.SVC()
param_grid = [
{'C': [1, 10, 100, 1000], 'kernel': ['linear']},
{'C': [1, 10, 100, 1000], 'gamma': [0.001, 0.0001], 'kernel': ['rbf']},
]
clf = GridSearchCV(model, param_grid, n_jobs=-1, cv=3)
clf.fit(self.X, y)
# Best parameter set
print('Best parameters found:\n', clf.best_params_)
else:
y = self.sentiment
model = MLPClassifier(max_iter=100)
param_grid = {
'hidden_layer_sizes': [(50, 50, 50), (50, 100, 50), (100,)],
'activation': ['tanh', 'relu'],
'solver': ['sgd', 'adam'],
'alpha': [0.0001, 0.05],
'learning_rate': ['constant', 'adaptive'],
}
clf = GridSearchCV(model, param_grid, n_jobs=-1, cv=3)
clf.fit(self.X, y)
# Best parameter set
print('Best parameters found:\n', clf.best_params_)
if __name__ == '__main__':
corpus = Parser.parsing_vector_corpus_pandas(os.path.join(ROOT_DIR, 'corpus/iot-tweets-vector-v3.tsv'))
model = ModelPrediction(corpus=corpus)
model.tweak_hyperparameters("SVM")
model.tweak_hyperparameters("MLP")
# print(model.gender_model())
# print(model.sentiment_model())
# print(model.country_model())
# print(model.gender_model().predict(np.zeros(300).reshape(1, -1)))
# print(model.sentiment_model().predict(np.zeros(300).reshape(1, -1)))
# print(model.country_model().predict(np.zeros(300).reshape(1, -1)))