예제 #1
0
        public double buildSVMTestCorpus(string filename)
        {
            double total = 0, tp = 0;
            string trainDataPath = filename + "SimpleTrainSVM.txt";

            if (File.Exists(trainDataPath))
            {
                _test = ProblemHelper.ReadProblem(trainDataPath);
                _test = ProblemHelper.ScaleProblem(_test);
                svm_node[][] sn = _test.x;
                total = sn.Length;
                double[] lbls = _test.y;
                for (int i = 0; i < sn.Length; i++)
                {
                    if (_test.y[i] == svm.Predict(sn[i]))
                    {
                        tp++;
                    }
                }
                fileExistance = true;
                //ProblemHelper.WriteProblem(filename+"TestSVM.txt", _test);
            }
            else
            {
                SVMScale readyData = new SVMScale();
                readyData.buildSVMCorpus(filename);
                readyData.scaleSVMData(filename);
                buildSVMTestCorpus(filename);
            }
            return((tp / total) * 100);
        }
        /// <summary>
        ///Test pour ScaleProblem
        ///</summary>
        //[TestMethod()]
        public void ScaleProblemTest()
        {
            string full_path = System.IO.Path.Combine(base_path, TEST_FILE);
            var    prob      = ProblemHelper.ScaleProblem(ProblemHelper.ReadProblem(full_path));

            Assert.IsNotNull(prob);
            Assert.IsTrue(prob.x.Max(v => v.Max(n => n.value)) == 1.0);
            Assert.IsTrue(prob.x.Min(v => v.Min(n => n.value)) == -1.0);
        }
예제 #3
0
        public bool buildSVMCorpus(string filename)
        {
            string trainDataPath = filename + "TrainSVM.txt";

            if (File.Exists(trainDataPath))
            {
                _prob = ProblemHelper.ReadProblem(trainDataPath);
                _test = ProblemHelper.ScaleProblem(_prob);
                svm   = new C_SVC(_test, KernelHelper.LinearKernel(), C);
                ProblemHelper.WriteProblem(filename + "output.txt", _test);
                fileExistance = true;
            }
            return(fileExistance);
        }
예제 #4
0
        public string svmRealTimeTest(double[] testData)
        {
            int len = 0;

            for (int i = 0; i < testData.Length; i++)
            {
                if (testData[i] < lowPass)
                {
                    len++;
                }
                else
                {
                    i += escape;
                }
            }
            svm_problem tempProb = _prob;

            tempProb.x[0] = new svm_node[len];
            //testData=scaleData(testData);

            /*List<List<double>> testD = new List<List<double>>();
             * testD.Add(testData);
             * testData = new List<double>();
             * double[] data1 = new double[] { 4, 13.465019915, 221.931854818, 34.448097045, 51.47996222,41.137614759, 15.230779949, 22.01443672, 32.395593998, 21.310546988, 0.988700891, 6.74993337, 5.037963203, 1.074775069, 0.615915165, 0.920866746, 6.755586104, 5.014666624, 2.568192279, 12.08015653, 03.931508695, 500, 500, 500, 1269.375212, 185.55572135 };
             * for (int i = 0; i < data1.Length; i++)
             *  testData.Add(data1[i]);
             * testD.Add(testData);
             */
            for (int i = 0, j = 0; i < len && j < testData.Length; j++)
            {
                if (testData[j] < lowPass)
                {
                    tempProb.x[0][i]       = new svm_node();
                    tempProb.x[0][i].value = testData[j];
                    tempProb.x[0][i].index = j + 1;
                    i++;
                }
                else
                {
                    j += escape;
                }
            }
            if (len > 0)
            {
                tempProb = ProblemHelper.ScaleProblem(tempProb);
            }
            var predictY = svm.Predict(tempProb.x[0]);

            return(predictionDictionary[(int)predictY]);
        }