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);
        }
Ejemplo n.º 2
0
        public override void Train(IForecastingDataSets datasets)
        {
            Parameter svmpara = mParameter as Parameter;

            OnStartRunning(new ComponentRunEventArgs(datasets));
            double[] yvalue   = null;
            int      maxIndex = 0;

            Node[][] nodes   = CreateNodes(datasets, out yvalue, out maxIndex);
            Problem  problem = new Problem(nodes.Length, yvalue, nodes, maxIndex);

            mRange  = Scaling.DetermineRange(problem);
            problem = Scaling.Scale(mRange, problem);

            TrainedModel            = Training.Train(problem, svmpara as Parameter);
            datasets.ForecastedData = new double[datasets.InputData.Length][];

            for (int i = 0; i < datasets.InputData.Length; i++)
            {
                datasets.ForecastedData[i]    = new double[1];
                datasets.ForecastedData[i][0] = Forecast(datasets.InputData[i]);
                OnRunningEpoch(new ComponentRunEpochEventArgs(i));
            }

            svmpara.Count      = TrainedModel.SupportVectorCount;
            svmpara.Percentage = TrainedModel.SupportVectorCount / (double)problem.Count;

            OnFinishRunning(new ComponentRunEventArgs(datasets));
        }
        public IProblemFactory WithRangeScaling()
        {
            var original = new ProblemSource(baseDataSet).GetProblem();

            transform = RangeTransform.Compute(original);
            return(this);
        }
        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));
        }
        private void UpdateTextBox()
        {
            using (MemoryStream s = new MemoryStream()) {
                StreamWriter writer = new StreamWriter(s);
                writer.WriteLine("RangeTransform:");
                writer.Flush();
                using (MemoryStream memStream = new MemoryStream()) {
                    RangeTransform.Write(memStream, Content.RangeTransform);
                    memStream.Seek(0, SeekOrigin.Begin);
                    memStream.WriteTo(s);
                }
                writer.WriteLine("Model:");
                writer.Flush();
                using (MemoryStream memStream = new MemoryStream()) {
                    svm.svm_save_model(new StreamWriter(memStream), Content.Model);
                    memStream.Seek(0, SeekOrigin.Begin);
                    memStream.WriteTo(s);
                }
                s.Flush();
                s.Seek(0, SeekOrigin.Begin);

                StreamReader reader = new StreamReader(s);
                textBox.Text = reader.ReadToEnd();
            }
        }
Ejemplo n.º 6
0
        public void Compute()
        {
            var dataSet   = ArffDataSet.LoadSimple(Path.Combine(TestContext.CurrentContext.TestDirectory, "Data", @"problem.arff"));
            var problem   = new ProblemSource(dataSet).GetProblem();
            var transform = RangeTransform.Compute(problem);
            var result    = transform.Scale(problem);

            Assert.IsNotNull(result);
        }
 public SupportVectorMachineModel(svm_model model, RangeTransform rangeTransform, string targetVariable, IEnumerable <string> allowedInputVariables)
     : base(targetVariable)
 {
     this.name                  = ItemName;
     this.description           = ItemDescription;
     this.model                 = model;
     this.rangeTransform        = rangeTransform;
     this.allowedInputVariables = allowedInputVariables.ToArray();
 }
 private SupportVectorMachineModel(SupportVectorMachineModel original, Cloner cloner)
     : base(original, cloner)
 {
     // only using a shallow copy here! (gkronber)
     this.model                 = original.model;
     this.rangeTransform        = original.rangeTransform;
     this.allowedInputVariables = (string[])original.allowedInputVariables.Clone();
     if (original.classValues != null)
     {
         this.classValues = (double[])original.classValues.Clone();
     }
 }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 11
0
        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));
        }
Ejemplo n.º 12
0
        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));
        }
Ejemplo n.º 13
0
        /// <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);
        }
Ejemplo n.º 14
0
        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!");
        }
        internal static async Task <double> GetQualityScoreAsync(IList <double> brisqueFeatures)
        {
            var problem = new Problem(1, new double[1] {
                1
            }, ToNodes(brisqueFeatures), brisqueFeatures.Count);

            if (Transform == null)
            {
                Transform = GetRangeTransform();
            }
            if (_model == null)
            {
                _model = SVM.Model.Read(MyStreamReader.GetMemoryStream(SvmModelPath));
            }
            var scaled = Transform.Scale(problem);

            return(Math.Round(Math.Abs(_model.Predict(scaled.X[0])), 2));
        }
        private static Tuple <svm_problem, svm_problem>[] GenerateSvmPartitions(IDataAnalysisProblemData problemData, int numberOfFolds, bool shuffleFolds = true)
        {
            var folds          = GenerateFolds(problemData, numberOfFolds, shuffleFolds).ToList();
            var targetVariable = GetTargetVariableName(problemData);
            var partitions     = new Tuple <svm_problem, svm_problem> [numberOfFolds];

            for (int i = 0; i < numberOfFolds; ++i)
            {
                int p                  = i; // avoid "access to modified closure" warning below
                var trainingRows       = folds.SelectMany((par, j) => j != p ? par : Enumerable.Empty <int>());
                var testRows           = folds[i];
                var trainingSvmProblem = CreateSvmProblem(problemData.Dataset, targetVariable, problemData.AllowedInputVariables, trainingRows);
                var rangeTransform     = RangeTransform.Compute(trainingSvmProblem);
                var testSvmProblem     = rangeTransform.Scale(CreateSvmProblem(problemData.Dataset, targetVariable, problemData.AllowedInputVariables, testRows));
                partitions[i] = new Tuple <svm_problem, svm_problem>(rangeTransform.Scale(trainingSvmProblem), testSvmProblem);
            }
            return(partitions);
        }
Ejemplo n.º 17
0
        public string SaveModel(ClassifyOptions options)
        {
            options.TransformFilePath  = Path.Combine(options.ModelDir, "transform");
            options.FeaturesFileName   = Path.Combine(options.ModelDir, "features");
            options.DictionaryFileName = Path.Combine(options.ModelDir, "dictionary");
            options.CategoriesFileName = Path.Combine(options.ModelDir, "categories");

            File.WriteAllText(options.FeaturesFileName, JsonConvert.SerializeObject(features));

            File.WriteAllText(options.DictionaryFileName, JsonConvert.SerializeObject(dictionary));

            File.WriteAllText(options.CategoriesFileName, JsonConvert.SerializeObject(categories));

            RangeTransform.Write(options.TransformFilePath, transform);
            Bigtree.Algorithm.SVM.Model.Write(options.ModelFilePath, model);

            return(options.ModelFilePath);
        }
Ejemplo n.º 18
0
        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));
        }
Ejemplo n.º 19
0
        object IClassifier.LoadModel(ClassifyOptions options)
        {
            options.FeaturesFileName   = Path.Combine(options.ModelDir, "features");
            options.DictionaryFileName = Path.Combine(options.ModelDir, "dictionary");
            options.ModelFilePath      = Path.Combine(options.ModelDir, options.ModelName);
            options.TransformFilePath  = Path.Combine(options.ModelDir, "transform");
            options.CategoriesFileName = Path.Combine(options.ModelDir, "categories");

            features = JsonConvert.DeserializeObject <List <String> >(File.ReadAllText(options.FeaturesFileName));

            dictionary = JsonConvert.DeserializeObject <List <Tuple <string, int> > >(File.ReadAllText(options.DictionaryFileName));

            categories = JsonConvert.DeserializeObject <List <String> >(File.ReadAllText(options.CategoriesFileName));

            model = Bigtree.Algorithm.SVM.Model.Read(options.ModelFilePath);

            options.Transform = RangeTransform.Read(options.TransformFilePath);

            return(model);
        }
        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);
                }
        }
Ejemplo n.º 21
0
        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!");
        }
Ejemplo n.º 22
0
        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!");
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Scales a problem using the provided range.  This will not affect the parameter.
        /// </summary>
        /// <param name="prob">The problem to scale</param>
        /// <param name="range">The Range transform to use in scaling</param>
        /// <returns>The Scaled problem</returns>
        public static svm_problem Scale(this RangeTransform range, svm_problem prob)
        {
            svm_problem scaledProblem = new svm_problem()
            {
                l = prob.l, y = new double[prob.l], x = new svm_node[prob.l][]
            };

            for (int i = 0; i < scaledProblem.l; i++)
            {
                scaledProblem.x[i] = new svm_node[prob.x[i].Length];
                for (int j = 0; j < scaledProblem.x[i].Length; j++)
                {
                    scaledProblem.x[i][j] = new svm_node()
                    {
                        index = prob.x[i][j].index, value = range.Transform(prob.x[i][j].value, prob.x[i][j].index)
                    }
                }
                ;
                scaledProblem.y[i] = prob.y[i];
            }
            return(scaledProblem);
        }
    }
        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));
        }
Ejemplo n.º 25
0
        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);
        }
 public SupportVectorMachineModel(svm_model model, RangeTransform rangeTransform, string targetVariable, IEnumerable <string> allowedInputVariables, IEnumerable <double> classValues)
     : this(model, rangeTransform, targetVariable, allowedInputVariables)
 {
     this.classValues = classValues.ToArray();
 }