-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_multiclass.py
61 lines (50 loc) · 1.69 KB
/
test_multiclass.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
# -*- coding: utf-8 -*-
"""
Created on Sat May 16 16:03:47 2020
@author: Marek
"""
import numpy as np
from sklearn.datasets import load_wine
from sklearn.linear_model import LogisticRegression as LR
from sklearn.metrics import accuracy_score
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
from logistic_regression import logistic_regression as lr
np.random.seed(5)
x, y = load_wine(return_X_y=True)
scaler = StandardScaler()
kf = StratifiedKFold(10, shuffle=True, random_state=0)
acc1 = []
acc2 = []
for train_index, validation_index in kf.split(x, y):
scaler = StandardScaler()
y_tr,y_te = y[train_index], y[validation_index]
x_tr,x_te = x[train_index], x[validation_index]
x_tr = scaler.fit_transform(x_tr)
x_te = scaler.transform(x_te)
log = lr.OneVsAll(max_iter=1000, reg_lambda=1e-4, learning_rate=0.15, beta=0.8)
log.fit(x_tr, y_tr)
sk = LR()
sk.fit(x_tr, y_tr)
temp = accuracy_score(y_te, log.predict(x_te))
acc1.append(temp)
temp = accuracy_score(y_te, sk.predict(x_te))
acc2.append(temp)
print(f'Custom LogReg mean acc: {np.mean(acc1)}')
print(f'Sklearn LogReg mean acc: {np.mean(acc2)}')
costs = log.cost_function
plt.plot(range(len(costs)), costs)
plt.title('Training loss')
plt.show()
plt.close()
#
# Test "online learning"
new_examples = 3
online_acc = [acc1[-1]]
for i in range(new_examples):
log.fit_sample(x_te[i, :].reshape(1, x.shape[1]), y_te[i], iterations=2)
online_acc.append(accuracy_score(y_te[i+1:], log.predict(x_te[i+1:, :])))
plt.plot(range(len(online_acc)), online_acc)
plt.title('Accuracy after online learning')
plt.show()