调试新的crossval函数:MATLAB

时间:2014-09-05 08:40:49

标签: matlab debugging cross-validation

我自己编写了一个crossval()函数,因为crossval()上的MATLAB并不能真正让我尝试不同的(自定义)丢失/错误措施。然而,我得到的loss_estimate似乎非常小,我无法弄清楚这里出了什么问题。

我使用的代码如下:

function Loss_est = CV(fun1,fun2,data,k)
K = crossvalindex(data,k);
Loss = zeros(size(K,2),1);
for j1 = 1:size(K,2)
    Xtest = K{j1};
    Xtest = [ones(size(Xtest,1),1) Xtest(:,1:end-1)];
    ytest = Xtest(:,end);
    K1 = K;
    K1{j1}=[];
    Xtrain = cell2mat(K1(1,:)');
    Xtrain = [ones(size(Xtrain,1),1) Xtrain(:,1:end-1) ]; 
    ytrain = Xtrain(:,end);
    ynew = Xtest*fun1(ytrain,Xtrain);
    Loss(j1,1) = fun2(ynew,ytest);
end
Loss(isnan(Loss))=[];
Loss_est = mean(Loss);

这里,corssvalindex函数在随机化数据行之后获取数据并将数据分区为单元格。

%% Temp_crossvalidation indexing
function [K,edges] = crossvalindex(data,k)
r = size(data,1);
id = randperm(r);
data_1 = data(id,:);

% Edges
edges = 1:round(r/k):r+1;
if numel(edges) < k+1
    edges = [edges r+1];
end

% Index-ed and partition
[~,idx] = histc(id,edges);
K = cell(1,numel(edges-1));
for i = 1:numel(edges-1)
    K{i} = data_1(idx == i,:);
end

end

损失估计似乎太小了。关于什么是错的任何想法?

data = randi(1000,100,10);
fun1 = @(x,y) regress(x,y);
fun2 = @(x,y) sum(abs(y-x));

k = 10;
Loss_est = CV(fun1,fun2,data,k);
Loss_est =

   5.4346e-26

0 个答案:

没有答案