/// <summary> /// Private method that tests a fold /// </summary> /// <param name="foldDirectories">directory in which the folds are placed</param> /// <param name="fold">the current fold being tested</param> /// <returns>the classifier containing accuracy, precision and recall</returns> private SVMClassify TestFold(string[] foldDirectories, int fold) { SVMClassify classifier = new SVMClassify(); classifier.ExecuteClassifier("svm_classify.exe", Path.Combine(foldDirectories[fold], fold + ".data"), Path.Combine(foldDirectories[fold], fold + ".model"), Path.Combine(foldDirectories[fold], fold + ".output"), Path.Combine(foldDirectories[fold], fold + ".test.log.txt"), Path.Combine(foldDirectories[fold], fold + ".incorrect.txt"), false); return(classifier); }
/// <summary> /// Perform n-fold cross validation /// </summary> /// <param name="cvFolder">folder that contains the folds</param> /// <param name="silent">whether or not to be silent on the screen</param> /// <param name="learner">The svm learner with the relevant parameters</param> /// <returns>results</returns> public Results PerformCrossValidation(String cvFolder, bool silent, SVMLearn learner) { String[] foldDirectories = Directory.GetDirectories(cvFolder); double[] precisions = new double[foldDirectories.Length]; double[] recalls = new double[foldDirectories.Length]; double[] accuracies = new double[foldDirectories.Length]; for (int counter = 0; counter < foldDirectories.Length; counter++) { String concatenatedFile = Path.Combine(foldDirectories[counter], "combined" + counter + ".data"); using (StreamWriter writer = new StreamWriter(concatenatedFile, false)) { for (int innerCounter = 0; innerCounter < foldDirectories.Length; innerCounter++) { if (innerCounter != counter) { using (StreamReader reader = new StreamReader(Path.Combine(foldDirectories[innerCounter], innerCounter + ".data"))) { while (!reader.EndOfStream) { writer.WriteLine(reader.ReadLine()); } } } } } //concatenated file created. now train and test. delete concatenated file this.TrainFold(foldDirectories, counter, concatenatedFile, silent, learner); File.Delete(concatenatedFile); SVMClassify classifier = this.TestFold(foldDirectories, counter); precisions[counter] = classifier.Precision; recalls[counter] = classifier.Recall; accuracies[counter] = classifier.Accuracy; } double accuracy = accuracies.Average(), precision = precisions.Average(), recall = recalls.Average(); Console.WriteLine("Accuracy: {0}, Precision: {1}, Recall: {2}", accuracy, precision, recall); return(new Results { accuracy = accuracy, precision = precision, recall = recall }); }
/// <summary> /// Private method that tests a fold /// </summary> /// <param name="foldDirectories">directory in which the folds are placed</param> /// <param name="fold">the current fold being tested</param> /// <returns>the classifier containing accuracy, precision and recall</returns> private SVMClassify TestFold(string[] foldDirectories, int fold) { SVMClassify classifier = new SVMClassify(); classifier.ExecuteClassifier("svm_classify.exe", Path.Combine(foldDirectories[fold], fold + ".data"), Path.Combine(foldDirectories[fold], fold + ".model"), Path.Combine(foldDirectories[fold], fold + ".output"), Path.Combine(foldDirectories[fold], fold + ".test.log.txt"), Path.Combine(foldDirectories[fold], fold + ".incorrect.txt"), false); return classifier; }