# Note: the original version of this demo is in sklearn doc:
# http://scikit-learn.org/stable/auto_examples/gaussian_process/plot_compare_gpr_krr.html
# http://scikit-learn.org/stable/auto_examples/plot_kernel_ridge_regression.html
# Authors: Jan Hendrik Metzen <jhm@informatik.uni-bremen.de>
# License: BSD 3 clause
import time
import numpy as np
import matplotlib
matplotlib.use('svg')
import matplotlib.pyplot as plt
from sklearn.svm import SVR
from sklearn.kernel_ridge import KernelRidge
from sklearn.model_selection import GridSearchCV
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import WhiteKernel, ExpSineSquared
rng = np.random.RandomState(0)
# Generate sample data
X = 15 * rng.rand(100, 1)
y = np.sin(X).ravel()
y[::2] += rng.normal(scale = 1.0, size = X.shape[0] // 2) # add noise
# epsilon = 0.01
svr_e001 = SVR(kernel="rbf", epsilon=0.01, C=1)
svr_e001.fit(X, y)
# epsilon = 0.1
svr_e01 = SVR(kernel="rbf", epsilon=0.1, C=1)
svr_e01.fit(X, y)
# epsilon = 1.0
svr_e10 = SVR(kernel="rbf", epsilon=1.0, C=1)
svr_e10.fit(X, y)
# epsilon = 2.0
svr_e20 = SVR(kernel="rbf", epsilon=2.0, C=1)
svr_e20.fit(X, y)
X_plot = np.linspace(0, 20, 10000)[:, None]
# Predict using SVR
y_e001 = svr_e001.predict(X_plot)
y_e01 = svr_e01.predict(X_plot)
y_e10 = svr_e10.predict(X_plot)
y_e20 = svr_e20.predict(X_plot)
# Plot results
plt.figure(figsize=(10, 5))
lw = 2
plt.scatter(X, y, c='k', label='Data')
plt.plot(X_plot, np.sin(X_plot), color='navy', lw=lw, label='True')
plt.plot(X_plot, y_e001,color='brown', lw=lw, label = 'epsilon = 0.01')
plt.plot(X_plot, y_e01, color='turquoise', lw=lw,
label='epsilon = 0.1')
plt.plot(X_plot, y_e10, color='orange', lw=lw,
label='epsilon = 1.0')
plt.plot(X_plot, y_e20, color='red', lw=lw,
label='epsilon = 2.0')
plt.xlabel('data')
plt.ylabel('target')
plt.xlim(0, 20)
plt.ylim(-3, 5)
plt.title('SVR (rbf kernel) with Different Epsilons')
plt.legend(loc="best", scatterpoints=1, prop={'size': 8})
plt.savefig('svr_epsilons_demo.svg', format='svg')