8000 EXAMPLE Added illustration of Matern kernel for different values of c… · scikit-learn/scikit-learn@002d3f2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 002d3f2

Browse files
author
Jan Hendrik Metzen
committed
EXAMPLE Added illustration of Matern kernel for different values of coef0
1 parent 300a661 commit 002d3f2

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

examples/metrics/README.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.. _metrics_examples:
2+
3+
Metrics
4+
-------
5+
6+
Examples concerning the :mod:`sklearn.metrics` module.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
r"""
5+
============================================================================
6+
Matern kernel: influence of coef0 on kernel covariance
7+
============================================================================
8+
9+
The example shows how the kernel covariance decreases with increasing
10+
dissimilarity of the two inputs for different values of coef0 (the parameter
11+
"nu" of the Matern kernel)
12+
13+
See Rasmussen and Williams 2006, pp84 for details regarding the different
14+
variants of the Matern kernel.
15+
16+
"""
17+
print(__doc__)
18+
19+
# Author: Jan Hendrik Metzen <jhm@informatik.uni-bremen.de>
20+
# Licence: BSD 3 clause
21+
22+
23+
import numpy as np
24+
25+
from sklearn.metrics.pairwise import matern_kernel
26+
27+
import matplotlib.pyplot as plt
28+
29+
d = np.linspace(-4, 4, 500)[:, None]
30+
31+
for coef0 in [0.5, 1.5, 2.5, np.inf]:
32+
K = matern_kernel(d, [[0.0]], gamma=1, coef0=coef0)
33+
plt.plot(d[:, 0], K[:, 0], label=coef0)
34+
35+
plt.xlabel("distance")
36+
plt.ylabel("covariance")
37+
plt.yscale("log")
38+
plt.ylim(1e-3, 1e0)
39+
plt.legend(title="coef0")
40+
plt.show()

examples/svm/plot_svm_matern_kernel.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
is compared on a (discontinuous) step-function:
1111
* The Matern kernel for coef0==1.5, learning a once differentiable function
1212
* The Matern kernel for coef0==2.5, learning a twice differentiable function
13+
* The Matern kernel for coef0==3.5, learning a three-rimes differentiable
14+
function
1315
* The absolute-exponential kernel which corresponds to a Matern kernel
1416
with coef0==0.5
1517
* The squared-exponential (RBF) kernel which corresponds to a Matern kernel
@@ -58,15 +60,14 @@
5860
svr_matern3_5.fit(x[:, None], y)
5961

6062
xp = np.linspace(0, 10, 100)
61-
plt.figure(0)
6263
plt.scatter(x, y, c='k', s=25, zorder=10)
6364
plt.plot(xp, xp < 5, label="True", c='k')
6465
plt.plot(xp, svr_rbf.predict(xp[:, None]), label="RBF", c='g')
6566
plt.plot(xp, svr_matern0_5.predict(xp[:, None]), label="Matern(0.5)", c='m')
6667
plt.plot(xp, svr_matern1_5.predict(xp[:, None]), label="Matern(1.5)", c='r')
6768
plt.plot(xp, svr_matern2_5.predict(xp[:, None]), label="Matern(2.5)", c='c')
6869
plt.plot(xp, svr_matern3_5.predict(xp[:, None]), label="Matern(3.5)", c='b')
69-
plt.legend(loc='best')
70+
plt.legend(loc='best', title="kernel")
7071
plt.xlabel("input")
7172
plt.ylabel("target")
7273
plt.show()

0 commit comments

Comments
 (0)
0