-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.py
49 lines (35 loc) · 1.92 KB
/
Test.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 numpy as np
from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split
import timeit
from AdaBoost import Adaboost
class Test:
def __init__(self, data, name, n_random_choice):
x_data = data.iloc[:, :data.shape[1] - 1]
y_data = data.iloc[:, data.shape[1] - 1]
self.x_train, self.x_test, self.y_train, self.y_test = train_test_split(x_data
, y_data
, train_size=0.8
, random_state=25)
self.name = name
self.n_random_choice = n_random_choice
def run_test_get_result(self, max_iteration, n_random_choice):
adaboost = Adaboost(max_iter=max_iteration, n_random_choice=n_random_choice, categorical=False)
start = timeit.default_timer()
adaboost.training_predict(self.x_train, self.y_train, self.x_test)
training_accuracy = []
testing_accuracy = []
for i in range(1, max_iteration):
training_accuracy.append(self.accuracy(adaboost.training_pred[i], self.y_train))
testing_accuracy.append(self.accuracy(adaboost.testing_pred[i], self.y_test))
stop = timeit.default_timer()
step = list(range(1, max_iteration))
plt.plot(step, training_accuracy, color='tab:blue', label='Accuracy on training data')
plt.plot(step, testing_accuracy, color='tab:orange', label='Accuracy on testing data')
time = "{:.5f}".format(stop-start)
plt.legend()
plt.grid()
plt.title(label=self.name + ' _ ' + 'Random choice: ' + str(self.n_random_choice) + ' _ ' + time + ' seconds')
plt.show()
def accuracy(self, predictions, expectations):
return np.sum(predictions == expectations) / len(expectations)