public void TestKNN(double[][] inputs, int[] outputs, int kValue) { var crossValidation = new CrossValidation(inputs.Length, KNNCVFolds); crossValidation.Fitting = delegate(int k, int[] indicesTrain, int[] indicesValidation) { var trainingInputs = inputs.Submatrix(indicesTrain); var trainingOutputs = outputs.Submatrix(indicesTrain); // And now the validation data: var validationInputs = inputs.Submatrix(indicesValidation); var validationOutputs = outputs.Submatrix(indicesValidation); var sw = Stopwatch.StartNew(); var knn = new KNN(); knn.TrainKNN(trainingInputs, trainingOutputs, kValue); sw.Stop(); //addToTestResultBox("Training for: " + sw.ElapsedMilliseconds + "ms"); var error = knn.ComputeError(validationInputs, validationOutputs); return(new CrossValidationValues(knn, 0, error)); }; // Compute the cross-validation var result = crossValidation.Compute(); // Finally, access the measured performance. var trainingErrors = result.Training.Mean; var validationErrors = result.Validation.Mean; textBoxTestResults.AppendText("Finished with " + trainingErrors + " training errors and " + validationErrors + " validation errors" + Environment.NewLine); }
private void buttonClassify_Click(object sender, EventArgs e) { if (selectedAlgorithm.Equals(Algorithms.KNN)) { textBoxResults.AppendText("Starting classification with kNN where k is " + classK + Environment.NewLine); if (img == null) { textBoxResults.AppendText("ERROR: Please choose an image first" + Environment.NewLine); } else { var knn = new KNN(); string result; textBoxResults.AppendText("Running training function..." + Environment.NewLine); knn.TrainKNN(inputs, outputs, classK); result = knn.Classify(imageProcessor.ProcessImages(img)); textBoxResults.AppendText("Result: " + result + Environment.NewLine); } } else if (selectedAlgorithm.Equals(Algorithms.SVM)) { textBoxResults.AppendText("Starting classification with SVM" + Environment.NewLine); if (img == null) { textBoxResults.AppendText("ERROR: Please choose an image first" + Environment.NewLine); } else { var svm = new SVM(); string result; textBoxResults.AppendText("Running training function..." + Environment.NewLine); svm.TrainSVM(new RationalQuadratic(1), 3, inputs, outputs); result = svm.Classify(imageProcessor.ProcessImages(img)); textBoxResults.AppendText("Result: " + result + Environment.NewLine); } } }