-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlinear.py
46 lines (41 loc) · 1.45 KB
/
linear.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
from sklearn import metrics
class LinearClassifier:
"""
Class represents the linear classification model
"""
def __init__(self, model):
"""
Initializes the linear classification model
:param model: scikit-learn classification model
"""
self.model = model
def fit(self, x_train, y_train, x_val, y_val):
"""
Fits the model
:param x_train: training samples
:param y_train: training labels
:param x_val: validation samples
:param y_val: validation labels
:return: fitted model
"""
self.model.fit(x_train, y_train[:, 0])
return self.evaluate(x_val, y_val)
def evaluate(self, x_test, y_test):
"""
Evaluates the fitted model
:param x_test: test samples
:param y_test: test labels
:return: required metrics
"""
predictions = []
real_outputs = []
for i in range(x_test.shape[0]):
pattern = x_test[i, :].reshape(1, -1)
prediction = self.model.predict(pattern)
predictions.append(prediction[0])
real_outputs.append(y_test[i, 0])
auc = metrics.roc_auc_score(real_outputs, predictions)
acc = metrics.accuracy_score(real_outputs, predictions)
prec = metrics.precision_score(real_outputs, predictions)
recall = metrics.recall_score(real_outputs, predictions)
return [auc, acc, prec, recall]