Skip to content

Instantly share code, notes, and snippets.

@kvorion
Created May 25, 2011 06:49
Show Gist options
  • Save kvorion/990475 to your computer and use it in GitHub Desktop.
Save kvorion/990475 to your computer and use it in GitHub Desktop.
NetSVMLight: Cross validation for model selection
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetSVMLight;
using System.IO;
namespace NetSVMLightConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Utilities u = new Utilities();
SVMLearn[] learners = new SVMLearn[10];
Dictionary<double, Results> cvResults = new Dictionary<double, Results>(10);
String outputFolder = @"E:\kv\code\project\10foldcv";
u.ConstructNFolds(@"E:\kv\project\combined.data", 10, outputFolder, r => r.StartsWith("-"));
for (int counter = 0; counter < 10; counter++)
{
learners[counter] = new SVMLearn();
learners[counter].mode = Mode.Classification;
learners[counter].kernelType = Kernel.Linear;
learners[counter].Cost = 0.35 + counter * 0.20;
learners[counter].RemoveInconsistentTrainingExamples = true;
cvResults.Add(learners[counter].Cost, u.PerformCrossValidation(outputFolder, false,
learners[counter]));
}
StreamWriter learningRate = new StreamWriter(Path.Combine(outputFolder, "learning.txt"));
foreach (KeyValuePair<double, Results> entry in cvResults)
{
Console.WriteLine("Cost: {0}: Accuracy {1}, Precision {2}, Recall {3}",
entry.Key, entry.Value.accuracy, entry.Value.precision, entry.Value.recall);
learningRate.WriteLine(entry.Key + ", " + entry.Value.accuracy + ", " +
entry.Value.precision + ", " + entry.Value.recall);
}
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment