Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 896
Andrea Avatar asked Jul 14 '26 14:07

Andrea


1 Answers

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.

like image 161
Engineero Avatar answered Jul 16 '26 08:07

Engineero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!