Search code examples
matlabsvmcross-validation

How can I choose the best model in cross validation in matlab?


I have two datasets and I want to train a SVM classification model (fitcsvm) by one of them and then predict labels for the other one. I use 10-fold cross-validation (crossval) to train my model so I have 10 different models. My question is which one of these models are the best for prediction and how can I find that? here is my code:

Mdl = fitcsvm(trainingData,labels);
CVMdl = crossval(Mdl);  

Solution

  • You may have mixed up something here. The function fitcsvm trains a single model and the function crossval validates this single model. It will then return an evaluation value.

    In general, you cannot train a model by cross-validation (as it says, it is a validation technique). However, you can use cross-validation to train good models.

    What you are looking for is a sort of hyperparameter optimization. Those are methods that automatically train multiple models on a given data set to find the best tuning values for the SVM. Have a look at the docs here

    You can turn it on like this

    Mdl = fitcsvm(trainingData,labels,'OptimizeHyperparameters','auto')
    

    You may want to use cross-validation to train multiple models with the same tuning parameters but I guess, you'll have to write this yourself. Perhaps this already helps you.