示例#1
0
        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()));
        }