📄 x_valid.m
字号:
function [errors]=x_valid(data,comp_min,comp_max)%X_VALID Cross Validation% % [errors]=x_valid(data,comp_min,comp_max)%% errors[no_sets,comp_max-comp_min+1] % % data[no_data,no_var+1] the whole data avaliable% comp_min[1,1]% comp_max[1,1]%% x_valid executes, for each degree of complexity between % comp_min and comp_max, the (no_sets)Cross_Validation % for a model of the type specified in the global variable "model".% It returns a matrix whose k-th column is composed by % the errors obtained on the different training sets, % for the complexity (k-1+comp_min).%% The following global variable are required:% model% no_sets% fid_h% file_e%______________________________________________________10 April 1996___global modelglobal no_setsglobal fid_hglobal file_eno_data=size(data,1);%sets_size[no_sets,1] is a vector containing the size of each setsets_size=fix(no_data/no_sets)*ones(no_sets,1);for i=1:rem(no_data,no_sets) sets_size(i,1)=sets_size(i,1)+1;end%Permutation of the lines of the matrix datapermutation=randperm(no_data);data_perm=zeros(size(data));for i=1:no_data data_perm(i,:)=data(permutation(i),:);enddata=data_perm; select_mat=zeros(no_data,no_sets);line_counter=1;for i=1:no_sets for j=1:sets_size(i) select_mat(line_counter,i)=1; line_counter=line_counter+1; endend%select_mat[tot_samples,no_sets]%where no_sets is the number of 'folds' in a 'n-fold'-Cross_Validation:%In the matrix select_mat, one and only one element per row%equals one, all the others are zeros.%If in the i-th row, the one is in the j-th column it means%that the i-th sample belongs to the j-th test set%and to all the training sets but the j-th.%__Complexity cycle______________________________________errors=zeros(no_sets,comp_max-comp_min+1);for comp=comp_min:comp_max for s=1:no_sets fprintf(fid_h,'\nComplexity: %g on %g \t X-validation: %g on %g\n',comp,comp_max,s,no_sets); %creation of the current test and training sets temp=select_mat; temp(:,s)=zeros(size(temp,1),1); training_set=data(find(sum(temp')),:); test_set=data(find(select_mat(:,s)),:); [no_samples,c]=size(test_set); in_tst=test_set(:,1:c-1); out_tst=test_set(:,c); [out_hat]=eval([model,'(comp,training_set,in_tst)']); RMSE=sqrt(sumsqr(out_tst-out_hat)/(no_samples-1)); fprintf(fid_h,'TRUE RMSE: %g\n\n',RMSE); errors(s,comp-comp_min+1)=RMSE; eval(['save ', file_e ,' errors']); endend
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -