forked from the-aerospace-corporation/brainblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_classifier_comparison.py
More file actions
203 lines (164 loc) · 6.84 KB
/
run_classifier_comparison.py
File metadata and controls
203 lines (164 loc) · 6.84 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/python
# ==============================================================================
# run_classifier_comparison.py
# ==============================================================================
import time
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from matplotlib.cm import get_cmap
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from sklearn.datasets import make_moons, make_circles, make_classification, make_s_curve, make_swiss_roll, make_blobs
from sklearn.neural_network import MLPClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
from brainblocks.tools import BBClassifier
from brainblocks.datasets.classification import make_box_data_grid, make_box_data_random
import faulthandler
faulthandler.enable()
# h = .05 # step size in the mesh
h = .02 # step size in the mesh
num_samples = 500
rand_seed = 42
color_maps = ['Reds', 'Blues', 'Greens', 'Oranges', 'Purples', 'Greys']
color_list = ['r', 'b', 'g', 'orange', 'purple', 'grey']
names = [
'BBClassifier',
'Nearest Neighbors',
'Decision Tree',
'Neural Net',
'Naive Bayes']
classifiers = [
BBClassifier(num_epochs=3, use_normal_dist_bases=True,
use_evenly_spaced_periods=True, random_state=rand_seed),
KNeighborsClassifier(3),
DecisionTreeClassifier(max_depth=5, random_state=rand_seed),
MLPClassifier(alpha=1, max_iter=1000, hidden_layer_sizes=(100, 100),
random_state=rand_seed),
GaussianNB()]
noise = 0.1
# Generate separable 2 classes dataset
X, y = make_classification(n_samples=num_samples, n_features=2, n_redundant=0,
n_informative=2, random_state=rand_seed,
n_clusters_per_class=1, n_classes=2)
rng = np.random.RandomState(2)
X += 2 * rng.uniform(low=0.0, high=noise, size=X.shape)
separable_2_classes = (X, y)
# Generate separable 3 classes dataset
X, y = make_classification(n_samples=num_samples, n_features=2, n_redundant=0,
n_informative=2, random_state=rand_seed,
n_clusters_per_class=1, n_classes=3)
rng = np.random.RandomState(2)
X += 2 * rng.uniform(low=0.0, high=noise, size=X.shape)
separable_3_classes = (X, y)
# Generate separable 4 classes dataset
X, y = make_classification(n_samples=num_samples, n_features=2, n_redundant=0,
n_informative=2, random_state=rand_seed,
n_clusters_per_class=1, n_classes=4)
rng = np.random.RandomState(2)
X += 2 * rng.uniform(low=0.0, high=noise, size=X.shape)
separable_4_classes = (X, y)
# Add separable 2, 3, and 4 datasets
datasets = [separable_2_classes, separable_3_classes, separable_4_classes]
# Add moons, circles, and box datasets
datasets += [
make_moons(n_samples=num_samples, noise=0.3, random_state=rand_seed),
make_circles(n_samples=num_samples, noise=0.2, factor=0.5,
random_state=rand_seed),
make_box_data_random(n_samples=num_samples, min_val=-0.3, max_val=1.3,
stratify=True, shuffle=True),
make_box_data_grid(h=0.05, min_val=-0.3, max_val=1.3, shuffle=True)]
# Add blobs datasets
for k in range(2, 5):
X, y = make_blobs(n_samples=num_samples, n_features=2, centers=k,
random_state=rand_seed)
rng = np.random.RandomState(2)
X += 2 * rng.uniform(low=0.0, high=3*noise, size=X.shape)
datasets.append((X, y))
# Setup matplotlib figure
figure = plt.figure(figsize=(18, 18))
i = 1
# Iterate over datasets
for ds_cnt, ds in enumerate(datasets):
print('Data Set %d' % ds_cnt)
# Preprocess dataset, split into training and test part
X, y = ds
X = MinMaxScaler().fit_transform(X)
X_train, X_test, y_train, y_test = \
train_test_split(X, y, test_size=.2, random_state=rand_seed)
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
# Color map based on number of classes
n_classes = np.unique(y).size
cm_bright = ListedColormap(color_list[:n_classes])
# Plot the dataset first
ax = plt.subplot(len(datasets), len(classifiers) + 1, i)
if ds_cnt == 0:
ax.set_title('Input data')
# Plot the training points
ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright,
edgecolors='k')
ax.set_xlim(xx.min(), xx.max())
ax.set_ylim(yy.min(), yy.max())
ax.set_xticks(())
ax.set_yticks(())
i += 1
# Iterate over classifiers
for name, clf in zip(names, classifiers):
ax = plt.subplot(len(datasets), len(classifiers) + 1, i)
print('Train %s' % name)
t0 = time.time()
clf.fit(X_train, y_train)
t1 = time.time()
print('Time %fs with size %d' % ((t1 - t0), y_train.shape[0]))
t0 = time.time()
score = clf.score(X_test, y_test)
t1 = time.time()
print('Score:', score)
print('Time %fs with size %d' % ((t1 - t0), y_test.shape[0]))
# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, x_max]x[y_min, y_max].
t0 = time.time()
Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])
t1 = time.time()
print('Decision Space')
print('Time %fs with size %d' % ((t1 - t0), xx.size))
# Zero the classes that are not the maximum probability
new_Z = []
for row in Z:
maxIndex = np.argmax(row)
tempRow = np.zeros(row.shape)
tempRow[maxIndex] = row[maxIndex]
new_Z.append(tempRow)
Z = np.asarray(new_Z)
for k in range(n_classes):
Z_class = Z[:, k].reshape(xx.shape)
class_colors = get_cmap(color_maps[k % n_classes], 100)
class_colors = class_colors(np.linspace(0, 1, 100))
class_colors[:, 3] = np.linspace(0, 1, 100)
class_cmp = ListedColormap(class_colors)
ax.contourf(xx, yy, Z_class, cmap=class_cmp)
# Plot the test points
ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright,
edgecolors='k', alpha=0.6)
ax.set_xlim(xx.min(), xx.max())
ax.set_ylim(yy.min(), yy.max())
ax.set_xticks(())
ax.set_yticks(())
if ds_cnt == 0:
ax.set_title(name)
ax.text(xx.max() - .3, yy.min() + .3, ('%.2f' % score).lstrip('0'),
size=15, horizontalalignment='right')
i += 1
print()
print('-----------', flush=True)
print()
plt.tight_layout()
plt.savefig('classifier_comparison.png')
plt.close()
for clf in classifiers:
del clf