Matlab学习笔记——(2)

Matlab学习笔记——(2)_第1张图片

load spectra_data.mat
axis equal
plot(NIR')

%随机产生训练集和测试集
temp=randperm(size(NIR,1));
%训练集
P_train=NIR(temp(1:50),:)';
T_train=octane(temp(1:50),:)';
%测试集
P_test=NIR(temp(51:end),:)';
T_test=octane(temp(51:end),:)';

N=size(P_test,2);

[p_train,ps_input]=mapminmax(P_train,0,1);
p_test=mapminmax('apply',P_test,ps_input);

[t_train,ps_output]=mapminmax(T_train,0,1);

net=newff(p_train,t_train,9);

net.trainParam.epochs=1000;
net.trainParam.goal=1e-8;
net.trainParam.lr=0.01;

net=train(net,p_train,t_train);

t_sim=sim(net,p_test);
T_sim=mapminmax('reverse',t_sim,ps_output);


error=abs(T_sim-T_test)./T_test;

R2 = (N * sum(T_sim .* T_test) - sum(T_sim) * sum(T_test))^2 / ((N * sum((T_sim).^2) - (sum(T_sim))^2) * (N * sum((T_test).^2) - (sum(T_test))^2));
result=[T_test' T_sim' error'];


figure
plot(1:N,T_test,'b:x',1:N,T_sim,'r-o')
legend('真实值','预测值');
xlabel('预测样本')
ylabel('辛烷值')
string={
     '测试集辛烷值含量预测结果对比';['R^2=' num2str(R2)]};
title(string);

mapminmax函数的官方解释

[Y,PS] = mapminmax(X,YMIN,YMAX)
[Y,PS] = mapminmax(X,FP)
Y = mapminmax('apply',X,PS)
X = mapminmax('reverse',Y,PS)
dx_dy = mapminmax('dx_dy',X,Y,PS)

可用于归一化及反归一化

你可能感兴趣的:(matlab,机器学习,监督学习)