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 static void Run(IRegressionProblemData problemData, IEnumerable <string> allowedInputVariables, string svmType, string kernelType, double cost, double nu, double gamma, double epsilon, int degree, out ISupportVectorMachineModel model, out int nSv) { var dataset = problemData.Dataset; string targetVariable = problemData.TargetVariable; IEnumerable <int> rows = problemData.TrainingIndices; svm_parameter parameter = new svm_parameter { svm_type = GetSvmType(svmType), kernel_type = GetKernelType(kernelType), C = cost, nu = nu, gamma = gamma, p = epsilon, cache_size = 500, probability = 0, eps = 0.001, degree = degree, shrinking = 1, coef0 = 0 }; svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows); RangeTransform rangeTransform = RangeTransform.Compute(problem); svm_problem scaledProblem = rangeTransform.Scale(problem); var svmModel = svm.svm_train(scaledProblem, parameter); nSv = svmModel.SV.Length; model = new SupportVectorMachineModel(svmModel, rangeTransform, targetVariable, allowedInputVariables); }
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)); }
private IEnumerable <double> GetEstimatedValuesHelper(IDataset dataset, IEnumerable <int> rows) { // calculate predictions for the currently requested rows svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows); svm_problem scaledProblem = rangeTransform.Scale(problem); for (int i = 0; i < problem.l; i++) { yield return(svm.svm_predict(Model, scaledProblem.x[i])); } }
public static void Run(IClassificationProblemData problemData, IEnumerable <string> allowedInputVariables, int svmType, int kernelType, double cost, double nu, double gamma, int degree, out ISupportVectorMachineModel model, out int nSv) { var dataset = problemData.Dataset; string targetVariable = problemData.TargetVariable; IEnumerable <int> rows = problemData.TrainingIndices; svm_parameter parameter = new svm_parameter { svm_type = svmType, kernel_type = kernelType, C = cost, nu = nu, gamma = gamma, cache_size = 500, probability = 0, eps = 0.001, degree = degree, shrinking = 1, coef0 = 0 }; var weightLabels = new List <int>(); var weights = new List <double>(); foreach (double c in problemData.ClassValues) { double wSum = 0.0; foreach (double otherClass in problemData.ClassValues) { if (!c.IsAlmost(otherClass)) { wSum += problemData.GetClassificationPenalty(c, otherClass); } } weightLabels.Add((int)c); weights.Add(wSum); } parameter.weight_label = weightLabels.ToArray(); parameter.weight = weights.ToArray(); svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows); RangeTransform rangeTransform = RangeTransform.Compute(problem); svm_problem scaledProblem = rangeTransform.Scale(problem); var svmModel = svm.svm_train(scaledProblem, parameter); nSv = svmModel.SV.Length; model = new SupportVectorMachineModel(svmModel, rangeTransform, targetVariable, allowedInputVariables, problemData.ClassValues); }
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 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)); }
public double[][] Predict(Sentence sentence, ClassifyOptions options) { Problem predict = new Problem(); predict.X = GetData(new List <Sentence> { sentence }).ToArray(); predict.Y = new double[1]; predict.Count = predict.X.Count(); predict.MaxIndex = features.Count; transform = options.Transform; Problem scaled = transform.Scale(predict); return(Prediction.PredictLabelsProbability(model, scaled)); }
public double[][] Predict(FeaturesWithLabel featureSet, ClassifyOptions options) { Problem predict = new Problem(); List <FeaturesWithLabel> featureSets = new List <FeaturesWithLabel>(); featureSets.Add(featureSet); predict.X = GetData(featureSets).ToArray(); predict.Y = new double[1]; predict.Count = predict.X.Count(); predict.MaxIndex = 300; RangeTransform transform = options.Transform; Problem scaled = transform.Scale(predict); return(Prediction.PredictLabelsProbability(options.Model, scaled)); }
/// <summary> /// Начать процес обучения /// </summary> public void DoWork() { Problem problem = new Problem(VectorList.Count, TypeElementVectorList.ToArray(), VectorList.ToArray(), VectorList[0].Length); range = RangeTransform.Compute(problem); problem = range.Scale(problem); // константы подобраны методом тыка. работают оптимально Parameter param = new Parameter(); param.C = 2; param.Gamma = .5; model = Training.Train(problem, param); }
public void SVMClassifierTrain(List <Sentence> sentences, ClassifyOptions options, SvmType svm = SvmType.C_SVC, KernelType kernel = KernelType.RBF, bool probability = true, string outputFile = null) { var tfidf = new TfIdfFeatureExtractor(); tfidf.Dimension = options.Dimension; tfidf.Sentences = sentences; tfidf.CalBasedOnCategory(); featuresInTfIdf = tfidf.Keywords(); // copy test multiclass Model Problem train = new Problem(); train.X = GetData(sentences, options).ToArray(); train.Y = GetLabels(sentences).ToArray(); train.Count = train.X.Count(); train.MaxIndex = train.X[0].Count();//int.MaxValue; Parameter param = new Parameter(); transform = RangeTransform.Compute(train); Problem scaled = transform.Scale(train); param.Gamma = 1.0 / 3; param.SvmType = svm; param.KernelType = kernel; param.Probability = probability; int numberOfClasses = train.Y.OrderBy(x => x).Distinct().Count(); if (numberOfClasses == 1) { Console.Write("Number of classes must greater than one!"); } if (svm == SvmType.C_SVC) { for (int i = 0; i < numberOfClasses; i++) { param.Weights[i] = 1; } } model = Training.Train(scaled, param); Console.Write("Training finished!"); }
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); } }
public void SVMClassifierTrain(List <Sentence> sentences, ClassifyOptions options, SvmType svm = SvmType.C_SVC, KernelType kernel = KernelType.RBF, bool probability = true, string outputFile = null) { // copy test multiclass Model Problem train = new Problem(); train.X = GetData(sentences).ToArray(); train.Y = GetLabels(sentences).ToArray(); train.Count = train.X.Count(); train.MaxIndex = train.X[0].Count();//int.MaxValue; Parameter param = new Parameter(); transform = RangeTransform.Compute(train); Problem scaled = transform.Scale(train); param.Gamma = 1.0 / 3; param.SvmType = svm; param.KernelType = kernel; param.Probability = probability; int numberOfClasses = train.Y.OrderBy(x => x).Distinct().Count(); if (numberOfClasses == 1) { throw new ArgumentException("Number of classes can't be one!"); } if (svm == SvmType.C_SVC) { for (int i = 0; i < numberOfClasses; i++) { param.Weights[i] = 1; } } model = Training.Train(scaled, param); Console.Write("Training finished!"); }
public void SVMClassifierTrain(List <FeaturesWithLabel> featureSets, ClassifyOptions options, SvmType svm = SvmType.C_SVC, KernelType kernel = KernelType.RBF, bool probability = true, string outputFile = null) { // copy test multiclass Model Problem train = new Problem(); train.X = GetData(featureSets).ToArray(); train.Y = GetLabels(featureSets).ToArray(); train.Count = train.X.Count(); train.MaxIndex = 300;//int.MaxValue; 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; int numberOfClasses = train.Y.Distinct().Count(); if (numberOfClasses == 1) { throw new ArgumentException("Number of classes can't be one!"); } if (svm == SvmType.C_SVC) { for (int i = 0; i < numberOfClasses; i++) { param.Weights[i] = 1; } } var model = Training.Train(scaled, param); RangeTransform.Write(options.TransformFilePath, transform); SVM.BotSharp.MachineLearning.Model.Write(options.ModelFilePath, model); Console.Write("Training finished!"); }
void classify(object args) { Problem train = new Problem { X = _data.Select(o => new Node[] { new Node(1, o.Position.X), new Node(2, o.Position.Y) }).ToArray(), Y = _data.Select(o => o.Label).ToArray(), Count = _data.Count, MaxIndex = 2 }; Parameter param = args as Parameter; RangeTransform transform = RangeTransform.Compute(train); train = transform.Scale(train); Model model = Training.Train(train, param); int width = (int)plot.ActualWidth; int height = (int)plot.ActualHeight; byte[] pixels = new byte[width * height * 3]; int cWidth = (width >> SCALE) + 1; int cHeight = (height >> SCALE) + 1; int[,] labels = new int[cHeight, cWidth]; for (int r = 0, i = 0; r < cHeight; r++) { for (int c = 0; c < cWidth; c++, i++) { int rr = r << SCALE; int cc = c << SCALE; Node[] datum = new Node[] { new Node(1, cc), new Node(2, rr) }; datum = transform.Transform(datum); labels[r, c] = (int)model.Predict(datum); classifyPB.Dispatcher.Invoke(() => classifyPB.Value = (i * 100) / (cHeight * cWidth)); } } PixelFormat format = PixelFormats.Rgb24; for (int i = 0, r = 0; r < height; r++) { for (int c = 0; c < width; c++) { int label = labels[r >> SCALE, c >> SCALE]; Color color = _colors[label]; pixels[i++] = color.R; pixels[i++] = color.G; pixels[i++] = color.B; } } plot.Dispatcher.Invoke(() => { ImageBrush brush = new ImageBrush(BitmapSource.Create(width, height, 96, 96, format, null, pixels, width * 3)); brush.Stretch = Stretch.None; brush.AlignmentX = 0; brush.AlignmentY = 0; plot.Background = brush; }); classifyPB.Dispatcher.Invoke(() => classifyPB.Value = 0); }