public void XORTest() { var range = Enumerable.Range(-10,16); var log2gammas = range.Select(i => Math.Pow(2, i)); var log2Cs = range.Select(i => Math.Pow(2, i + 1)); var log2Rs = range.Select(i => Math.Pow(2, i + 1)); var prob = ProblemHelper.ReadProblem(XOR_TRAINING_FILE); //Assert.IsTrue(prob.l == 4); Tuple<double, double, double, int> best = Tuple.Create(0.0, 0.0 ,0.0, prob.l); foreach (var g in log2gammas) { foreach (var c in log2Cs) { foreach (var r in log2Rs) { var svm = new C_SVC(prob, KernelHelper.SigmoidKernel(g,r), c); var errorCout = 0; for (int i = 0; i < prob.l; i++) { //var x = (prob.x[i].FirstOrDefault(xi => xi.index == 1) == null) ? 0.0 : prob.x[i].FirstOrDefault(xi => xi.index == 1).value; //var y = (prob.x[i].FirstOrDefault(xi => xi.index == 2) == null) ? 0.0 : prob.x[i].FirstOrDefault(xi => xi.index == 2).value; var z = svm.Predict(prob.x[i]); var probabilities = svm.PredictProbabilities(prob.x[i]); if (z != prob.y[i]) errorCout++; //Debug.WriteLine(String.Format("x={0} & y={1} => z={2} -- {3}", x, y, z, z == prob.y[i])); } if (errorCout < best.Item4) best = Tuple.Create(g, c, r, errorCout); //Debug.WriteLine(String.Format("g={0} && C={1} && C={2} => Error rate = {3}%", g, c, r, (double)errorCout / prob.l * 100)); } } } Debug.WriteLine(String.Format("BEST :: g={0} && C={1} && R={2} => Error rate = {3}%", best.Item1, best.Item2, best.Item3, (double)best.Item4 / (double)prob.l * 100)); }
public static void SVMPredict() { var svm = new C_SVC(prob, KernelHelper.RadialBasisFunctionKernel(gamma), C); double accuracy = svm.GetCrossValidationAccuracy(nr_fold); for (int i = 0; i < test.l; i++) { svm_node[] x = test.x[i]; double y = test.y[i]; double predict = svm.Predict(x); // returns the predicted value 'y' Dictionary<int, double> probabilities = svm.PredictProbabilities(x); // returns the probabilities for each 'y' value Console.WriteLine(predict + " :" + probabilities[1]); } Console.ReadKey(); }
/// <summary> ///Show how to predict probabilities for classification problems ///Verify that the prediction is always the most probable class ///</summary> //[TestMethod()] public void PredictTest() { var svm = new C_SVC(_prob, KernelHelper.RadialBasisFunctionKernel(gamma), C); var nb_class = _prob.y.Distinct().Count(); for (int i = 0; i < _prob.l; i++) { var x = _prob.x[i]; var y = _prob.y[i]; var probabilities = svm.PredictProbabilities(x); var predict = svm.Predict(x); Assert.IsTrue(predict == probabilities.OrderByDescending(p => p.Value).First().Key); Assert.IsNotNull(probabilities); Assert.IsTrue(probabilities.Count == nb_class); var sum = probabilities.Sum(e => e.Value) ; } }