public void TrainAndTest(IEnumerable <ItemRating> trainSet, IEnumerable <ItemRating> testSet) { var problem = new SvmProblem() { X = trainSet.Select(ir => FeatureBuilder.GetSvmNode(ir)).ToArray(), Y = trainSet.Select(ir => LabelSelector(ir)).ToArray() }; Parameters.Check(problem); Console.WriteLine("Writing training samples..."); WriteSvmFile(problem, "train.libsvm"); Console.WriteLine("LibSvm training..."); LibSvm.SvmModel model = Svm.Train(problem, Parameters); var predictedClasses = new List <float>(); Console.WriteLine("LibSvm testing..."); foreach (var ir in testSet) { ir.PredictedRating = (float)model.Predict(FeatureBuilder.GetSvmNode(ir)); predictedClasses.Add(ir.PredictedRating); } Console.WriteLine("Writing output..."); File.WriteAllLines("output.libsvm", predictedClasses.Select(i => i.ToString())); }
public SvmTracker(SpellFigure figure) : base(0) { if (_svm == null) { _svm = new Svm(); } _figure = figure; }
public static void Run() { Console.WriteLine("OneClassDemo"); var trainData = DemoHelper.GenerateClass(0, 0.5, 0.5, 100); var parameters = new SvmParameter { SvmType = SvmType.ONE_CLASS, KernelType = KernelType.Rbf, Gamma = 0.5, Nu = 0.5, CacheSize = 128, Eps = 1e-3, Shrinking = true, Probability = false }; var problem = new SvmProblem { Y = trainData.Select(p => 1.0).ToArray(), X = trainData.Select(p => p.ToSvmNodes()).ToArray() }; parameters.Check(problem); var model = Svm.Train(problem, parameters); var x = new Point(0.9, 0.9).ToSvmNodes(); var resx = model.Predict(x); Console.WriteLine(resx); var y = new Point(0.5, 0.5).ToSvmNodes(); var resy = model.Predict(y); Console.WriteLine(resy); var z = new Point(0.45, 0.45).ToSvmNodes(); var resz = model.Predict(z); Console.WriteLine(resz); }
public IEnumerable <Label> Label(double[] input) { var labels = Svm .Classify(input) .Select(c => new Label(Labels[c.Number], c.Score)); foreach (var filter in MangledName.Filters) { if (filter.IsPercent) { labels = labels.Where(l => l.Score >= filter.Value / 100d); } else { labels = labels.Take(filter.Value); } } return(labels); }
public static void Run() { Console.WriteLine("EpsSVRDemo"); var rnd = new Random(); var trainData = DemoHelper.Range(-10.0, 10.01, 0.1).Select(val => new { X = val, Y = DemoHelper.Sinc(val) + (rnd.NextDouble() - 0.5) / 4 }); var parameters = new SvmParameter { SvmType = SvmType.EPSILON_SVR, KernelType = KernelType.Rbf, Gamma = 0.5, CacheSize = 128, C = 1, Eps = 1e-3, P = 0.1, Shrinking = true, Probability = false }; var problem = new SvmProblem { Y = trainData.Select(p => p.Y).ToArray(), X = trainData.Select(p => p.X.ToSvmNodes()).ToArray() }; parameters.Check(problem); var model = Svm.Train(problem, parameters); foreach (var item in DemoHelper.Range(-1.0, 1.01, 0.1)) { var x = item.ToSvmNodes(); var yPred = model.Predict(x); var yReal = DemoHelper.Sinc(item); Console.WriteLine("x: {0}", item); Console.WriteLine("y_real: {0}", yReal); Console.WriteLine("y_pred: {0}", yPred); Console.WriteLine(); } }
public static void Run() { Console.WriteLine("CSVMDemo"); var class1 = DemoHelper.GenerateClass(0, 0.1, 0.1, 50); var class2 = DemoHelper.GenerateClass(1, 0.8, 0.8, 50); var trainData = class1.Concat(class2); var parameters = new SvmParameter { SvmType = SvmType.C_SVC, KernelType = KernelType.Rbf, Gamma = 0.5, CacheSize = 128, C = 1, Eps = 1e-3, Shrinking = true, Probability = false }; var problem = new SvmProblem { Y = trainData.Select(p => (double)p.Label).ToArray(), X = trainData.Select(p => p.ToSvmNodes()).ToArray() }; parameters.Check(problem); var model = Svm.Train(problem, parameters); var x = new Point(0.9, 0.9).ToSvmNodes(); var resx = model.Predict(x); Console.WriteLine(resx); var y = new Point(0.1, 0.1).ToSvmNodes(); var resy = model.Predict(y); Console.WriteLine(resy); }
private static void Main() { // ランダムにデータを作成 var r = new Random(); const int trainCount = 500; const int testCount = 100; var temp = Path.GetTempPath(); var trainDic = new StringBuilder(); var testDic = new StringBuilder(); var testDicAns = new List <int>(); for (var l = 0; l < 10; l++) { for (var i = 0; i < trainCount; i++) { // 0 以上 10未満の小数を生成 var v = r.NextDouble() + l; trainDic.AppendLine($"{l} 1:{v}"); } for (var i = 0; i < testCount; i++) { // 0 以上 10未満の小数を生成 var v = r.NextDouble() + l; testDic.AppendLine($"{l} 1:{v}"); testDicAns.Add(l); } } var tempTrainPath = Path.Combine(temp, "train"); var tempTestPath = Path.Combine(temp, "test"); using (var fs = new FileStream(tempTrainPath, FileMode.Create, FileAccess.Write, FileShare.Write)) using (var sw = new StreamWriter(fs, Encoding.ASCII)) sw.Write(trainDic.ToString()); using (var fs = new FileStream(tempTestPath, FileMode.Create, FileAccess.Write, FileShare.Write)) using (var sw = new StreamWriter(fs, Encoding.ASCII)) sw.Write(testDic.ToString()); // データセットの読み込み var train = Svm.ReadProblem(tempTrainPath); var test = Svm.ReadProblem(tempTestPath); // パラメータの設定 var param = new Parameter(); param.SvmType = SvmType.C_SVC; param.KernelType = KernelType.RBF; param.Gamma = 0.05d; param.C = 5d; param.CacheSize = 100; param.Degree = 3; param.Coef0 = 0; param.Nu = 0.5; param.Eps = 1e-3; param.p = 0.1; param.Shrinking = 1; param.Probability = 0; param.WeightLabel = new int[0]; param.Weight = new double[0]; var message = Svm.CheckParameter(train, param); if (!string.IsNullOrWhiteSpace(message)) { Console.WriteLine($"Error: {message} for train problem"); return; } message = Svm.CheckParameter(test, param); if (!string.IsNullOrWhiteSpace(message)) { Console.WriteLine($"Error: {message} for test problem"); return; } // 学習データとパラメータから学習 var model = Svm.Train(train, param); var correct = 0; var total = 0; for (var i = 0; i < test.Count; i++) { // 学習データのベクトル var x = test.x[i]; // 分類結果 (ラベルが返ってくる) var ret1 = (int)Svm.Predict(model, x); if (ret1 == testDicAns[i]) { correct++; } total++; } Console.WriteLine($"Accuracy: {correct / (double)total * 100}%"); }