示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="testset"></param>
        /// <param name="target"></param>
        /// <param name="labels"></param>
        /// <param name="confusionMatrix"></param>
        /// <returns>Accuracy for C_SVC, NU_SVC and ONE_CLASS.</returns>
        public static double EvaluateClassificationProblem(SVMProblem testset, double[] target, int[] labels, out int[,] confusionMatrix)
        {
            if (testset.Length != target.Length)
            {
                confusionMatrix = null;
                return(-1);
            }

            Dictionary <int, int> indexes = new Dictionary <int, int>();

            for (int i = 0; i < labels.Length; i++)
            {
                indexes.Add(labels[i], i);
            }

            confusionMatrix = new int[labels.Length, labels.Length];

            int total_correct = 0;

            for (int i = 0; i < testset.Length; i++)
            {
                int y = (int)testset.Y[i];
                int v = (int)target[i];

                confusionMatrix[indexes[y], indexes[v]]++;

                if (y == v)
                {
                    ++total_correct;
                }
            }

            return(100.0 * ((double)total_correct / (double)testset.Length));
        }
        public void TestLibsvmClassify()
        {
            var advancedClassify = new AdvancedClassify();
            var numericalset     = advancedClassify.LoadNumerical();
            var result           = advancedClassify.ScaleData(numericalset);
            var scaledSet        = result.Item1;
            var scalef           = result.Item2;
            var prob             = new SVMProblem();

            foreach (var matchRow in scaledSet)
            {
                prob.Add(matchRow.NumData.Select((v, i) => new SVMNode(i + 1, v)).ToArray(), matchRow.Match);
            }
            var param = new SVMParameter()
            {
                Kernel = SVMKernelType.RBF
            };
            var m = prob.Train(param);

            m.SaveModel("trainModel");
            Func <double[], SVMNode[]> makeInput = ma => scalef(ma).Select((v, i) => new SVMNode(i + 1, v)).ToArray();
            var newrow = new[] { 28, -1, -1, 26, -1, 1, 2, 0.8 };//男士不想要小孩,而女士想要

            TestOutput(m.Predict(makeInput(newrow)));
            newrow = new[] { 28, -1, 1, 26, -1, 1, 2, 0.8 };//双方都想要小孩
            TestOutput(m.Predict(makeInput(newrow)));
        }
示例#3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="testset"></param>
        /// <param name="target"></param>
        /// <param name="correlationCoeff">Squared correlation coefficient for EPSILON_SVR and NU_SVR.</param>
        /// <returns>Mean squared error for EPSILON_SVR and NU_SVR.</returns>
        public static double EvaluateRegressionProblem(SVMProblem testset, double[] target, out double correlationCoeff)
        {
            double total_error = 0;
            double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;

            for (int i = 0; i < testset.Length; i++)
            {
                double y = testset.Y[i];
                double v = target[i];
                total_error += (v - y) * (v - y);
                sumv        += v;
                sumy        += y;
                sumvv       += v * v;
                sumyy       += y * y;
                sumvy       += v * y;
            }

            double mean_squared_error = total_error / (double)testset.Length;

            correlationCoeff =
                (((double)testset.Length * sumvy - sumv * sumy) * ((double)testset.Length * sumvy - sumv * sumy)) /
                (((double)testset.Length * sumvv - sumv * sumv) * ((double)testset.Length * sumyy - sumy * sumy));

            return(mean_squared_error);
        }
示例#4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="testset"></param>
        /// <param name="target"></param>
        /// <param name="labels"></param>
        /// <param name="confusionMatrix"></param>
        /// <returns>Accuracy for C_SVC, NU_SVC and ONE_CLASS.</returns>
        public static double EvaluateClassificationProblem(SVMProblem testset, double[] target, int[] labels, out int[,] confusionMatrix)
        {
            if (testset.Length != target.Length)
            {
                confusionMatrix = null;
                return -1;
            }

            Dictionary<int, int> indexes = new Dictionary<int, int>();
            for (int i = 0; i < labels.Length; i++)
            {
                indexes.Add(labels[i], i);
            }
            
            confusionMatrix = new int[labels.Length, labels.Length];

            int total_correct = 0;
            for (int i = 0; i < testset.Length; i++)
            {
                int y = (int)testset.Y[i];
                int v = (int)target[i];

                confusionMatrix[indexes[y], indexes[v]]++;

                if (y == v)
                {
                    ++total_correct;
                }
            }

            return 100.0 * ((double)total_correct / (double)testset.Length);
        }
示例#5
0
文件: Program.cs 项目: rennisa/cozy
        private static void Train(string prefix)
        {
            SVMProblem trainingSet = SVMProblemHelper.Load(MnistDataPath + prefix + ".txt");

            trainingSet = trainingSet.Normalize(SVMNormType.L2);

            SVMParameter parameter = new SVMParameter();

            parameter.Type   = SVMType.C_SVC;
            parameter.Kernel = SVMKernelType.RBF;
            parameter.C      = 1;
            parameter.Gamma  = 1;

            double[] crossValidationResults;
            int      nFold = 5;

            trainingSet.CrossValidation(parameter, nFold, out crossValidationResults);
            double crossValidationAccuracy = trainingSet.EvaluateClassificationProblem(crossValidationResults);

            Console.WriteLine("\n\nCross validation accuracy: " + crossValidationAccuracy);

            SVMModel model = trainingSet.Train(parameter);

            SVM.SaveModel(model, MnistDataPath + "model.txt");
            Console.WriteLine("\n\nModel ok!");
        }
示例#6
0
        // for training the face,
        public void face_training(SVMProblem f_training)
        {
            SVMProblem trainingSet = SVMProblemHelper.Load(@"C:\Users\temp\Desktop\0921_towp.txt");
            SVMProblem testSet     = SVMProblemHelper.Load(@"C:\Users\temp\Desktop\0921_towpt.txt");

            trainingSet = trainingSet.Normalize(SVMNormType.L2);
            testSet     = testSet.Normalize(SVMNormType.L2);

            SVMParameter parameter = new SVMParameter();

            parameter.Type        = SVMType.NU_SVC;
            parameter.Kernel      = SVMKernelType.SIGMOID;
            parameter.C           = 1;
            parameter.Gamma       = 1;
            parameter.Probability = true;
            double[] crossValidationResults;
            int      nFold = 10;

            trainingSet.CrossValidation(parameter, nFold, out crossValidationResults);
            double   crossValidationAccuracy = trainingSet.EvaluateClassificationProblem(crossValidationResults);
            SVMModel model = trainingSet.Train(parameter);


            double[] testResults = testSet.Predict(model);
            int[,] confusionMatrix;
            double testAccuracy = testSet.EvaluateClassificationProblem(testResults, model.Labels, out confusionMatrix);

            Training_result.Content    = "testAccuracy:" + testAccuracy + "\nCross validation accuracy: " + crossValidationAccuracy + "\nCount " + trainingSet.Y.Count;
            Training_result.FontSize   = 14;
            Training_result.FontStyle  = FontStyles.Normal;
            Training_result.Foreground = Brushes.Red;
            Training_result.Background = Brushes.Black;
            index++;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="problem"></param>
        /// <returns></returns>
        public static SVMProblem RemoveDuplicates(SVMProblem problem)
        {
            SVMProblem temp = new SVMProblem();

            for (int i = 0; i < problem.Length; i++)
            {
                bool same = false;
                for (int j = i + 1; j < problem.Length; j++)
                {
                    same |= SVMProblemHelper.IsEqual(problem.X[i], problem.Y[i], problem.X[j], problem.Y[j]);

                    if (same)
                    {
                        break;
                    }
                }

                if (!same)
                {
                    temp.Add(problem.X[i], problem.Y[i]);
                }
            }

            return(temp);
        }
示例#8
0
        public void TrainAndTestSvmTest()
        {
            SVMProblem train  = new SVMProblem();
            Random     random = new Random();

            for (int i = 0; i < 300; i++)
            {
                int value = random.Next() % 2;
                train.Add(new SVMNode[]
                {
                    new SVMNode(1, value),
                }, value);
            }

            SVMProblem test = new SVMProblem();

            for (int i = 0; i < 100; i++)
            {
                int value = random.Next() % 2;
                test.Add(new SVMNode[]
                {
                    new SVMNode(1, value),
                }, value);
            }

            ProblemHandler.SvmResult result = ProblemHandler.TrainAndTestSvm(train, test);
            Assert.True(result.TestAccuracy > 95);
        }
示例#9
0
        public static SVMProblem Load(IEnumerable <IEnumerable <string> > data, bool isWithClass)
        {
            NumberFormatInfo provider = new NumberFormatInfo();

            SVMProblem problem = new SVMProblem();

            foreach (var row in data)
            {
                var doubleRows = row
                                 .Select(v => Convert.ToDouble(v))
                                 .Select((d, i) => new { d, i })
                                 .Reverse();

                SVMNode[] nodes = doubleRows
                                  .Skip(isWithClass ? 1 : 0)
                                  .Select(d => new SVMNode()
                {
                    Index = d.i + 1, Value = d.d
                })
                                  .ToArray();

                double y = isWithClass ? doubleRows.First().d : 0;
                problem.Add(nodes, y);
            }

            return(problem);
        }
        public void face_training(SVMProblem f_training)
        {
            SVMProblem trainingSet = SVMProblemHelper.Load(@"C:\Users\temp\Desktop\0921_towp.txt");
            SVMProblem testSet     = SVMProblemHelper.Load(@"C:\Users\temp\Desktop\0921_towpt.txt");

            // f_training.Save(@"C:\Users\temp\Desktop\1005f.txt");
            //  trainingSet.Insert(index, f_training.X[0], 2);
            trainingSet.Add(f_training.X[0], 1);
            trainingSet.Save(@"C:\Users\temp\Desktop\flag.txt");
            //   trainingSet.Save(@"C:\Users\temp\Desktop\1005.txt");
            // Console.WriteLine();
            //   SVMNode node = new SVMNode();
            //  node.Index = Convert.ToInt32(o);
            //  node.Value = Convert.ToDouble(f_training.X);
            //  nodes.Add(node);
            //  trainingSet.Add(nodes.ToArray(), 1);
            //  int number = randon.Next(0, trainingSet.X.Count);
            //  int trainingsample = Convert.ToInt32(trainingSet.X.Count * 2 / 3);
            //  int testingsample = Convert.ToInt32(trainingSet.X.Count / 3);

            trainingSet = trainingSet.Normalize(SVMNormType.L2);
            testSet     = testSet.Normalize(SVMNormType.L2);

            SVMParameter parameter = new SVMParameter();

            parameter.Type        = SVMType.NU_SVC;
            parameter.Kernel      = SVMKernelType.SIGMOID;
            parameter.C           = 1;
            parameter.Gamma       = 1;
            parameter.Probability = true;
            int        nFold = 10;
            MainWindow main  = new MainWindow();

            double[] crossValidationResults; // output labels
            trainingSet.CrossValidation(parameter, nFold, out crossValidationResults);
            double   crossValidationAccuracy = trainingSet.EvaluateClassificationProblem(crossValidationResults);
            SVMModel model = SVM.Train(trainingSet, parameter);

            // SVMModel model = trainingSet.Train(parameter);

            SVM.SaveModel(model, @"C:\Users\temp\Desktop\1005.txt");

            double[] testResults = testSet.Predict(model);
            //     Console.WriteLine("");
            int[,] confusionMatrix;
            double testAccuracy = testSet.EvaluateClassificationProblem(testResults, model.Labels, out confusionMatrix);

            // Console.WriteLine("\n\nCross validation accuracy: " + crossValidationAccuracy);
            //  Console.WriteLine("testAccuracy:" + testAccuracy);
            //  Console.WriteLine(Convert.ToString(trainingSet.X.Count));
            main.Training_result.Content    = "testAccuracy:" + testAccuracy + "\nCross validation accuracy: " + crossValidationAccuracy + "\nCount " + trainingSet.X.Count;
            main.Training_result.FontSize   = 14;
            main.Training_result.FontStyle  = FontStyles.Normal;
            main.Training_result.Foreground = Brushes.Red;
            main.Training_result.Background = Brushes.Black;
            // Console.WriteLine(trainingSet1.Length);
            //  trainingSet.Save(@"C:\Users\temp\Desktop\1005.txt");
            index++;
        }
示例#11
0
        public static double[] Predict(this SVMProblem problem, SVMModel model)
        {
            IntPtr ptr_model = SVMModel.Allocate(model);

            double[] target = problem.X.Select(x => x.Predict(ptr_model)).ToArray();
            SVMModel.Free(ptr_model);
            return(target);
        }
示例#12
0
文件: Program.cs 项目: rennisa/cozy
        private static void TestOne(string prefix)
        {
            SVMModel   model   = SVM.LoadModel(MnistDataPath + "model.txt");
            SVMProblem testSet = SVMProblemHelper.Load(MnistDataPath + prefix + ".txt");

            testSet = testSet.Normalize(SVMNormType.L2);
            double[] testResults = testSet.Predict(model);
            Console.WriteLine("\nTest result: " + testResults[0].ToString());
        }
示例#13
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="problem"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 public static SVMProblem Normalize(SVMProblem problem, SVMNormType type)
 {
     SVMProblem temp = new SVMProblem();
     for (int i = 0; i < problem.Length; i++)
     {
         SVMNode[] x = SVMNodeHelper.Normalize(problem.X[i], type);
         temp.Add(x, problem.Y[i]);
     }
     return temp;
 }
        public void AddToProblem(SVMProblem problem, string lable, IEnumerable <KeyValuePair <string, double> > xValues)
        {
            var xx = CreateNodes(xValues);

            if (!Lables.ContainsKey(lable))
            {
                Lables.Add(lable, Lables.Count + 100 + 1);
            }
            problem.Add(xx, Lables[lable]);
        }
示例#15
0
        public OneClassClassifier(List <SVMNode[]> trainingData)
        {
            SVMProblem problem = new SVMProblem();

            for (int i = 0; i < trainingData.Count; i++)
            {
                problem.Add(trainingData[i], 1);
            }
            _trainingData = problem;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="problem"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static SVMProblem Normalize(SVMProblem problem, SVMNormType type)
        {
            SVMProblem temp = new SVMProblem();

            for (int i = 0; i < problem.Length; i++)
            {
                SVMNode[] x = SVMProblemHelper.Normalize(problem.X[i], type);
                temp.Add(x, problem.Y[i]);
            }
            return(temp);
        }
示例#17
0
        //Coin SVM按鈕事件
        private void button8_Click(object sender, EventArgs e)
        {
            StreamWriter Train_txt = new StreamWriter(@"train.txt");
            StreamWriter Test_txt  = new StreamWriter(@"test.txt");

            for (int i = 0; i < 2000; i++)
            {
                if (CoinTrainingSet[i, 0] == 1)
                {
                    Train_txt.WriteLine("1" + " 1:" + CoinTrainingSet[i, 1]);
                }
                else
                {
                    Train_txt.WriteLine("-1" + " 1:" + CoinTrainingSet[i, 1]);
                }
            }

            for (int i = 0; i < 20000; i++)
            {
                if (i < 10000)
                {
                    Test_txt.WriteLine("1" + " 1:" + RV_XY[i]);
                }
                else
                {
                    Test_txt.WriteLine("-1" + " 1:" + RV_XY[i]);
                }
            }

            Train_txt.Close();
            Test_txt.Close();

            SVMProblem problem     = SVMProblemHelper.Load(@"train.txt");
            SVMProblem testProblem = SVMProblemHelper.Load(@"test.txt");

            SVMParameter parameter = new SVMParameter();

            parameter.Type   = SVMType.C_SVC;
            parameter.Kernel = SVMKernelType.RBF;
            parameter.C      = 1;
            parameter.Gamma  = 0.0001;

            SVMModel model = SVM.Train(problem, parameter);

            double[] target = new double[testProblem.Length];
            for (int i = 0; i < testProblem.Length; i++)
            {
                target[i] = SVM.Predict(model, testProblem.X[i]);
            }

            double accuracy = SVMHelper.EvaluateClassificationProblem(testProblem, target);

            label6.Text = accuracy.ToString();
        }
示例#18
0
文件: Program.cs 项目: rennisa/cozy
        private static void Test(string prefix)
        {
            SVMModel   model   = SVM.LoadModel(MnistDataPath + "model.txt");
            SVMProblem testSet = SVMProblemHelper.Load(MnistDataPath + prefix + ".txt");

            testSet = testSet.Normalize(SVMNormType.L2);
            double[] testResults = testSet.Predict(model);
            int[,] confusionMatrix;
            double testAccuracy = testSet.EvaluateClassificationProblem(testResults, model.Labels, out confusionMatrix);

            Console.WriteLine("\nTest accuracy: " + testAccuracy);
        }
示例#19
0
文件: Program.cs 项目: src8655/sku
        //인식 하기
        public static int SVM_Classification(SVMModel md)
        {
            int result = 0;

            SVMProblem testSet = SVMProblemHelper.Load("tmp.txt");    //인식데이터셋 열기

            testSet = testSet.Normalize(SVMNormType.L2);
            double[] testResults = testSet.Predict(md);

            result = (int)testResults[0];

            return(result);
        }
示例#20
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="problem"></param>
 /// <returns></returns>
 public static Dictionary<double, int> GetLabelsCount(SVMProblem problem)
 {
     Dictionary<double, int> dic = new Dictionary<double, int>();
     for (int i = 0; i < problem.Length; i++)
     {
         if (!dic.ContainsKey(problem.Y[i]))
         {
             dic.Add(problem.Y[i], 1);
         }
         else
         {
             dic[problem.Y[i]]++;
         }
     }
     return dic;
 }
示例#21
0
        public void ClassifiGesture(string pathModel, string pathTest, string pathResult)
        { 
            testProblem = SVMProblemHelper.Load(pathTest);
            model = SVM.LoadModel(pathModel);

            double[] testResults = testProblem.Predict(model);

            using (StreamWriter file = new StreamWriter(pathResult, true))
            {
                foreach (double element in testResults)
                {
                    file.Write(element.ToString()+"\n");
                    file.Write(Environment.NewLine); 
                }
            }
        }//end ClassifyGesture 
示例#22
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="testset"></param>
        /// <param name="target"></param>
        /// <returns>Accuracy for C_SVC, NU_SVC and ONE_CLASS.</returns>
        public static double EvaluateClassificationProblem(SVMProblem testset, double[] target)
        {
            int total_correct = 0;

            for (int i = 0; i < testset.Length; i++)
            {
                int y = (int)testset.Y[i];
                int v = (int)target[i];

                if (y == v)
                {
                    ++total_correct;
                }
            }

            return(100.0 * ((double)total_correct / (double)testset.Length));
        }
        public static SVMParameter FindBestHyperparameters(SVMProblem problem, SVMParameter parameter)
        {
            int nFold   = int.Parse(Configuration.Get("nFold"));
            int logTo   = int.Parse(Configuration.Get("logTo"));
            int logFrom = int.Parse(Configuration.Get("logFrom"));

            BlockingCollection <ParameterResult> results = new BlockingCollection <ParameterResult>();
            List <Task> tasks = new List <Task>();

            for (double cLog = logFrom; cLog <= logTo; cLog++)
            {
                double c = Math.Pow(2, cLog);
                tasks.Add(Task.Factory.StartNew(() =>
                {
                    for (double gammaLog = logFrom; gammaLog <= logTo; gammaLog++)
                    {
                        SVMParameter parameterUnderTest = parameter.Clone();
                        parameterUnderTest.C            = c;
                        parameterUnderTest.Gamma        = Math.Pow(2, gammaLog);
                        problem.CrossValidation(parameterUnderTest, nFold, out var crossValidationResults);
                        double crossValidationAccuracy = problem.EvaluateClassificationProblem(crossValidationResults);

                        results.Add(new ParameterResult()
                        {
                            Accuracy = crossValidationAccuracy, C = parameterUnderTest.C,
                            Gamma    = parameterUnderTest.Gamma
                        });
                    }
                }));
            }
            Task.WaitAll(tasks.ToArray());

            var resultList = results.ToList();

            resultList.Sort();

            ParameterResult bestParameter =
                HighestScore(resultList);

            SaveToCsv(results, "svmData.txt");
            SVMParameter returnValue = parameter.Clone();

            returnValue.C     = bestParameter.C;
            returnValue.Gamma = bestParameter.Gamma;
            return(returnValue);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="problem"></param>
        /// <returns></returns>
        public static Dictionary <double, int> GetLabelsCount(SVMProblem problem)
        {
            Dictionary <double, int> dic = new Dictionary <double, int>();

            for (int i = 0; i < problem.Length; i++)
            {
                if (!dic.ContainsKey(problem.Y[i]))
                {
                    dic.Add(problem.Y[i], 1);
                }
                else
                {
                    dic[problem.Y[i]]++;
                }
            }
            return(dic);
        }
示例#25
0
        public SVMClassifier(int folds = 5)
        {
            Folds         = folds;
            DataFile      = @"Data\data.csv";
            Data          = new DataLoader(Folds, DataFile);
            TrainProblems = new SVMProblem[folds];
            TestProblems  = new SVMProblem[folds];

            for (int i = 0; i < folds; i++)
            {
                TrainProblems[i] = new SVMProblem();
                TestProblems[i]  = new SVMProblem();
            }

            for (int i = 0; i < folds; i++)
            {
                for (int j = 0; j < folds; j++)
                {
                    for (int k = 0; k < Data.InstancesPerFold; k++)
                    {
                        var nodes = new SVMNode[Data.FeatureCount];
                        var label = (double)Data.Labels[j, k];
                        for (int x = 0; x < Data.FeatureCount; x++)
                        {
                            nodes[x] = new SVMNode(x + 1, Data.Data[j, k, x]);
                        }
                        if (i != j)
                        {
                            TrainProblems[i].Add(nodes, label);
                        }
                        else
                        {
                            TestProblems[i].Add(nodes, label);
                        }
                    }
                }
            }
            Parameters        = new SVMParameter();
            Parameters.Type   = SVMType.C_SVC;
            Parameters.Kernel = SVMKernelType.RBF;
            Parameters.C      = 1000000;
            Parameters.Degree = 3;
            Parameters.Coef0  = 0;
            Parameters.Gamma  = 0.001;
        }
        public void LibsvmFirstLook()
        {
            var prob = new SVMProblem();

            prob.Add(new[] { new SVMNode(1, 1), new SVMNode(2, 0), new SVMNode(3, 1) }, 1);
            prob.Add(new[] { new SVMNode(1, -1), new SVMNode(2, 0), new SVMNode(3, -1) }, -1);
            var param = new SVMParameter();

            param.Kernel = SVMKernelType.LINEAR;
            param.C      = 10;
            var m = prob.Train(param);

            TestOutput(m.Predict(new [] { new SVMNode(1, 1), new SVMNode(2, 1), new SVMNode(3, 1) }));
            m.SaveModel("trainModel");
            var ml = SVM.LoadModel("trainModel");

            TestOutput(ml.Predict(new[] { new SVMNode(1, 1), new SVMNode(2, 1), new SVMNode(3, 1) }));
        }
示例#27
0
        public static double[] PredictValues(this SVMProblem problem, SVMModel model, out List <double[]> valuesList)
        {
            IntPtr ptr_model = SVMModel.Allocate(model);

            List <double[]> temp = new List <double[]>();

            double[] target = problem.X.Select(x =>
            {
                double[] estimations;
                double y = x.PredictProbability(ptr_model, out estimations);
                temp.Add(estimations);
                return(y);
            }).ToArray();

            SVMModel.Free(ptr_model);

            valuesList = temp;
            return(target);
        }
示例#28
0
        public static SVMProblem CreateCompleteProblem(this IEnumerable <List <double> > original, SAMData sam, SAMDataPoint.FeelingModel feelingModel)
        {
            SVMProblem completeProblem = new SVMProblem();

            for (int i = 0; i < original.Count(); i++)
            {
                SVMNode[] nodeSet = new SVMNode[original.ElementAt(i).Count];
                for (int j = 0; j < original.ElementAt(i).Count; j++)
                {
                    SVMNode currentNode = new SVMNode();
                    currentNode.Index = j + 1;
                    currentNode.Value = original.ElementAt(i)[j];
                    nodeSet[j]        = currentNode;
                }
                completeProblem.Add(nodeSet, sam.dataPoints[i].ToAVCoordinate(feelingModel));
            }

            return(completeProblem);
        }
示例#29
0
        public static SVMProblem CreateCompleteProblemOneClass(this IEnumerable <List <double> > original)
        {
            SVMProblem completeProblem = new SVMProblem();

            for (int i = 0; i < original.Count(); i++)
            {
                SVMNode[] nodeSet = new SVMNode[original.ElementAt(i).Count];
                for (int j = 0; j < original.ElementAt(i).Count; j++)
                {
                    SVMNode currentNode = new SVMNode();
                    currentNode.Index = j + 1;
                    currentNode.Value = original.ElementAt(i)[j];
                    nodeSet[j]        = currentNode;
                }
                completeProblem.Add(nodeSet, 1);
            }

            return(completeProblem);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static SVMProblem Load(string filename)
        {
            if (String.IsNullOrWhiteSpace(filename) || !File.Exists(filename))
            {
                return(null);
            }

            NumberFormatInfo provider = new NumberFormatInfo();

            provider.NumberDecimalSeparator = ".";

            SVMProblem problem = new SVMProblem();

            using (StreamReader sr = new StreamReader(filename))
            {
                while (true)
                {
                    string line = sr.ReadLine();
                    if (line == null)
                    {
                        break;
                    }

                    string[] list = line.Trim().Split(' ');

                    double y = Convert.ToDouble(list[0].Trim());

                    List <SVMNode> nodes = new List <SVMNode>();
                    for (int i = 1; i < list.Length; i++)
                    {
                        string[] temp = list[i].Split(':');
                        SVMNode  node = new SVMNode();
                        node.Index = Convert.ToInt32(temp[0].Trim());
                        node.Value = Convert.ToDouble(temp[1].Trim(), provider);
                        nodes.Add(node);
                    }

                    problem.Add(nodes.ToArray(), y);
                }
            }

            return(problem);
        }
        public static void Train()
        {
            SVMProblem svmproblem = new SVMProblem();
            List <Tuple <Sentence, string> > sentences = new List <Tuple <Sentence, string> >();
            StreamReader sr = new StreamReader("training_data.txt");
            string       line;

            while ((line = sr.ReadLine()) != null)
            {
                string[] lines = line.Split(',');
                lines[0] = lines[0].Substring(1, lines[0].Length - 2);
                string sentence = lines[0];
                sentences.Add(Tuple.Create(Sentence.ParseIntoSentence(sentence, 0, false), lines[1]));
            }

            List <SVMNode[]> modelNodes = new List <SVMNode[]>();

            for (int i = 0; i < sentences.Count; i++)
            {
                List <SVMNode> nodes = new List <SVMNode>();
                for (int j = 0; j < Corpus.count; j++)
                {
                    SVMNode sentenceNode = new SVMNode(j, 0);
                    nodes.Add(sentenceNode);
                }
                for (int j = 0; j < sentences[i].Item1.SentenceWords.Count; j++)
                {
                    CorpusWord cw                   = Corpus.CorpusList.Find(c => c.Word.Equals(sentences[i].Item1.SentenceWords[j].Stem.Word) && c.Attribute.Equals(sentences[i].Item1.SentenceWords[j].Stem.Attribute));
                    SVMNode    sentenceNode         = new SVMNode(Corpus.CorpusList.IndexOf(cw), 1);
                    SVMNode    sentenceNodeToRemove = new SVMNode(Corpus.CorpusList.IndexOf(cw), 0);
                    nodes.Remove(sentenceNodeToRemove);
                    nodes.Add(sentenceNode);
                }
                SVMNode[] sentenceNodes = nodes.ToArray();
                sentenceNodes = sentenceNodes.OrderBy(x => x.Index).ToArray();

                svmproblem.Add(sentenceNodes, Corpus.CorpusList.IndexOf(Corpus.CorpusList.Find(c => c.Word.Equals(sentences[i].Item2))));
            }

            model = SVM.Train(svmproblem, parameter);

            sr.Close();
        }
        public static bool Save(SVMProblem problem, string filename)
        {
            if (String.IsNullOrWhiteSpace(filename) || problem == null || problem.Length == 0)
            {
                return(false);
            }

            //    //  StreamWriter sw = new StreamWriter(fs);
            NumberFormatInfo provider = new NumberFormatInfo();

            provider.NumberDecimalSeparator = ".";

            using (StreamWriter sw = new StreamWriter(filename))
            {
                for (int i = 0; i < problem.Length; i++)
                {
                    // sw.Write("\r\n");

                    sw.Write(problem.Y[i]);

                    if (problem.X[i].Length > 0)
                    {
                        sw.Write(" ");

                        for (int j = 0; j < problem.X[i].Length; j++)
                        {
                            sw.Write(problem.X[i][j].Index);
                            sw.Write(":");
                            sw.Write(problem.X[i][j].Value.ToString(provider));

                            if (j < problem.X[i].Length - 1)
                            {
                                sw.Write(" ");
                            }
                        }
                    }

                    sw.Write("\n");
                }
            }

            return(true);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="problem"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static bool Save(SVMProblem problem, string filename)
        {
            if (String.IsNullOrWhiteSpace(filename) || problem == null || problem.Length == 0)
            {
                return(false);
            }
            Console.WriteLine("aaaa");
            //    //  StreamWriter sw = new StreamWriter(fs);
            NumberFormatInfo provider = new NumberFormatInfo();

            provider.NumberDecimalSeparator = ".";
            //   FileStream fs = new FileStream(@"C:\Users\temp\Desktop\1005!!", FileMode.Append, FileAccess.Write);
            using (StreamWriter sw = new StreamWriter(filename))
            {
                for (int i = 0; i < problem.Length; i++)
                {
                    sw.Write("\n");
                    sw.Write(problem.Y[i]);

                    if (problem.X[i].Length > 0)
                    {
                        sw.Write(" ");

                        for (int j = 0; j < problem.X[i].Length; j++)
                        {
                            sw.Write(problem.X[i][j].Index);
                            sw.Write(":");
                            sw.Write(problem.X[i][j].Value.ToString(provider));

                            if (j < problem.X[i].Length - 1)
                            {
                                sw.Write(" ");
                            }
                        }
                    }

                    sw.Write("\n");
                }
            }

            return(true);
        }
示例#34
0
        //--------------------------------------------------------------------------------------
        // private
        //---------------------------------------------------------------------------------------

        /// <summary>
        /// 辞書ファイルを作成する
        /// </summary>
        /// <param name="input_learing_file"></param>
        /// <param name="gammma"></param>
        /// <param name="cost"></param>
        private void Training(string input_learing_file, float gammma, float cost)
        {
            //LibSVMのテスト
            //学習用のデータの読み込み
            SVMProblem problem = SVMProblemHelper.Load(input_learing_file);

            //SVMパラメータ
            SVMParameter parameter = new SVMParameter();

            parameter.Type   = LibSVMsharp.SVMType.C_SVC;
            parameter.Kernel = LibSVMsharp.SVMKernelType.RBF;
            parameter.C      = cost;
            parameter.Gamma  = gammma;

            //svmModelが上手く作れていない?ラベルが付けられてない!!

            libSVM_model = SVM.Train(problem, parameter);
            //辞書ファイルを出力(xmlファイル)
            string xml_name = @"model_" + input_learing_file;

            xml_name = xml_name.Replace(@".csv", @".xml");
            SVM.SaveModel(libSVM_model, xml_name);

            //判定結果をファイルに出してみる
            SVMProblem testProblem = SVMProblemHelper.Load(input_learing_file);

            double[] target         = new double[testProblem.Length];
            string   debug_file_str = @"debug_" + input_learing_file;

            using (StreamWriter w = new StreamWriter(debug_file_str))
            {
                for (int i = 0; i < testProblem.Length; i++)
                {
                    target[i] = SVM.Predict(libSVM_model, testProblem.X[i]);
                    w.Write(target[i] + "\n");
                    Console.Out.WriteLine(@"{0} : {1}", i, target[i]);
                }
            }
            //正解率を出す。
            double accuracy = SVMHelper.EvaluateClassificationProblem(testProblem, target);
        }
示例#35
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="testset"></param>
        /// <param name="target"></param>
        /// <returns>Accuracy for C_SVC, NU_SVC and ONE_CLASS.</returns>
        public static double EvaluateClassificationProblem(SVMProblem testset, double[] target)
        {
            if (testset.Length != target.Length)
            {
                return -1;
            }

            int total_correct = 0;
            for (int i = 0; i < testset.Length; i++)
            {
                int y = (int)testset.Y[i];
                int v = (int)target[i];

                if (y == v)
                {
                    ++total_correct;
                }
            }

            return 100.0 * ((double)total_correct / (double)testset.Length);
        }
示例#36
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="testset"></param>
        /// <param name="target"></param>
        /// <param name="correlationCoeff">Squared correlation coefficient for EPSILON_SVR and NU_SVR.</param>
        /// <returns>Mean squared error for EPSILON_SVR and NU_SVR.</returns>
        public static double EvaluateRegressionProblem(SVMProblem testset, double[] target, out double correlationCoeff)
        {
            double total_error = 0;
            double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
            for (int i = 0; i < testset.Length; i++)
            {
                double y = testset.Y[i];
                double v = target[i];
                total_error += (v - y) * (v - y);
                sumv += v;
                sumy += y;
                sumvv += v * v;
                sumyy += y * y;
                sumvy += v * y;
            }

            double mean_squared_error = total_error / (double)testset.Length;
            correlationCoeff =
                (((double)testset.Length * sumvy - sumv * sumy) * ((double)testset.Length * sumvy - sumv * sumy)) /
                (((double)testset.Length * sumvv - sumv * sumv) * ((double)testset.Length * sumyy - sumy * sumy));

            return mean_squared_error;
        }
示例#37
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="problem"></param>
        /// <returns></returns>
        public static SVMProblem RemoveDuplicates(SVMProblem problem)
        {
            SVMProblem temp = new SVMProblem();
            for (int i = 0; i < problem.Length; i++)
            {
                bool same = false;
                for (int j = i + 1; j < problem.Length; j++)
                {
                    same |= SVMNodeHelper.IsEqual(problem.X[i], problem.Y[i], problem.X[j], problem.Y[j]);

                    if (same)
                    {
                        break;
                    }
                }

                if (!same)
                {
                    temp.Add(problem.X[i], problem.Y[i]);
                }
            }

            return temp;
        }
示例#38
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static SVMProblem Load(string filename)
        {
            if (String.IsNullOrWhiteSpace(filename) || !File.Exists(filename))
            {
                return null;
            }

            NumberFormatInfo provider = new NumberFormatInfo();
            provider.NumberDecimalSeparator = ".";

            SVMProblem problem = new SVMProblem();
            using (StreamReader sr = new StreamReader(filename))
            {
                while (true)
                {
                    string line = sr.ReadLine();
                    if (line == null)
                        break;

                    string[] list = line.Trim().Split(' ');

                    double y = Convert.ToDouble(list[0].Trim(), provider);

                    List<SVMNode> nodes = new List<SVMNode>();
                    for (int i = 1; i < list.Length; i++)
                    {
                        string[] temp = list[i].Split(':');
                        SVMNode node = new SVMNode();
                        node.Index = Convert.ToInt32(temp[0].Trim());
                        node.Value = Convert.ToDouble(temp[1].Trim(), provider);
                        nodes.Add(node);
                    }

                    problem.Add(nodes.ToArray(), y);
                }
            }

            return problem;
        }
示例#39
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="problem"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static bool Save(SVMProblem problem, string filename)
        {
            if (String.IsNullOrWhiteSpace(filename) || problem == null || problem.Length == 0)
            {
                return false;
            }

            NumberFormatInfo provider = new NumberFormatInfo();
            provider.NumberDecimalSeparator = ".";

            using (StreamWriter sw = new StreamWriter(filename))
            {
                for (int i = 0; i < problem.Length; i++)
                {
                    sw.Write(problem.Y[i]);

                    if (problem.X[i].Length > 0)
                    {
                        sw.Write(" ");

                        for (int j = 0; j < problem.X[i].Length; j++)
                        {
                            sw.Write(problem.X[i][j].Index);
                            sw.Write(":");
                            sw.Write(problem.X[i][j].Value.ToString(provider));

                            if (j < problem.X[i].Length - 1)
                            {
                                sw.Write(" ");
                            }
                        }
                    }

                    sw.Write("\n");
                }
            }

            return true;
        }