Andrea
Andrea

Reputation: 91

Cross validation matlab - crossval function

I do not understand what the function "crossval" in matlab takes as first parameter, I understand that it is a function for performing a regression, but I don´t get what is intended as "some criterion testval". I need to use it on a K-nn regressor, but the examples are not making everything clear to me.

vals = crossval(fun,X)

Each time it is called, fun should use XTRAIN to fit a model, then return some criterion testval computed on XTEST using that fitted model.

Here is where I am reading: Matlab reference

Upvotes: 3

Views: 2153

Answers (1)

Engineero
Engineero

Reputation: 12908

It should be similar to optimization functions, where the returned value from your fitting function fun should be an indication of how well it fits the data. As the documentation states, fun takes two arguments, a training data set XTRAIN and a testing data set XTEST.

If your data, X, comprises a column of known results X(:,1) and other columns of features X(:, 2:end), and train your data using XTRAIN, then your return value could be as simple as the sum-squared error of the fitted model:

testval = sum( (model(XTEST(:, 2:end)) - XTEST(:, 1)).^2 );

where model(XTEST(:, 2:end)) is the result of your fitted model on the features of the testing data set, XTEST, and XTEST(:, 1) are the known results for those feature sets.

Upvotes: 3

Related Questions