-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
[MRG] Matern kernel #3885
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
[MRG] Matern kernel #3885
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.. _metrics_examples: | ||
|
||
Metrics | ||
------- | ||
|
||
Examples concerning the :mod:`sklearn.metrics` module. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
|
||
r""" | ||
============================================================================ | ||
Matern kernel: influence of coef0 on kernel covariance | ||
============================================================================ | ||
|
||
The example shows how the kernel covariance decreases with increasing | ||
dissimilarity of the two inputs for different values of coef0 (the parameter | ||
"nu" of the Matern kernel) | ||
|
||
See Rasmussen and Williams 2006, pp84 for details regarding the different | ||
variants of the Matern kernel. | ||
|
||
""" | ||
print(__doc__) | ||
|
||
# Author: Jan Hendrik Metzen <jhm@informatik.uni-bremen.de> | ||
# Licence: BSD 3 clause | ||
|
||
|
||
import numpy as np | ||
|
||
import matplotlib.pyplot as plt | ||
|
||
from sklearn.metrics.pairwise import matern_kernel | ||
|
||
d = np.linspace(-4, 4, 500)[:, None] | ||
|
||
for coef0 in [0.5, 1.5, 2.5, np.inf]: | ||
K = matern_kernel(d, [[0.0]], gamma=1, coef0=coef0) | ||
plt.plot(d[:, 0], K[:, 0], label=coef0) | ||
|
||
plt.xlabel("distance") | ||
plt.ylabel("covariance") | ||
plt.yscale("log") | ||
plt.ylim(1e-3, 1e0) | ||
plt.legend(title="coef0") | ||
plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
|
||
r""" | ||
============================================================================ | ||
Support Vector Regression: comparing different variants of Matern kernel | ||
============================================================================ | ||
|
||
Support Vector Regression with four different variants of the Matern kernel | ||
is compared on a (discontinuous) step-function: | ||
* The Matern kernel for coef0==1.5, learning a once differentiable function | ||
* The Matern kernel for coef0==2.5, learning a twice differentiable function | ||
* The Matern kernel for coef0==3.5, learning a three-times differentiable | ||
function | ||
* The absolute-exponential kernel which corresponds to a Matern kernel | ||
with coef0==0.5 | ||
* The squared-exponential (RBF) kernel which corresponds to a Matern kernel | ||
for the limit of coef0 becoming infinitely large | ||
|
||
See Rasmussen and Williams 2006, pp84 for details regarding the different | ||
variants of the Matern kernel. | ||
|
||
The example shows that smaller values of coef0 can better approximate the | ||
discontinuous step-function. | ||
""" | ||
print(__doc__) | ||
|
||
# Author: Jan Hendrik Metzen <jhm@informatik.uni-bremen.de> | ||
# Licence: BSD 3 clause | ||
|
||
from functools import partial | ||
|
||
import numpy as np | ||
|
||
import matplotlib.pyplot as plt | ||
|
||
from sklearn.svm import NuSVR | ||
from sklearn.metrics.pairwise import matern_kernel | ||
|
||
|
||
np.random.seed(0) | ||
|
||
# Train SVR with RBF and Matern kernels and plot resulting | ||
# predictions | ||
x = np.random.uniform(0, 10, 50) | ||
y = (x < 5) | ||
|
||
svr_rbf = NuSVR(nu=0.25, C=1e2, kernel="rbf", gamma=0.25) | ||
svr_matern0_5 = NuSVR(nu=0.25, C=1e2, | ||
kernel=partial(matern_kernel, coef0=0.5, gamma=0.25)) | ||
svr_matern1_5 = NuSVR(nu=0.25, C=1e2, | ||
kernel=partial(matern_kernel, coef0=1.5, gamma=0.25)) | ||
svr_matern2_5 = NuSVR(nu=0.25, C=1e2, | ||
kernel=partial(matern_kernel, coef0=2.5, gamma=0.25)) | ||
svr_matern3_5 = NuSVR(nu=0.25, C=1e2, | ||
kernel=partial(matern_kernel, coef0=3.5, gamma=0.25)) | ||
|
||
svr_rbf.fit(x[:, None], y) | ||
svr_matern0_5.fit(x[:, None], y) | ||
svr_matern1_5.fit(x[:, None], y) | ||
svr_matern2_5.fit(x[:, None], y) | ||
svr_matern3_5.fit(x[:, None], y) | ||
|
||
xp = np.linspace(0, 10, 100) | ||
plt.scatter(x, y, c='k', s=25, zorder=10) | ||
plt.plot(xp, xp < 5, label="True", c='k') | ||
plt.plot(xp, svr_rbf.predict(xp[:, None]), label="RBF", c='g') | ||
plt.plot(xp, svr_matern0_5.predict(xp[:, None]), label="Matern(0.5)", c='m') | ||
plt.plot(xp, svr_matern1_5.predict(xp[:, None]), label="Matern(1.5)", c='r') | ||
plt.plot(xp, svr_matern2_5.predict(xp[:, None]), label="Matern(2.5)", c='c') | ||
plt.plot(xp, svr_matern3_5.predict(xp[:, None]), label="Matern(3.5)", c='b') | ||
plt.legend(loc='best', title="kernel") | ||
plt.xlabel("input") | ||
plt.ylabel("target") | ||
plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
matplotlib import should be before sklearn