public void ReadProblem() { Problem expected = SVMUtilities.CreateTwoClassProblem(100); Problem actual = Problem.Read("train0.problem"); Assert.AreEqual(expected, actual); }
private double testMulticlassModel(int numberOfClasses, int count, SvmType svm, KernelType kernel, bool probability = false, string outputFile = null) { Problem train = SVMUtilities.CreateMulticlassProblem(numberOfClasses, count); Parameter param = new Parameter(); RangeTransform transform = RangeTransform.Compute(train); Problem scaled = transform.Scale(train); param.Gamma = 1.0 / 3; param.SvmType = svm; param.KernelType = kernel; param.Probability = probability; if (svm == SvmType.C_SVC) { for (int i = 0; i < numberOfClasses; i++) { param.Weights[i] = 1; } } Model model = Training.Train(scaled, param); Problem test = SVMUtilities.CreateMulticlassProblem(numberOfClasses, count, false); scaled = transform.Scale(test); return(Prediction.Predict(scaled, outputFile, model, false)); }
public void WriteProblem() { Problem prob = SVMUtilities.CreateTwoClassProblem(100); using (MemoryStream stream = new MemoryStream()) using (StreamReader input = new StreamReader("train0.problem")) { Problem.Write(stream, prob); string expected = input.ReadToEnd().Replace("\r\n", "\n"); string actual = Encoding.ASCII.GetString(stream.ToArray()); Assert.AreEqual(expected, actual); } }
public void ReadModel() { Problem train = SVMUtilities.CreateTwoClassProblem(100); Parameter param = new Parameter(); RangeTransform transform = RangeTransform.Compute(train); Problem scaled = transform.Scale(train); param.KernelType = KernelType.LINEAR; Training.SetRandomSeed(SVMUtilities.TRAINING_SEED); Model expected = Training.Train(scaled, param); Model actual = Model.Read("svm0.model"); Assert.AreEqual(expected, actual); }
private double testRegressionModel(int count, SvmType svm, KernelType kernel, string outputFile = null) { Problem train = SVMUtilities.CreateRegressionProblem(count); Parameter param = new Parameter(); RangeTransform transform = RangeTransform.Compute(train); Problem scaled = transform.Scale(train); param.Gamma = 1.0 / 2; param.SvmType = svm; param.KernelType = kernel; param.Degree = 2; Model model = Training.Train(scaled, param); Problem test = SVMUtilities.CreateRegressionProblem(count, false); scaled = transform.Scale(test); return(Prediction.Predict(scaled, outputFile, model, false)); }
public void WriteModel() { Problem train = SVMUtilities.CreateTwoClassProblem(100); Parameter param = new Parameter(); RangeTransform transform = RangeTransform.Compute(train); Problem scaled = transform.Scale(train); param.KernelType = KernelType.LINEAR; Training.SetRandomSeed(SVMUtilities.TRAINING_SEED); Model model = Training.Train(scaled, param); using (MemoryStream stream = new MemoryStream()) using (StreamReader input = new StreamReader("svm0.model")) { Model.Write(stream, model); string expected = input.ReadToEnd().Replace("\r\n", "\n"); string actual = Encoding.ASCII.GetString(stream.ToArray()); Assert.AreEqual(expected, actual); } }
private double testTwoClassModel(int count, SvmType svm, KernelType kernel, bool probability = false, string outputFile = null) { Problem train = SVMUtilities.CreateTwoClassProblem(count); Parameter param = new Parameter(); RangeTransform transform = RangeTransform.Compute(train); Problem scaled = transform.Scale(train); param.Gamma = .5; param.SvmType = svm; param.KernelType = kernel; param.Probability = probability; if (svm == SvmType.C_SVC) { param.Weights[-1] = 1; param.Weights[1] = 1; } Model model = Training.Train(scaled, param); Problem test = SVMUtilities.CreateTwoClassProblem(count, false); scaled = transform.Scale(test); return(Prediction.Predict(scaled, outputFile, model, false)); }