[PR & ML 2] [Introduction] Example: Polynomial Curve Fitting

时间:2023-03-10 06:34:33
[PR & ML 2] [Introduction] Example: Polynomial Curve Fitting

啊啊啊,竟然不支持latex,竟然HTML代码不能包含javascript,代码编辑器也不支持Matlab!!!我要吐槽博客的编辑器。。。T_T只能贴图凑合看了,代码不是图,但这次为了省脑细胞,写的不简洁,凑合看吧。。。

[PR & ML 2] [Introduction] Example: Polynomial Curve Fitting

[PR & ML 2] [Introduction] Example: Polynomial Curve Fitting

[PR & ML 2] [Introduction] Example: Polynomial Curve Fitting

[PR & ML 2] [Introduction] Example: Polynomial Curve Fitting

numPoints = ;
lnlambda = [-Inf - ];
M = ; % [, , , ];
x = linspace(,);
% gt data for plotting
t = sin(*pi*x);
ttest = t + normrnd(,0.2, size(x)); % generate observed data
if exist('C0_Curve_Fitting_data.mat', 'file')
load('C0_Curve_Fitting_data.mat');
else
xob = :/(numPoints-):;
tob = sin(*pi*xob) + normrnd(,0.2, size(xob));
save('C0_Curve_Fitting_data.mat', 'xob', 'tob');
end ERMStest = zeros(size(M));
ERMStrain = zeros(size(M));
figCount = ;
% for i = :length(M)
i = ;
for l = :length(lnlambda)
Aob = [];
A = [];
for j = :M(i)
Aob = [Aob (xob.^j)']; %#ok<AGROW>
A = [A (x.^j)']; %#ok<AGROW>
end w = (Aob'*Aob+eye(M(i)+1)*exp(lnlambda(l)))\(Aob'*tob');
tpred = A*w; ERMStest(i) = sqrt(2*0.5*sum((tpred' - ttest).^)/length(x));
ERMStrain(i) = sqrt(*0.5*sum((Aob*w - tob').^2)/length(x));
figure(3);
if M(i)==0 || M(i)==1 || M(i)==3 || M(i)==9
disp('w=')
disp(w);
figCount = figCount + 1;
subplot(1, 3, figCount);
plot(xob,tob,'or');
hold on;
plot(x,t,'b');
plot(x,tpred, 'g')
hold off;
text(0.6,1,['M = ' num2str(M(i))],'FontSize',14);
text(0.6,1.3,['ln\lambda = ' num2str(lnlambda(l))], 'FontSize',14);
xlabel('x');
ylabel('t');
set(gca,'FontSize',14)
end
end % figure(2)
% h1 = plot(M,ERMStest,'go-', 'LineWidth', 2);
% hold on;
% h2 = plot(M,ERMStrain,'bo-', 'LineWidth', 2);
% hold off;
% legend([h1, h2], 'Test', 'Training')
% set(gca,'FontSize',14)