예제 #1
0
        public static void GetMyPrediction()
        {
            Console.WriteLine("Begin ML.NET demo run");
            Console.WriteLine("Income from age, sex, politics");
            var    pipeline = new LearningPipeline();
            string dataPath = AppDomain.CurrentDomain.BaseDirectory + "/PeopleData.txt";

            pipeline.Add(new TextLoader(dataPath).
                         CreateFrom <myLottery>(separator: ' '));
            pipeline.Add(new ColumnCopier(("Income", "Label")));
            //pipeline.Add(new CategoricalOneHotVectorizer("Politic"));
            pipeline.Add(new ColumnConcatenator("Features", "pre10",
                                                "pre9", "pre8", "pre7", "pre6", "pre5", "pre4", "pre3"
                                                , "pre2", "pre1"));
            var sdcar = new StochasticDualCoordinateAscentRegressor();

            sdcar.MaxIterations     = 1000;
            sdcar.NormalizeFeatures = NormalizeOption.Auto;
            pipeline.Add(sdcar);
            // pipeline.N
            Console.WriteLine("\nStarting training \n");
            var model = pipeline.Train <myLottery, myPrediction>();

            Console.WriteLine("\nTraining complete \n");
            string modelPath = AppDomain.CurrentDomain.BaseDirectory + "/IncomeModel.zip";

            Task.Run(async() =>
            {
                await model.WriteAsync(modelPath);
            }).GetAwaiter().GetResult();
            var testData = new TextLoader(dataPath).
                           CreateFrom <myLottery>(separator: ' ');
            var    evaluator = new RegressionEvaluator();
            var    metrics   = evaluator.Evaluate(model, testData);
            double rms       = metrics.Rms;

            Console.WriteLine("Root mean squared error = " +
                              rms.ToString("F4"));
            Console.WriteLine("Income age 40 conservative male: ");
            myLottery newPatient = new myLottery()
            {
                pre10 = 6824298f,
                pre9  = 2589916f,
                pre8  = 2602089f,
                pre7  = 2915497f,
                pre6  = 8507838f,
                pre5  = 7679324f,
                pre4  = 607461f,
                pre3  = 5806877,
                pre2  = 6776442f,
                pre1  = 9975203
            };
            myPrediction prediction = model.Predict(newPatient);
            float        predIncome = prediction.Income;

            Console.WriteLine("Predicted income = $" +
                              predIncome.ToString("F2"));
            Console.WriteLine("\nEnd ML.NET demo");
            Console.ReadLine();
        }
예제 #2
0
        public static void GetMyPrediction()
        {
            Console.WriteLine("Begin ML.NET demo run");
            Console.WriteLine("Income from age, sex, politics");
            var    pipeline = new LearningPipeline();
            string dataPath = AppDomain.CurrentDomain.BaseDirectory + "/datamodel/PeopleData.txt";

            pipeline.Add(new TextLoader(dataPath).
                         CreateFrom <IncomeData>(separator: ','));
            pipeline.Add(new ColumnCopier(("Income", "Label")));
            pipeline.Add(new CategoricalOneHotVectorizer("Politic"));
            pipeline.Add(new ColumnConcatenator("Features", "Age",
                                                "Sex", "Politic"));
            var sdcar = new StochasticDualCoordinateAscentRegressor();

            sdcar.MaxIterations     = 1000;
            sdcar.NormalizeFeatures = NormalizeOption.Auto;
            pipeline.Add(sdcar);
            // pipeline.N
            Console.WriteLine("\nStarting training \n");
            var model = pipeline.Train <IncomeData, IncomePrediction>();

            Console.WriteLine("\nTraining complete \n");
            string modelPath = AppDomain.CurrentDomain.BaseDirectory + "/IncomeModel.zip";

            Task.Run(async() =>
            {
                await model.WriteAsync(modelPath);
            }).GetAwaiter().GetResult();
            var testData = new TextLoader(dataPath).
                           CreateFrom <IncomeData>(separator: ',');
            var    evaluator = new RegressionEvaluator();
            var    metrics   = evaluator.Evaluate(model, testData);
            double rms       = metrics.Rms;

            Console.WriteLine("Root mean squared error = " +
                              rms.ToString("F4"));
            Console.WriteLine("Income age 40 conservative male: ");
            IncomeData newPatient = new IncomeData()
            {
                Age     = 40.0f,
                Sex     = -1f,
                Politic = "conservative"
            };
            IncomePrediction prediction = model.Predict(newPatient);
            float            predIncome = prediction.Income * 10000;

            Console.WriteLine("Predicted income = $" +
                              predIncome.ToString("F2"));
            Console.WriteLine("\nEnd ML.NET demo");
            Console.ReadLine();
        } // Main