跳至內容

File:Regressions sine demo.svg

頁面內容不支援其他語言。
這個檔案來自維基共享資源
維基百科,自由的百科全書

原始檔案 (SVG 檔案,表面大小:900 × 450 像素,檔案大小:582 KB)


摘要

描述
English: Predictions over a perturbed sine curve with various learning models, e.g., GPR, KRR, SVR. The plot was prepared using scikit-learn.
日期
來源 自己的作品
 
向量圖形使用Matplotlib創作。
作者 Shiyu Ji

Python 3 Source Code

# 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 <[email protected]>
# 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

# Fit KernelRidge with param selection
param_grid_kr = {"alpha": [1e-1, 1e-2, 1e-3],
              "kernel": [ExpSineSquared(l, p)
                         for l in np.logspace(-2, 2, 10)
                         for p in np.logspace(0, 2, 10)]}
kr = GridSearchCV(KernelRidge(), cv=5, param_grid=param_grid_kr)
stime = time.time()
kr.fit(X, y)
print("Time for KRR fitting: %.3f" % (time.time() - stime))

# Fit GPR
gp_kernel = ExpSineSquared(1.0, 5.0, \
             periodicity_bounds=(1e-2, 1e1)) \
             + WhiteKernel(1e-1)
gpr = GaussianProcessRegressor(kernel=gp_kernel)
stime = time.time()
gpr.fit(X, y)
print("Time for GPR fitting: %.3f" % (time.time() - stime))

# Fit SVR
svr = SVR(kernel="rbf", C=1, gamma=1)
stime = time.time()
svr.fit(X, y)
print("Time for SVR fitting: %.3f" % (time.time() - stime))

# Predict using kernel ridge
X_plot = np.linspace(0, 20, 10000)[:, None]
stime = time.time()
y_kr = kr.predict(X_plot)
print("Time for KRR prediction: %.3f" % (time.time() - stime))

# Predict using Gaussian process
stime = time.time()
y_gpr = gpr.predict(X_plot, return_std=False)
print("Time for GPR prediction: %.3f" % (time.time() - stime))

stime = time.time()
y_gpr, y_std = gpr.predict(X_plot, return_std=True)
print("Time for GPR prediction with standard-deviation: %.3f"
      % (time.time() - stime))

# Predict using SVR
stime = time.time()
y_svr = svr.predict(X_plot)
print("Time for SVR prediction: %.3f" % (time.time() - stime))

# 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_svr, color='red', lw=lw, label='SVR (kernel=%s, C=%s, gamma=%s)' % (svr.get_params()['kernel'], svr.get_params()['C'], svr.get_params()['gamma']))
plt.plot(X_plot, y_kr, color='turquoise', lw=lw,
         label='KRR (%s)' % kr.best_params_)
plt.plot(X_plot, y_gpr, color='darkorange', lw=lw,
         label='GPR (%s)' % gpr.kernel_)
plt.fill_between(X_plot[:, 0], y_gpr - y_std, y_gpr + y_std, color='darkorange',
                 alpha=0.2)
plt.xlabel('data')
plt.ylabel('target')
plt.xlim(0, 20)
plt.ylim(-3, 5)
plt.title('GPR v.s. Kernel Ridge v.s. SVR')
plt.legend(loc="best",  scatterpoints=1, prop={'size': 8})

plt.savefig('regressions_sine_demo.svg', format='svg')

授權條款

我,本作品的著作權持有者,決定用以下授權條款發佈本作品:
w:zh:共享創意
姓名標示 相同方式分享
您可以自由:
  • 分享 – 複製、發佈和傳播本作品
  • 重新修改 – 創作演繹作品
惟需遵照下列條件:
  • 姓名標示 – 您必須指名出正確的製作者,和提供授權條款的連結,以及表示是否有對內容上做出變更。您可以用任何合理的方式來行動,但不得以任何方式表明授權條款是對您許可或是由您所使用。
  • 相同方式分享 – 如果您利用本素材進行再混合、轉換或創作,您必須基於如同原先的相同或兼容的條款,來分布您的貢獻成品。

說明

添加單行說明來描述出檔案所代表的內容

在此檔案描寫的項目

描繪內容

沒有維基數據項目的某些值

作者姓名字串 繁體中文 (已轉換拼寫):​Shiyu Ji
維基媒體使用者名稱 繁體中文 (已轉換拼寫):​Shiyu Ji

著作權狀態 繁體中文 (已轉換拼寫)

有著作權 繁體中文 (已轉換拼寫)

檔案來源 Chinese (Taiwan) (已轉換拼寫)

上傳者的原創作品 繁體中文 (已轉換拼寫)

多媒體型式 繁體中文 (已轉換拼寫)

image/svg+xml

檔案歷史

點選日期/時間以檢視該時間的檔案版本。

日期/時間縮⁠圖尺寸用戶備⁠註
目前2017年7月3日 (一) 19:57於 2017年7月3日 (一) 19:57 版本的縮圖900 × 450(582 KB)Shiyu JiUser created page with UploadWizard

下列頁面有用到此檔案:

全域檔案使用狀況

以下其他 wiki 使用了這個檔案:

詮釋資料