-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine_learning.py
163 lines (108 loc) · 5.5 KB
/
machine_learning.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
import model
import numpy as np
import tensorflow as tf
from joblib import dump, load
def pre_train_machine_learning(embedding_model_name, machine_type, X_train, X_test, y_train, y_test):
if machine_type == 'Logistic':
# load the machine_model from disk
filename = '/home/ubuntu/project2/machine_model/' + embedding_model_name.lower() + '_logistic.pkl'
log_clf = load(filename)
train_y_pred = log_clf.predict(X_train)
test_y_pred = log_clf.predict(X_test)
elif machine_type == 'SVM':
# load the machine_model from disk
filename = '/home/ubuntu/project2/machine_model/' + embedding_model_name.lower() + '_svm.pkl'
svm_clf = load(filename)
train_y_pred = svm_clf.predict(X_train)
test_y_pred = svm_clf.predict(X_test)
elif machine_type == 'RandomForest':
# load the machine_model from disk
filename = '/home/ubuntu/project2/machine_model/' + embedding_model_name.lower() + '_randomforest.pkl'
rnd_clf = load(filename)
train_y_pred = rnd_clf.predict(X_train)
test_y_pred = rnd_clf.predict(X_test)
elif machine_type == 'FNN':
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
train_label = le.fit_transform(y_train)
# load the machine_model from disk
fnn_clf = tf.keras.models.load_model(
'/home/ubuntu/project2/machine_model/' + embedding_model_name.lower() + '_fnn.h5')
train_prediction = fnn_clf.predict(X_train.toarray())
test_prediction = fnn_clf.predict(X_test.toarray())
tr_y_pred = []
for i in range(len(train_prediction)):
tr_y_pred.append(np.argmax(train_prediction[i]))
te_y_pred = []
for i in range(len(test_prediction)):
te_y_pred.append(np.argmax(test_prediction[i]))
train_y_pred = le.inverse_transform(tr_y_pred)
test_y_pred = le.inverse_transform(te_y_pred)
elif machine_type == 'user_defined_machine_learning':
pass
return train_y_pred, test_y_pred
def machine_learning(embedding_model_name, machine_type, X_train, X_test, y_train, y_test, params):
target_names = list(set(y_train))
if machine_type == 'Logistic':
log_clf = model.myLogisticRegression(params)
log_clf.fit(X_train, y_train)
# save the machine_model to disk
filename = '/home/ubuntu/project2/machine_model/' + embedding_model_name.lower() + '_logistic.pkl'
dump(log_clf, filename)
train_y_pred = log_clf.predict(X_train)
test_y_pred = log_clf.predict(X_test)
elif machine_type == 'SVM':
svm_clf = model.mySVM(params)
svm_clf.fit(X_train, y_train)
# save the machine_model to disk
filename = '/home/ubuntu/project2/machine_model/' + embedding_model_name.lower() + '_svm.pkl'
dump(svm_clf, filename)
train_y_pred = svm_clf.predict(X_train)
test_y_pred = svm_clf.predict(X_test)
elif machine_type == 'RandomForest':
rnd_clf = model.myRandomForestClassifier(params)
rnd_clf.fit(X_train, y_train)
# save the machine_model to disk
filename = '/home/ubuntu/project2/machine_model/' + embedding_model_name.lower() + '_randomforest.pkl'
dump(rnd_clf, filename)
train_y_pred = rnd_clf.predict(X_train)
test_y_pred = rnd_clf.predict(X_test)
elif machine_type == 'FNN':
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
train_label = le.fit_transform(y_train)
input_layer_units = int(params['input_layer_units'][0])
hidden_layer_units = int(params['hidden_layer_units'][0])
output_layer_units = len(target_names)
hidden_layer_count = int(params['hidden_layer_count'][0])
input_layer_activation = params['input_layer_activation'][0]
hidden_layer_activation = params['hidden_layer_activation'][0]
output_layer_activation = params['output_layer_activation'][0]
optimizer = params['optimizer'][0]
epochs = int(params['epochs'][0])
batch_size = int(params['batch_size'][0])
fnn_clf = tf.keras.Sequential()
fnn_clf.add(tf.keras.layers.Dense(input_layer_units, activation=input_layer_activation, input_shape=(len(X_train.toarray()[0]), )))
for i in range(hidden_layer_count):
fnn_clf.add(tf.keras.layers.Dense(hidden_layer_units, activation=hidden_layer_activation))
fnn_clf.add(tf.keras.layers.Dense(output_layer_units, activation=output_layer_activation))
fnn_clf.compile(optimizer=optimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
fnn_clf.fit(X_train.toarray(), train_label, epochs=epochs, batch_size=batch_size)
# save the machine_model to disk
fnn_clf.save(
'/home/ubuntu/project2/machine_model/' + embedding_model_name.lower() + '_fnn.h5')
train_prediction = fnn_clf.predict(X_train.toarray())
test_prediction = fnn_clf.predict(X_test.toarray())
tr_y_pred = []
for i in range(len(train_prediction)):
tr_y_pred.append(np.argmax(train_prediction[i]))
te_y_pred = []
for i in range(len(test_prediction)):
te_y_pred.append(np.argmax(test_prediction[i]))
train_y_pred = le.inverse_transform(tr_y_pred)
test_y_pred = le.inverse_transform(te_y_pred)
elif machine_type == 'user_defined_machine_learning':
pass
return train_y_pred, test_y_pred