-
Notifications
You must be signed in to change notification settings - Fork 0
/
separatingFKernRLS.py
executable file
·61 lines (43 loc) · 1.77 KB
/
separatingFKernRLS.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
import numpy as np
from regularizedKernLSTest import regularizedKernLSTest
def separatingFKernRLS(c, Xtr, Ytr, kernel, sigma, Xte, Yte, axs):
'''The function classifies points evenly sampled in a visualization area,
according to the classifier Regularized Least Squares
Arguments:
c: model weights
Xtr: training input
Ytr: training output
kernel: type of kernel ('linear', 'polynomial', 'gaussian')
sigma: width of the gaussian kernel, if used
Xte: test input
Yte: test output
Example of usage:
from regularizationNetworks import MixGauss
from regularizationNetworks import separatingFKernRLS
from regularizationNetworks import regularizedKernLSTrain
import numpy as np
lam = 0.01
kernel = 'gaussian'
sigma = 1
Xtr, Ytr = MixGauss.mixgauss(np.matrix('0 1; 0 1'), np.matrix('0.5 0.25'), 100)
Xts, Yts = MixGauss.mixgauss(np.matrix('0 1; 0 1'), np.matrix('0.5 0.3'), 100)
c = regularizedKernLSTrain.regularizedkernlstrain(Xtr, Ytr, 'gaussian', sigma, lam)
separatingFKernRLS.separatingfkernrls(c, Xtr, Ytr, 'gaussian', sigma, Xts, Yts)
'''
step = 0.05
x = np.arange(Xte[:, 0].min(), Xte[:, 0].max(), step)
y = np.arange(Xte[:, 1].min(), Xte[:, 1].max(), step)
xv, yv = np.meshgrid(x, y)
xv = xv.flatten('F')
xv = np.reshape(xv, (xv.shape[0], 1))
yv = yv.flatten('F')
yv = np.reshape(yv, (yv.shape[0], 1))
xgrid = np.concatenate((xv, yv), axis=1)
ygrid = regularizedKernLSTest(c, Xtr, kernel, sigma, xgrid)
colors = [-1, +1]
cc = []
for item in Ytr:
cc.append(colors[(int(item)+1)//2])
axs.scatter(Xte[:, 0], Xte[:, 1], c=Yte, s=50)
z = np.asarray(np.reshape(ygrid, (y.shape[0], x.shape[0]), 'F'))
axs.contour(x, y, z, 1, colors='black')