-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp_folds.py
49 lines (41 loc) · 1.29 KB
/
exp_folds.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
import TLNN as tl
import numpy as np
import sklearn as sk
import pandas as pd
import matplotlib.pyplot as plt
# load iris dataset
name = 'wine'
dataset = pd.read_csv(f'Datasets/{name}.csv')
X = dataset.drop('class', axis=1).values
y = dataset['class'].values
# 5-fold indices split
kf = sk.model_selection.KFold(n_splits=10, shuffle=True, random_state=0)
# values for k
ks = [1,3,5,7,9,11]
acc = [] # information of accuracy
for ki in ks:
print(f'\n### K = {ki} ###')
aux_acc = []
for i, (train_index, test_index) in enumerate(kf.split(X)):
# data split
X_train = X[train_index]
y_train = y[train_index]
X_test = X[test_index]
y_test = y[test_index]
# predictions
classifier = tl.kTLNN(k=ki, kb_factor=1.4)
classifier.fit(X_train, y_train)
preds = classifier.predict(X_test)
accF = sk.metrics.accuracy_score(y_true=y_test, y_pred=preds)
aux_acc.append(accF)
print(f"- Fold {i+1}: {accF}")
print(f'- Mean: {np.mean(aux_acc)}')
acc.append(aux_acc)
#acc = np.array(acc)
# grafico
fig, ax = plt.subplots()
ax.set_ylabel('accuracy')
ax.set_xlabel('K values')
ax.set_title(f'Accuracy on 10-folds on {name} dataset')
ax.boxplot(acc, tick_labels=ks)
fig.savefig(f'imgs/ks_{name}',bbox_inches ="tight",dpi=300)