Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CrossValidation
- {
- MulticlassSupportVectorMachine classifier;
- //numeroDimensiones: integer value, 95 as the number of principal components
- //numeroClases: integer value, 10, as the number of classes in the database [0-9]
- public CrossValidation(double[][] inputs, int[] outputs, out CrossValidationStatistics trainingErrors, out CrossValidationStatistics validationErrors, string path)
- {
- var crossvalidation = new CrossValidation<MulticlassSupportVectorMachine>(size: inputs.Length, folds: 10);
- crossvalidation.Fitting = delegate(int index, int []indicesTrain, int [] indicesValidation)
- {
- double trainingError = 0, validationError = 0;
- // Lets now grab the training data:
- double[][] trainingInputs = inputs.Submatrix(indicesTrain);
- int[] trainingOutputs = outputs.Submatrix(indicesTrain);
- // And now the validation data:
- double[][] validationInputs = inputs.Submatrix(indicesValidation);
- int[] validationOutputs = outputs.Submatrix(indicesValidation);
- // Create a new kernel
- //IKernel kernel = Gaussian.Estimate(trainingInputs);
- IKernel kernel = new Linear();
- // Complexity
- //var complexity = SequentialMinimalOptimization.EstimateComplexity(kernel, trainingInputs);
- // Create a new Multi-class Support Vector Machine
- var model = new MulticlassSupportVectorMachine(trainingInputs[0].Length, kernel, inputs.Distinct().Length);
- // Create the Multi-class learning algorithm for the machine
- var teacher = new MulticlassSupportVectorLearning(model, trainingInputs, trainingOutputs);
- // Configure the learning algorithm to use SMO to train the
- // underlying SVMs in each of the binary class subproblems.
- teacher.Algorithm = (svm, classInputs, classOutputs, i, j) =>
- new SequentialMinimalOptimization(svm, classInputs, classOutputs) {
- Tolerance = 0.1,
- };
- // Run the learning algorithm
- trainingError = teacher.Run();
- // Get the validation errors
- validationError = teacher.ComputeError(validationInputs, validationOutputs);
- // Return a new information structure containing the model and the errors achieved.
- return new CrossValidationValues<MulticlassSupportVectorMachine>(model, trainingError, validationError);
- };
- // Compute the cross-validation
- var result = crossvalidation.Compute();
- result.Save(path);
- // Finally, access the measured performance.
- result.Training.Tag = "Training results";
- result.Validation.Tag = "Validation results";
- trainingErrors = result.Training;
- validationErrors = result.Validation;
- var minIndex = result.Validation.Values.Find(v => v == result.Training.Values.Min()).FirstOrDefault();
- var minModelValues = result.Models[minIndex];
- this.classifier = minModelValues.Model;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement