This repository has been archived by the owner on Jan 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plots.py
227 lines (188 loc) · 7.06 KB
/
plots.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import numpy as np
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
import seaborn
from matplotlib.patches import Patch
from matplotlib import animation
from deap import tools
def plot_cv_indices(cv, X, y, groups, ax, cmap_cv, cmap_data, lw=15):
"""Create a sample plot for indices of a cross-validation object."""
n_splits = cv.n_splits
# Generate the training/testing visualizations for each CV split
for ii, (tr, tt) in enumerate(cv.split(X=X, y=y, groups=groups)):
# Fill in indices with the training/test groups
indices = np.array([np.nan] * len(X))
indices[tt] = 1
indices[tr] = 0
# Visualize the results
ax.scatter(range(len(indices)), [ii + .5] * len(indices),
c=indices, marker='_', lw=lw, cmap=cmap_cv,
vmin=-.2, vmax=1.2)
# Plot the data classes and groups at the end
ax.scatter(range(len(X)), [ii + 1.5] * len(X),
c=groups, marker='_', lw=lw, cmap=cmap_data)
# Formatting
yticklabels = list(range(n_splits)) + ['Class']
ax.set(yticks=np.arange(n_splits+1) + .5, yticklabels=yticklabels,
xlabel='Sample index', ylabel="CV iteration",
ylim=[n_splits+1.2, -.2], xlim=[0, len(y)])
ax.set_title('{}'.format(type(cv).__name__), fontsize=15)
return ax
def plot_deviation(dataset, ax, color=None, label=None, linewidth=1):
x = np.arange(dataset.shape[1])
mean = dataset.mean(axis=0)
std = dataset.std(axis=0)
plot = ax.plot(x, mean)
fill = ax.fill_between(x, mean-std, mean+std, alpha=.2)
if color is not None:
plot[0].set_color(color)
fill.set_color(color)
if label is not None:
plot[0].set_label(label)
return [ax, plot[0], fill]
def animate(frame_index, logbook, ax):
colors = seaborn.color_palette('Set1', n_colors=4)
# colors = [ 'blue', 'red', 'green', 'purple' ]
ax.clear()
fronts = tools.emo.sortLogNondominated(logbook.select('pop')[frame_index],
len(logbook.select('pop')[frame_index]))
for i, (inds, color) in enumerate(zip(fronts, colors)):
par = [tb.evaluate(ind) for ind in inds]
df = pd.DataFrame(par)
df.plot(ax=ax, kind='scatter', label=f'Front {i+1}',
x=df.columns[0], y=df.columns[1], alpha=.45, color=color, legend=False)
patches, labels = ax.get_legend_handles_labels()
ax.legend(patches, labels, loc='lower left')
ax.set_xlim([0,1])
ax.set_ylim([-60,0])
ax.yaxis.set_major_locator(ticker.MaxNLocator(integer=True))
ticks = ax.get_yticks()
ax.set_yticklabels([int(abs(tick)) for tick in ticks])
ax.set_xlabel('Acc')
ax.set_ylabel('#Attrs')
ax.set_title(f't = {frame_index}: Pareto Front')
#ax.set_tight_layout()
return []
def plot_population_fitness(population, axes, color=None, alpha=None, sizes=None, label=None, clear=False):
"""Plot a population fitness given optional parameters"""
population_fitness = np.array([ p.fitness.values for p in population])
x = population_fitness[:, 0]
y = population_fitness[:, 1]
# FUCH
axes.set_xlim([0,1])
axes.set_ylim([-60,0])
if clear:
axes.clear()
sct = axes.scatter(x, y)
if color is not None:
sct.set_color(color)
if alpha is not None:
sct.set_alpha(alpha)
if sizes is not None:
sct.set_sizes(sizes)
if label is not None:
sct.set_label(label)
axes.yaxis.set_major_locator(ticker.MaxNLocator(integer=True))
ticks = axes.get_yticks()
axes.set_yticklabels([int(abs(tick)) for tick in ticks])
axes.set_xlabel('Acurácia')
axes.set_ylabel('#Atributos')
return sct
def plot_feature_histogram(data, axes, normalized=False):
dset = np.copy(data)
if normalized:
dset = dset/np.sum(dset)
axes.bar(np.arange(len(dset)), dset)
axes.set_xlabel('Atributo')
axes.set_title('Ocorrência de atributos')
def plot_feature_selection(data, axes):
img = axes.imshow(data, cmap='Greys', aspect='auto', origin='lower')
axes.set_xlabel('Atributo')
axes.set_ylabel('Geração')
cbar = plt.colorbar(img)
cbar.ax.set_ylabel('%Atributo')
axes.set_title('Atributos escolhidos por geração')
def plot_pareto_fronts(pop, axes, nfronts=5, showall=True):
colors = seaborn.color_palette('Set1', n_colors=nfronts)
fronts = tools.sortLogNondominated(pop, k=len(pop))
fitnesses = np.array([ ind.fitness.values for ind in pop ])
axes.scatter(fitnesses[:,0], fitnesses[:,1], color='black', sizes=[2], alpha=.5)
for i, (color, front) in reversed(list(enumerate(zip(colors, fronts)))):
fitnesses = np.array([ ind.fitness.values for ind in front ])
#axes.scatter(fitnesses[:,0], fitnesses[:,1], color=color, sizes=[6], label=f'Frente {i+1}')
axes.plot(fitnesses[:,0], fitnesses[:,1], marker='o', ms=2, color=color, label=f'Frente {i+1}')
#
#
# plt.close('all')
# fig, ax = plt.subplots(1, figsize=(6,4), dpi=150)
# for i, (color, inds) in enumerate(zip(colors, fronts)):
# par = [tb.evaluate(ind) for ind in inds]
# df = pd.DataFrame(par) #PRECISO?
# df.plot(ax=ax, kind='scatter', label=f'Front {i+1}', x=df.columns[0], y=df.columns[1], color=color)
#
# ax.yaxis.set_major_locator(ticker.MaxNLocator(integer=True))
# ticks = ax.get_yticks()
# ax.set_yticklabels([int(abs(tick)) for tick in ticks])
# plt.xlabel('Acc')
# plt.ylabel('#Attrs')
# plt.title('Pareto Front')
# plt.tight_layout()
# #plt.xlim([0,1])
# #plt.ylim([-60,0])
#
#
# #plt.legend()
#plt.close('all')
#fig = plt.figure(figsize=(6,5), dpi=150)
#ax = fig.gca()
#from matplotlib import animation
#anim = animation.FuncAnimation(fig, lambda i: animate(i, logbook, ax),
# frames=len(logbook), interval=120, blit=True)
#
#anim.save('pareto.mp4')
#
#
#
#
#
#fronts = tools.sortLogNondominated(pop, k=len(pop))
#plt.close('all')
#for ind in fronts[0]:
# plt.plot(ind.fitness.values[1], ind.fitness.values[0], 'k.', ms=3, alpha=.5)
# plt.close('all')
# for ind in pop:
# plt.plot(ind.fitness.values[1], ind.fitness.values[0], 'k.', ms=3, alpha=.5)
#
# for color, front in zip(colors, fronts):
# for i, ind in enumerate(front):
# plt.plot(ind.fitness.values[1], ind.fitness.values[0], 'o', color=color, ms=4)
#
# for ind in fronts[0]:
# plt.plot(ind.fitness.values[1], ind.fitness.values[0], 'o', color='black', ms=5)
#
# plt.ylim([0,1])
# plt.xlim([-60,0])
# plt.xlabel('#Attrs')
# plt.ylabel('Acc')
# plt.title('Pareto Front')
#
#
# plt.close('all')
# fig, ax = plt.subplots(1, figsize=(6,4), dpi=150)
# for i, (color, inds) in enumerate(zip(colors, fronts)):
# par = [tb.evaluate(ind) for ind in inds]
# df = pd.DataFrame(par) #PRECISO?
# df.plot(ax=ax, kind='scatter', label=f'Front {i+1}', x=df.columns[0], y=df.columns[1], color=color)
#
# ax.yaxis.set_major_locator(ticker.MaxNLocator(integer=True))
# ticks = ax.get_yticks()
# ax.set_yticklabels([int(abs(tick)) for tick in ticks])
# plt.xlabel('Acc')
# plt.ylabel('#Attrs')
# plt.title('Pareto Front')
# plt.tight_layout()
# #plt.xlim([0,1])
# #plt.ylim([-60,0])
#
#
# #plt.legend()