-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtparbressource.py
More file actions
218 lines (184 loc) · 8.22 KB
/
Copy pathtparbressource.py
File metadata and controls
218 lines (184 loc) · 8.22 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# -*- coding: utf-8 -*-
"""
@author: J. Salmon, A. Sabourin, A. Gramfort
"""
############################################################################
# Import part
############################################################################
# from future import absolute_import, division, print_function
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from math import fmod
import seaborn as sns
############################################################################
# Data Generation
############################################################################
collist = ['blue', 'grey', 'red', 'purple', 'orange', 'salmon', 'black',
'fuchsia']
def rand_gauss(n=100, mu=[1, 1], sigma=[0.1, 0.1]):
""" Sample n points from a Gaussian variable with center mu,
and std deviation sigma
"""
d = len(mu)
res = np.random.randn(n, d)
return np.array(res * sigma + mu)
def rand_bi_gauss(n1=100, n2=100, mu1=[1, 1], mu2=[-1, -1], sigma1=[0.1, 0.1],
sigma2=[0.1, 0.1]):
""" Sample n1 and n2 points from two Gaussian variables centered in mu1,
mu2, with std deviation sigma1, sigma2
"""
ex1 = rand_gauss(n1, mu1, sigma1)
ex2 = rand_gauss(n2, mu2, sigma2)
res = np.vstack([np.hstack([ex1, 1. * np.ones((n1, 1))]),
np.hstack([ex2, 2. * np.ones((n2, 1))])])
ind = np.arange(res.shape[0])
np.random.shuffle(ind)
return np.array(res[ind, :])
def rand_tri_gauss(n1=100, n2=100, n3=100, mu1=[1, 1],
mu2=[-1, -1], mu3=[1, -1], sigma1=[0.1, 0.1],
sigma2=[0.1, 0.1], sigma3=[0.1, 0.1]):
""" Sample n1, n2 and n3 points from three Gaussian variables centered in mu1,
mu2 and mu3 with std deviation sigma1, sigma2 and sigma3
"""
ex1 = rand_gauss(n1, mu1, sigma1)
ex2 = rand_gauss(n2, mu2, sigma2)
ex3 = rand_gauss(n3, mu3, sigma3)
res = np.vstack([np.hstack([ex1, 1. * np.ones((n1, 1))]),
np.hstack([ex2, 2. * np.ones((n2, 1))]),
np.hstack([ex3, 3. * np.ones((n3, 1))])])
ind = np.arange(res.shape[0])
np.random.shuffle(ind)
return np.array(res[ind, :])
def rand_clown(n1=100, n2=100, sigma1=1, sigma2=2):
""" Sample a dataset clown with
n1 points and noise std deviation sigma1 for the first class, and
n2 points and noise std deviation sigma2 for the second one
"""
x0 = np.random.randn(n1)
x1 = x0 * x0 + sigma1 * np.random.randn(n1)
x2 = np.vstack([sigma2 * np.random.randn(n2),
sigma2 * np.random.randn(n2) + 2.])
res = np.hstack([np.vstack([[x0, x1], 1. * np.ones([1, n1])]),
np.vstack([x2, 2. * np.ones([1, n2])])]).T
ind = np.arange(res.shape[0])
np.random.shuffle(ind)
return np.array(res[ind, :])
def rand_checkers(n1=100, n2=100, n3=100, n4=100, sigma=0.1):
""" Sample n1 and n2 points from a noisy checker"""
nb1 = n1 // 8
nb2 = n2 // 8
nb3 = n3 // 8
nb4 = n4 // 8
xapp = np.reshape(np.zeros((nb1 + nb2 + nb3 + nb4) * 16),
[(nb1 + nb2 + nb3 + nb4) * 8, 2])
yapp = np.ones((nb1 + nb2 + nb3 + nb4) * 8)
idx = 0
nb = 2 * nb1
for i in range(-2, 2):
for j in range(-2, 2):
yapp[idx:(idx + nb)] = [fmod(i - j + 100, 4)] * nb
xapp[idx:(idx + nb), 0] = np.random.rand(nb)
xapp[idx:(idx + nb), 0] += i + sigma * np.random.randn(nb)
xapp[idx:(idx + nb), 1] = np.random.rand(nb)
xapp[idx:(idx + nb), 1] += j + sigma * np.random.randn(nb)
idx += nb
ind = np.arange((nb1 + nb2 + nb3 + nb4) * 8)
np.random.shuffle(ind)
res = np.hstack([xapp, yapp[:, np.newaxis]])
return np.array(res[ind, :])
############################################################################
# Displaying labeled data
############################################################################
symlist = ['o', 's', 'D', 'x', '+', '*', 'p', 'v', '-', '^']
def plot_2d(data, y=None, w=None, alpha_choice=1):
""" Plot in 2D the dataset data, colors and symbols according to the
class given by the vector y (if given); the separating hyperplan w can
also be displayed if asked"""
k = np.unique(y).shape[0]
color_blind_list = sns.color_palette("colorblind", k)
sns.set_palette(color_blind_list)
if y is None:
labs = [""]
idxbyclass = [range(data.shape[0])]
else:
labs = np.unique(y)
idxbyclass = [np.where(y == labs[i])[0] for i in range(len(labs))]
for i in range(len(labs)):
plt.scatter(data[idxbyclass[i], 0], data[idxbyclass[i], 1],
c=color_blind_list[i], s=80, marker=symlist[i])
plt.ylim([np.min(data[:, 1]), np.max(data[:, 1])])
plt.xlim([np.min(data[:, 0]), np.max(data[:, 0])])
mx = np.min(data[:, 0])
maxx = np.max(data[:, 0])
if w is not None:
plt.plot([mx, maxx], [mx * -w[1] / w[2] - w[0] / w[2],
maxx * -w[1] / w[2] - w[0] / w[2]],
"g", alpha=alpha_choice)
def plot_2d_simple(data, y=None):
if y is None:
plt.scatter(data[:, 0], data[:, 1], s=50)
else:
nY = len(y)
y = y.astype(int)
Ycol = [collist[y[i] - 1 % len(collist)] for i in range(nY)]
plt.scatter(data[:, 0], data[:, 1], c=Ycol, s=40)
############################################################################
# Displaying tools for the Frontiere
############################################################################
def frontiere(f, data, step=50):
""" trace la frontiere pour la fonction de decision f"""
xmin, xmax = data[:, 0].min() - 1., data[:, 0].max() + 1.
ymin, ymax = data[:, 1].min() - 1., data[:, 1].max() + 1.
xx, yy = np.meshgrid(np.arange(xmin, xmax, (xmax - xmin) * 1. / step),
np.arange(ymin, ymax, (ymax - ymin) * 1. / step))
z = f(np.c_[xx.ravel(), yy.ravel()])
z = z.reshape(xx.shape)
n_classes = np.unique(z).size
cmap = ListedColormap(collist[:n_classes])
plt.imshow(z, origin='lower', interpolation="nearest",
extent=[xmin, xmax, ymin, ymax], cmap=cmap)
plt.colorbar()
def frontiere_new(f, X, y, w=None, step=50, alpha_choice=1, colorbar=True,
samples=True):
""" trace la frontiere pour la fonction de decision f"""
# construct cmap
min_tot0 = np.min(X[:, 0])
min_tot1 = np.min(X[:, 1])
max_tot0 = np.max(X[:, 0])
max_tot1 = np.max(X[:, 1])
delta0 = (max_tot0 - min_tot0)
delta1 = (max_tot1 - min_tot1)
xx, yy = np.meshgrid(np.arange(min_tot0, max_tot0, delta0 / step),
np.arange(min_tot1, max_tot1, delta1 / step))
z = np.array([f(vec) for vec in np.c_[xx.ravel(), yy.ravel()]])
z = z.reshape(xx.shape)
labels = np.unique(z)
color_blind_list = sns.color_palette("colorblind", labels.shape[0])
sns.set_palette(color_blind_list)
my_cmap = ListedColormap(color_blind_list)
plt.imshow(z, origin='lower', interpolation="mitchell", alpha=0.80,
cmap=my_cmap, extent=[min_tot0, max_tot0, min_tot1, max_tot1])
if colorbar is True:
ax = plt.gca()
cbar = plt.colorbar(ticks=labels)
cbar.ax.set_yticklabels(labels)
labels = np.unique(y)
k = np.unique(y).shape[0]
color_blind_list = sns.color_palette("colorblind", k)
sns.set_palette(color_blind_list)
ax = plt.gca()
if samples is True:
for i, label in enumerate(y):
label_num = np.where(labels == label)[0][0]
plt.scatter(X[i, 0], X[i, 1], c=color_blind_list[label_num],
s=80, marker=symlist[label_num])
plt.xlim([min_tot0, max_tot0])
plt.ylim([min_tot1, max_tot1])
ax.get_yaxis().set_ticks([])
ax.get_xaxis().set_ticks([])
if w is not None:
plt.plot([min_tot0, max_tot0],
[min_tot0 * -w[1] / w[2] - w[0] / w[2],
max_tot0 * -w[1] / w[2] - w[0] / w[2]],
"k", alpha=alpha_choice)