cross validation for Bayesian regression model

Hello everyone! I have obtained an NN model for the prediction of properties, now I want to test cross-validation for my Bayesian regression model, but I am afraid that the code I have used is not entirely correct, since at no time does it ask me for the model to be evaluated. Could someone explain to me if the code is correct and how to interpret the result?
data = readtable('data.csv');
outputdata = readtable('var.csv');
st = table2array(data(:,:));
inputs = table2array(outputdata(:,:));
%Fgroups=Finalinputs(:,1); %read the composite class column from the data
k=5; %number of k-fold cross-validations
cv=cvpartition(449,'Leaveout'); %this cvpartition function does the division into 10 randomly chosen arrays considering the various composite classes
perfresults=zeros(8,k); %this is to initialize the matrix where the performance (R, MSE, MAPE) is saved for all k-folds
x = inputs'; %read the inputs, note that the data was read from the csv file as a "cell array" variable
t = st';
%n is the number of data = 325 y k=n
%%
for i = 1:cv.NumTestSets % for fold=1 to k where cv.NumTestSets=k
testindex=zeros(cv.TestSize(i),1);
trainindex=zeros(cv.TrainSize(i),1);
ntest=1;
ntrain=1;
trainIdx = cv.training(i);
testIdx = cv.test(i);
for j=1:cv.N
if testIdx(j) == 1
testindex(ntest)=j;
ntest=ntest+1;
else
trainindex(ntrain)=j;
ntrain=ntrain+1;
end
end
% Choose a Training Function
trainFcn = 'trainbr'; % Bayesian Regularization backpropagation.
% Create a Fitting Network
hiddenLayerSize = 4;
net = fitnet(hiddenLayerSize,trainFcn);
% Choose Input and Output Pre/Post-Processing Functions
% For a list of all processing functions type: help nnprocess
net.input.processFcns = {'removeconstantrows','mapminmax'};
net.output.processFcns = {'removeconstantrows','mapminmax'};
net.divideFcn = 'divideind'; % Divide data according to index
net.divideMode = 'sample'; % Divide up every sample
net.divideParam.trainInd = trainindex;
%net.divideParam.valInd = 221:257;
net.divideParam.testInd = testindex;
% Choose a Performance Function
% For a list of all performance functions type: help nnperformance
net.performFcn = 'mse'; % Mean Squared Error
% Choose Plot Functions
% For a list of all plot functions type: help nnplot
%net.plotFcns = {'plotperform','plottrainstate','ploterrhist', ...
%'plotregression', 'plotfit'};
% Train the Network
[net,tr] = train(net,x,t);
% Test the Network
y = net(x);
e = gsubtract(t,y);
performance = perform(net,t,y);
% Recalculate Training, Validation and Test Performance
trainTargets = t .* tr.trainMask{1};
valTargets = t .* tr.valMask{1};
testTargets = t .* tr.testMask{1};
trainPerformance = perform(net,trainTargets,y)
valPerformance = perform(net,valTargets,y)
testPerformance = perform(net,testTargets,y)
% get the predicted output and target for trainin and test sets
trOut=y(tr.trainInd);
tstOut=y(tr.testInd);
trTarget=trainTargets(tr.trainInd);
tstTarget=testTargets(tr.testInd);
% calculate correlation and mape
rtest=corrcoef(tstTarget,tstOut);
rtrain=corrcoef(trTarget,trOut);
rall=corrcoef(t,y);
mapetest=mean(abs(e(tr.testInd)./tstTarget));
%mapetest=0;
mape=mean(abs(e./t));
% fill the perfresults matrix for fold i
%perfresults(:,i)=[rtrain(1,2); rtest(1,2); rall(1,2); trainPerformance; testPerformance; performance; mapetest; mape];
perfresults(:,i)=[rtrain(1,2); rtest; rall(1,2); trainPerformance; testPerformance; performance; mapetest; mape];
%weights=getwb(net);
%pred=y';
end
%Guardar los resultados en un archivo '.mat'
save('corrida4_trainbr', 'perfresults')

回答(1 个)

Hi Rajanigandha, have you tried using phyton? I can help you but to be honest I'm not good at matlab. Years ago I tried to do the same but it's complicated, even I'd say impossible to do what you want

产品

版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by