L2約束最小二乘:高斯核模型
阿新 • • 發佈:2019-01-22
clear; clc; n = 50; N = 1000; x = linspace(-3, 3, n)'; X = linspace(-3, 3, N)'; pix = pi*x; rng('default'); y = sin(pix) ./ (pix) + 0.1*x + 0.2*randn(n, 1); x2 = x .^ 2; X2 = X .^ 2; hh = 2*0.3^2; lambda = 0.1; k = exp(-(repmat(x2, 1, n) + repmat(x2', n, 1) - 2*(x*x'))/hh); K = exp(-(repmat(X2, 1, n) + repmat(x2', N, 1) - 2*X*x')/hh); t1 = k\y; F1 = K*t1; t2 = (k^2 + lambda*eye(n))\(k*y); % L2範數約束 F2 = K*t2; plot(X, F1, 'g-', 'linewidth', 2); hold on; plot(X, F2, 'r--', 'linewidth', 2); plot(x, y, 'bo', 'linewidth', 2); axis([-2.8 2.8 -1 1.5]); hold off legend('LS','L2-Constrained LS');