Iso-probability lines for Gaussian Processes classification (GPC)
cm
from
matplotlib
import
pyplot
as
plt
from
sklearn.gaussian_process
import
from
sklearn.gaussian_process.kernels
import
ConstantKernel
as
from
sklearn.gaussian_process.kernels
import
lim
=
8
def
g
(
x
):
"""The function to predict (classification will then consist in predicting
whether g(x) <= 0 or not)"""
return
5.0
-
x
[:,
1
]
-
0.5
*
x
[:,
0
]
**
2.0
X
=
(
[
[
-
4.61611719
,
-
6.00099547
],
[
4.10469096
,
5.32782448
],
[
0.00000000
,
-
0.50000000
],
[
-
6.17289014
,
-
4.6984743
],
[
1.3109306
,
-
6.93271427
],
[
-
5.03823144
,
3.10584743
],
[
-
2.87600388
,
6.74310541
],
[
5.21301203
,
4.26386883
],
]
)
y
=
(
g
(
X
)
>
0
,
dtype
=
int
)
kernel
=
(
0.1
,
(
1e-5
,
))
*
(
sigma_0
=
0.1
)
**
2
gp
=
(
kernel
=
kernel
)
gp
.
fit
(
X
,
y
)
print
(
"Learned kernel:
%s
"
%
gp
.
kernel_
)
res
=
50
x1
,
x2
=
(
(
-
lim
,
lim
,
res
),
(
-
lim
,
lim
,
res
))
xx
=
([
x1
.
reshape
(
x1
.
size
),
x2
.
reshape
(
x2
.
size
)])
.
T
y_true
=
g
(
xx
)
y_prob
=
gp
.
predict_proba
(
xx
)[:,
1
]
y_true
=
y_true
.
reshape
((
res
,
res
))
y_prob
=
y_prob
.
reshape
((
res
,
res
))
fig
=
(
1
)
ax
=
fig
.
gca
()
ax
.
axes
.
set_aspect
(
"equal"
)
([])
([])
ax
.
set_xticklabels
([])
ax
.
set_yticklabels
([])
(
"$x_1$"
)
(
"$x_2$"
)
cax
=
(
y_prob
,
cmap
=
cm
.
gray_r
,
alpha
=
0.8
,
extent
=
(
-
lim
,
lim
,
-
lim
,
lim
))
norm
=
plt
.
matplotlib
.
colors
.
Normalize
(
vmin
=
0.0
,
vmax
=
0.9
)
cb
=
(
cax
,
ticks
=
[
0.0
,
0.2
,
0.4
,
0.6
,
0.8
,
1.0
],
norm
=
norm
)
cb
.
set_label
(
r
"${\rm \mathbb
{P}
}\left[\widehat
{G}
(\mathbf
{x}
) \leq 0\right]$"
)
(
0
,
1
)
(
X
[
y
<=
0
,
0
],
X
[
y
<=
0
,
1
],
"r."
,
markersize
=
12
)
(
X
[
y
>
0
,
0
],
X
[
y
>
0
,
1
],
"b."
,
markersize
=
12
)
(
x1
,
x2
,
y_true
,
[
0.0
],
colors
=
"k"
,
linestyles
=
"dashdot"
)
cs
=
(
x1
,
x2
,
y_prob
,
[
0.666
],
colors
=
"b"
,
linestyles
=
"solid"
)
(
cs
,
fontsize
=
11
)
cs
=
(
x1
,
x2
,
y_prob
,
[
0.5
],
colors
=
"k"
,
linestyles
=
"dashed"
)
(
cs
,
fontsize
=
11
)
cs
=
(
x1
,
x2
,
y_prob
,
[
0.334
],
colors
=
"r"
,
linestyles
=
"solid"
)
(
cs
,
fontsize
=
11
)
()
Total running time of the script: (0 minutes 0.123 seconds)
Related examples