-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
163 lines (139 loc) · 5.88 KB
/
run.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 matplotlib.pyplot as plt
from pandas import DataFrame
import numpy as np
import rbf_es
"""
.. sectionauthor:: Ali ArjomandBigdeli <https://github.com/aliarjomandbigdeli>
.. since:: 6/2/2019
"""
colors = {-2: 'black', - 1: 'orange', 0: 'red', 1: 'blue', 2: 'green', 3: 'cyan', 4: 'yellow', 5: 'black', 6: 'magenta'}
def main():
print('RBF Network')
run_regression()
run_bin_classification()
run_classification()
def run_classification():
sample_num = 1500
test_size = 5000
iter_num = 5
my_rbf_classifier = rbf_es.RBFClassifier()
my_rbf_classifier.read_excel("dataset/5clstrain1500.xlsx", "dataset/5clstest5000.xlsx")
my_rbf_classifier.initialize_parameters_based_on_data()
my_rbf_classifier.train(iter_num, my_rbf_classifier.data())
my_rbf_classifier.test()
df = DataFrame(dict(x=my_rbf_classifier.data()[:, 0], y=my_rbf_classifier.data()[:, 1],
label=my_rbf_classifier._y_star_before_1hot))
fig, ax = plt.subplots()
grouped = df.groupby('label')
for key, group in grouped:
group.plot(ax=ax, kind='scatter', x='x', y='y', label=key, color=colors[key])
plt.title(f'number of samples: {sample_num} ,number of iterations: {iter_num}, training data')
plt.savefig(f'1.png')
plt.figure()
df = DataFrame(dict(x=my_rbf_classifier.data()[:, 0], y=my_rbf_classifier.data()[:, 1],
label=my_rbf_classifier.y()))
fig, ax = plt.subplots()
grouped = df.groupby('label')
for key, group in grouped:
group.plot(ax=ax, kind='scatter', x='x', y='y', label=key, color=colors[key])
plt.legend()
plt.title(f'number of samples: {sample_num} ,number of iterations: {iter_num}, trained result')
plt.savefig(f'2.png')
plt.figure()
df = DataFrame(
dict(x=my_rbf_classifier._data_test[:, 0], y=my_rbf_classifier._data_test[:, 1],
label=my_rbf_classifier._y_star_test_before_1hot))
fig, ax = plt.subplots()
grouped = df.groupby('label')
for key, group in grouped:
group.plot(ax=ax, kind='scatter', x='x', y='y', label=key, color=colors[key])
# plt.show()
plt.legend()
plt.title(f'number of samples: {test_size} ,number of iterations: {iter_num}, test data')
plt.savefig(f'3.png')
plt.figure()
df = DataFrame(
dict(x=my_rbf_classifier._data_test[:, 0], y=my_rbf_classifier._data_test[:, 1],
label=np.around(my_rbf_classifier._y_test)))
fig, ax = plt.subplots()
grouped = df.groupby('label')
for key, group in grouped:
group.plot(ax=ax, kind='scatter', x='x', y='y', label=key, color=colors[key])
# plt.show()
plt.legend()
plt.title(f'number of samples: {test_size} ,number of iterations: {iter_num}, test predict')
plt.savefig(f'4.png')
def run_regression():
# read excel
iter_num = 6
my_rbf_reg = rbf_es.RBFRegression()
my_rbf_reg.read_excel("dataset/regdata2000.xlsx")
my_rbf_reg.initialize_parameters_based_on_data()
my_rbf_reg.train(iter_num, my_rbf_reg.data())
my_rbf_reg.test()
plt.figure()
plt.plot(my_rbf_reg._y_test, '-o', label='RBF-Net')
plt.plot(my_rbf_reg._y_star_test, '-', label='true')
# -- sorted
plt.legend()
plt.title(f'number of samples: 2000 ,number of iterations: {iter_num}')
# plt.show()
plt.savefig(f'reg-result.png')
def run_bin_classification():
sample_num = 1500
test_num = 5000
iter_num = 5
my_rbf_bin = rbf_es.RBFBinClassifier()
my_rbf_bin.create_random_dataset(sample_num, 2, 2)
my_rbf_bin.read_excel("dataset/2clstrain1500.xlsx", "dataset/2clstest5000.xlsx")
my_rbf_bin.initialize_parameters_based_on_data()
my_rbf_bin.train(iter_num, my_rbf_bin.data())
my_rbf_bin.test()
df = DataFrame(dict(x=my_rbf_bin.data()[:, 0], y=my_rbf_bin.data()[:, 1], label=my_rbf_bin._y_star))
fig, ax = plt.subplots()
grouped = df.groupby('label')
for key, group in grouped:
group.plot(ax=ax, kind='scatter', x='x', y='y', label=key, color=colors[key])
plt.title(f'number of samples: {sample_num} ,number of iterations: {iter_num}, training data')
plt.savefig(f'bin-1.png')
plt.figure()
df = DataFrame(dict(x=my_rbf_bin.data()[:, 0], y=my_rbf_bin.data()[:, 1], label=np.sign(my_rbf_bin.y())))
fig, ax = plt.subplots()
grouped = df.groupby('label')
for key, group in grouped:
group.plot(ax=ax, kind='scatter', x='x', y='y', label=key, color=colors[key])
# plt.show()
plt.legend()
plt.title(f'number of samples: {sample_num} ,number of iterations: {iter_num}, trained result')
plt.savefig(f'bin-2.png')
plt.figure()
df = DataFrame(
dict(x=my_rbf_bin._data_test[:, 0], y=my_rbf_bin._data_test[:, 1], label=my_rbf_bin._y_star_test))
fig, ax = plt.subplots()
grouped = df.groupby('label')
for key, group in grouped:
group.plot(ax=ax, kind='scatter', x='x', y='y', label=key, color=colors[key])
# plt.show()
plt.legend()
plt.title(f'number of samples: {test_num} ,number of iterations: {iter_num}, test data, test data')
plt.savefig(f'bin-3.png')
plt.figure()
df = DataFrame(
dict(x=my_rbf_bin._data_test[:, 0], y=my_rbf_bin._data_test[:, 1], label=np.sign(my_rbf_bin._y_test)))
fig, ax = plt.subplots()
grouped = df.groupby('label')
for key, group in grouped:
group.plot(ax=ax, kind='scatter', x='x', y='y', label=key, color=colors[key])
# plt.show()
plt.legend()
plt.title(
f'number of samples: {test_num} ,number of iterations: {iter_num}, test predict, test predict')
plt.savefig(f'bin-4.png')
# plt.figure()
# plt.plot(my_rbf_bin._best_fitness_list, '-o', label='best')
# plt.plot(my_rbf_bin._avg_fitness_list, '-o', label='average')
# plt.legend()
# plt.title(f'number of samples: {sample_num} ,number of iterations: {iter_num}')
# plt.savefig(f'bin-5.png')
if __name__ == '__main__':
main()