Ali
Ali

Reputation: 1678

CrossValidation in Accord.NET

Suppose we initiate cross validation with 10 folds to train Support Vector Machine, as per theory, every single fold will use a different model and based on the least cross validation error we'll select that Model, now according to Accord.NET framework this is what we use to implement cross validation:

var crossvalidation = new CrossValidation(size: data.Length, folds: 3);


crossvalidation.Fitting = delegate(int k, int[] indicesTrain, int[] indicesValidation)
{

// Lets now grab the training data:
var trainingInputs = data.Submatrix(indicesTrain);
var trainingOutputs = xor.Submatrix(indicesTrain);

// And now the validation data:
var validationInputs = data.Submatrix(indicesValidation);
var validationOutputs = xor.Submatrix(indicesValidation);


// Create a Kernel Support Vector Machine to operate on the set
var svm = new KernelSupportVectorMachine(new Polynomial(2), 2);

// Create a training algorithm and learn the training data
var smo = new SequentialMinimalOptimization(svm, trainingInputs, trainingOutputs);

double trainingError = smo.Run();

// Now we can compute the validation error on the validation data:
double validationError = smo.ComputeError(validationInputs, validationOutputs);

// Return a new information structure containing the model and the errors achieved.
return new CrossValidationValues(svm, trainingError, validationError);
};

and then we compute:

// Compute the cross-validation
var result = crossvalidation.Compute();

now how can the best model be extracted from these folds or on what logic the framework is working if not the previously mentioned?

Upvotes: 0

Views: 1136

Answers (2)

methus
methus

Reputation: 83

Yo can do the following with the result:

var minError = result.Models.Select(y=>y.ValidationValue).Min();
var bestModel = result.Models.Where(x=>x.ValidationValue==minError).FirstOrDefault()

First find out, what is the least error and then select the model that produced this error.

Upvotes: 0

Ali
Ali

Reputation: 1678

I am starting to think about this in other way, may be this is a good candidate for an answer, due to lack of proper documentation, unlike encog etc. may be we are supposed to select our models by ourselves and then using each selected model run the CrossValidation against it and then can use the Mean to select the appropriate model.

Upvotes: 0

Related Questions