Added another matlab example

This commit is contained in:
Lukas Elmer
2013-10-28 18:06:17 +01:00
parent 27566d93e2
commit 086b565488

View File

@@ -0,0 +1,31 @@
function [ error ] = cross_validation(x,y,hyper_parameter)
num_data = size(x,1);
K = 10;
indices = crossvalind('Kfold', num_data, K);
errors = zeros(K,1);
for i = 1:K
% get indices
test_idx = (indices == i);
train_idx = ~test_idx;
% get training data
x_train = x(train_idx,:);
y_train = y(train_idx,:);
% train
w = train(x_train, y_train, hyper_parameter);
% get test data
x_test = x(test_idx,:);
y_test = y(test_idx,:);
% calculate error
errors(i) = calc_cost(x_test, y_test, w, hyper_parameter); %calc_error
%errors(i) = calc_error(x_test, y_test, w);
end
error = mean(errors);
end