Exemple #1
0
        private static void TestSinglePrediction(MLContext mlContext)
        {
            ITransformer loadedModel;

            using (var stream = new FileStream(_modelPath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                loadedModel = mlContext.Model.Load(stream);
            }

            // PredictionEngine is a "wrapper" around a model that allows predictions on individual examples (most common scenario in production for consumer-scale applications? Or is batching more common?
            var predictionFunction = loadedModel.CreatePredictionEngine <TaxiTrip, TaxiTripFarePrediction>(mlContext);

            // TODO: Parameterise
            var taxiTripSample = new TaxiTrip()
            {
                VendorId       = "VTS",
                RateCode       = "1",
                PassengerCount = 1,
                TripTime       = 1140,
                TripDistance   = 3.75f,
                PaymentType    = "CRD",
                FareAmount     = 0 // To predict. Actual/Observed = 15.5
            };

            var prediction = predictionFunction.Predict(taxiTripSample);

            //TODO: PrintBanner again
            Console.WriteLine($"**********************************************************************");
            Console.WriteLine($"Predicted fare: {prediction.FareAmount:0.####}, actual fare: 15.5");
            Console.WriteLine($"**********************************************************************");
        }
        private static void TestSinglePrediction(MLContext mlContext, ITransformer model)
        {
            //Prediction test
            // Create prediction function and make prediction.
            var predictionFunction = mlContext.Model.CreatePredictionEngine <TaxiTrip, TaxiTripFarePrediction>(model);

            //Sample:
            //vendor_id,rate_code,passenger_count,trip_time_in_secs,trip_distance,payment_type,fare_amount
            //VTS,1,1,1140,3.75,CRD,15.5

            var taxiTripSample = new TaxiTrip()
            {
                VendorId       = "VTS",
                RateCode       = "1",
                PassengerCount = 1,
                TripTime       = 1140,
                TripDistance   = 3.75f,
                PaymentType    = "CRD",
                FareAmount     = 0 // To predict. Actual/Observed = 15.5
            };

            var prediction = predictionFunction.Predict(taxiTripSample);

            Console.WriteLine($"**********************************************************************");
            Console.WriteLine($"Predicted fare: {prediction.FareAmount:0.####}, actual fare: 15.5");
            Console.WriteLine($"**********************************************************************");
        }
Exemple #3
0
        private static void TestSinglePrediction(MLContext mlContext)
        {
            ITransformer loadedModel;

            Microsoft.ML.DataViewSchema inputSchema;
            using (var stream = new FileStream(_modelPath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                loadedModel = mlContext.Model.Load(stream, out inputSchema);
            }
            var predictionFunction = mlContext.Model.CreatePredictionEngine <TaxiTrip, TaxiTripFarePrediction>(loadedModel);
            var taxiTripSample     = new TaxiTrip()
            {
                VendorId       = "VTS",
                RateCode       = "1",
                PassengerCount = 1,
                TripTime       = 1140,
                TripDistance   = 3.75f,
                PaymentType    = "CRD",
                FareAmount     = 0 // To predict. Actual/Observed = 15.5
            };

            var prediction = predictionFunction.Predict(taxiTripSample);

            Console.WriteLine($"**********************************************************************");
            Console.WriteLine($"Predicted fare: {prediction.FareAmount:0.####}, actual fare: 15.5");
            Console.WriteLine($"**********************************************************************");
        }
Exemple #4
0
        static void Main(string[] args)
        {
            MLContext mainContext = new MLContext();
            // RegressionContext regressionContext = new RegressionContext(mainContext);

            EasyML MLConstruction = new EasyML("RegressionModel", mainContext);

            IEstimator <ITransformer> pipeline = mainContext.Transforms.CopyColumns("FareAmount", "Label")
                                                 .Append(mainContext.Transforms.Categorical.OneHotEncoding("VendorId"))
                                                 .Append(mainContext.Transforms.Categorical.OneHotEncoding("PaymentType"))
                                                 .Append(mainContext.Transforms.Concatenate("Features", "VendorId", "RateCode", "PassengerCount", "TripTimeInSecs", "TripDistance", "PaymentType"))
                                                 .Append(mainContext.Regression.Trainers.FastTree());

            var model = MLConstruction.CreateFittedModel(pipeline);
            // MLConstruction.SaveModelAsFile(model);
            TaxiTrip TestInstance = new TaxiTrip()
            {
                VendorId       = "VTS",
                RateCode       = 1,
                PassengerCount = 1,
                TripTimeInSecs = 1140,
                TripDistance   = 3.75f,
                PaymentType    = "CRD",
                FareAmount     = 0.0f // To predict. Actual/Observed = 15.5
            };
            var testScore = MLConstruction.TestSinglePrediction <TaxiTrip, TaxiTripFarePrediction>(TestInstance, model);

            Console.WriteLine($"{testScore.FareAmount}");
        }
Exemple #5
0
        private static void TestSinglePrediction(MLContext mlContext)
        {
            //vendor_id,rate_code,passenger_count,trip_time_in_secs,trip_distance,payment_type,fare_amount
            //VTS,1,1,1140,3.75,CRD,15.5

            var taxiTripSample = new TaxiTrip
            {
                VendorId       = "VTS",
                RateCode       = "1",
                PassengerCount = 1,
                TripTime       = 1140,
                TripDistance   = 3.75f,
                PaymentType    = "CRD",
                FareAmount     = 0 // To predict. Actual/Observed = 15.5
            };

            // Load trained model
            var trainedModel = mlContext.Model.Load(ModelPath, out var modelInputSchema);

            // Create prediction engine related to the loaded trained model
            var predEngine = mlContext.Model.CreatePredictionEngine <TaxiTrip, TaxiTripFarePrediction>(trainedModel);

            //Score
            var resultprediction = predEngine.Predict(taxiTripSample);

            Console.WriteLine($"**********************************************************************");
            Console.WriteLine($"Predicted fare: {resultprediction.FareAmount:0.#####}, actual fare: 15.5");
            Console.WriteLine($"**********************************************************************");
        }
Exemple #6
0
        public static async Task <TaxiTripFarePrediction> Predict(TaxiTrip trip)
        {
            PredictionModel <TaxiTrip, TaxiTripFarePrediction> _model = await PredictionModel.ReadAsync <TaxiTrip, TaxiTripFarePrediction>(_modelpath);

            var prediction = _model.Predict(trip);

            return(prediction);
        }
Exemple #7
0
        /// <summary>
        /// 使用模型來進行預測
        /// TestSinglePrediction 方法會執行下列工作:
        /// * 建立單一評論的測試資料。
        /// * 根據測試資料預測費用金額。
        /// * 合併測試資料和預測來進行報告。
        /// * 顯示預測的結果。
        /// </summary>
        /// <param name="mlContext"></param>
        /// <param name="model"></param>
        private static void TestSinglePrediction(MLContext mlContext, ITransformer model)
        {
            var predictionFunction = mlContext.Model.CreatePredictionEngine <TaxiTrip, TaxiTripFarePrediction>(model);

            //測試用的計程車行程
            var taxiTripSample = new TaxiTrip()
            {
                VendorId       = "VTS",
                RateCode       = "1",
                PassengerCount = 1,
                TripTime       = 1140,
                TripDistance   = 3.75f,
                PaymentType    = "CRD",
                FareAmount     = 0 // To predict. Actual/Observed = 15.5
            };

            var taxiTripSample2 = new TaxiTrip()
            {
                VendorId       = "VTS",
                RateCode       = "1",
                PassengerCount = 1,
                TripTime       = 1260,
                TripDistance   = 10.33f,
                PaymentType    = "CSH",
                FareAmount     = 0 // To predict. Actual/Observed = 29.5
            };
            var taxiTripSample3 = new TaxiTrip()
            {
                VendorId       = "VTS",
                RateCode       = "1",
                PassengerCount = 3,
                TripTime       = 480,
                TripDistance   = 1.9f,
                PaymentType    = "CRD",
                FareAmount     = 0 // To predict. Actual/Observed = 8.5
            };


            var prediction  = predictionFunction.Predict(taxiTripSample);
            var prediction2 = predictionFunction.Predict(taxiTripSample2);
            var prediction3 = predictionFunction.Predict(taxiTripSample3);



            Console.WriteLine($"**********************************************************************");
            Console.WriteLine($"預測: {prediction.FareAmount:0.####}, 實際: 15.5, 誤差 " + Math.Round((Math.Abs(15.5 - prediction.FareAmount)) / 15.5, 4) + "%");
            Console.WriteLine($"預測: {prediction2.FareAmount:0.####}, 實際: 29.5, 誤差 " + Math.Round((Math.Abs(29.5 - prediction.FareAmount)) / 29.5, 4) + "%");
            Console.WriteLine($"預測: {prediction3.FareAmount:0.####}, 實際: 8.5, 誤差 " + Math.Round(Math.Abs(8.5 - prediction.FareAmount) / 8.5, 4) + "%");
            Console.WriteLine($"**********************************************************************");
        }
Exemple #8
0
        private static void TestTwoPredictions(MLContext mlContext)
        {
            //load the model
            ITransformer loadedModel;

            using (var stream = new FileStream(_modelPath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                loadedModel = mlContext.Model.Load(stream);
            }

            //Prediction test
            // Create prediction function and make prediction.
            var predictionFunction = loadedModel.CreatePredictionEngine <TaxiTrip, TaxiTripFarePrediction>(mlContext);
            //Sample:
            //vendor_id,rate_code,passenger_count,trip_time_in_secs,trip_distance,payment_type,fare_amount
            //VTS,1,1,1140,3.75,CRD,15.5
            var taxiTripSample = new TaxiTrip()
            {
                VendorId       = "VTS",
                RateCode       = "1",
                PassengerCount = 4,
                TripTime       = 1140,
                TripDistance   = 3.75f,
                PaymentType    = "CRD",
                FareAmount     = 0 // To predict. Actual/Observed = 15.5
            };
            var prediction = predictionFunction.Predict(taxiTripSample);

            WriteLine($"**********************************************************************");
            WriteLine($"Predicted fare: {prediction.FareAmount:0.####}, actual fare: 15.5");
            WriteLine($"**********************************************************************\n");

            //  VTS 1   2   1140    6.29    CRD 21
            taxiTripSample = new TaxiTrip()
            {
                VendorId       = "VTS",
                RateCode       = "1",
                PassengerCount = 2,
                TripTime       = 1140,
                TripDistance   = 6.29f,
                PaymentType    = "CRD",
                FareAmount     = 0 // To predict. Actual/Observed = 21
            };
            prediction = predictionFunction.Predict(taxiTripSample);

            WriteLine($"**********************************************************************");
            WriteLine($"Predicted fare: {prediction.FareAmount:0.####}, actual fare: 21");
            WriteLine($"**********************************************************************\n");
        }
Exemple #9
0
        /// <summary>
        /// Extending this with something actually useful
        /// </summary>
        /// <param name="mLContext">Pass context in because I'm not a fan of state</param>
        /// <param name="trip">Trip details for which to predict price</param>
        /// <returns></returns>
        private static TaxiTripFarePrediction Predict(MLContext mlContext, TaxiTrip trip)  // This should not really be in Program.cs, make public and move this and everything else out into an Engine class?
        {
            // TODO: Cache model? Going from disk every time seems extremely inefficient.
            ITransformer loadedModel;

            using (var stream = new FileStream(_modelPath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                loadedModel = mlContext.Model.Load(stream);
            }

            var predictionFunction = loadedModel.CreatePredictionEngine <TaxiTrip, TaxiTripFarePrediction>(mlContext);

            var prediction = predictionFunction.Predict(trip);

            return(prediction);
        }
Exemple #10
0
        private static void ReadAndTestRandomRowFromFile(MLContext mlContext)
        {
            if (File.Exists(_testDataPath))
            {
                using (StreamReader reader = new StreamReader(_testDataPath))
                {
                    string        strFile              = reader.ReadToEnd();
                    Random        rand                 = new Random();
                    List <string> strArray             = new List <string>(strFile.Split("\n"));
                    List <string> randomTaxiTripSample = new List <string>(strArray[rand.Next(1, strArray.Count)].Split(","));

                    ITransformer loadedModel;
                    using (var stream = new FileStream(_modelPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        loadedModel = mlContext.Model.Load(stream);
                    }

                    var predictionFunction = loadedModel.CreatePredictionEngine <TaxiTrip, TaxiTripFarePrediction>(mlContext);
                    var taxiTripSample     = new TaxiTrip()
                    {
                        VendorId       = randomTaxiTripSample[0],
                        RateCode       = randomTaxiTripSample[1],
                        PassengerCount = float.Parse(randomTaxiTripSample[2]),
                        TripTime       = float.Parse(randomTaxiTripSample[3]),
                        TripDistance   = float.Parse(randomTaxiTripSample[4]),
                        PaymentType    = randomTaxiTripSample[5],
                        FareAmount     = float.Parse(randomTaxiTripSample[6])
                    };

                    var prediction = predictionFunction.Predict(taxiTripSample);

                    WriteLine($"**********************************************************************");
                    WriteLine($"VendorId:       {taxiTripSample.VendorId}");
                    WriteLine($"RateCode:       {taxiTripSample.RateCode}");
                    WriteLine($"PassengerCount: {taxiTripSample.PassengerCount}");
                    WriteLine($"TripTime:       {taxiTripSample.TripTime}");
                    WriteLine($"TripDistance:   {taxiTripSample.TripDistance}");
                    WriteLine($"PaymentType:    {taxiTripSample.PaymentType}");
                    WriteLine($"Predicted fare: {prediction.FareAmount:0.####}, actual fare: {taxiTripSample.FareAmount}");
                    WriteLine($"**********************************************************************\n");
                }
            }
            else
            {
                WriteLine($"{_testDataPath} not found.");
            }
        }
Exemple #11
0
        static void Main(string[] args)
        {
            var mlContext = new MLContext(seed: 0);

            _textLoader = mlContext.Data.CreateTextLoader(new TextLoader.Arguments()
            {
                Separators = new[] { ',' },
                HasHeader  = true,
                Column     = new[]
                {
                    new TextLoader.Column("VendorId", DataKind.Text, 0),
                    new TextLoader.Column("RateCode", DataKind.Text, 1),   // Peak, offpeak?
                    new TextLoader.Column("PassengerCount", DataKind.R4, 2),
                    new TextLoader.Column("TripTime", DataKind.R4, 3),     // Duration in seconds
                    new TextLoader.Column("TripDistance", DataKind.R4, 4), // Decimal miles
                    new TextLoader.Column("PaymentType", DataKind.Text, 5),
                    new TextLoader.Column("FareAmount", DataKind.R4, 6)    // Decimal dollars
                }
            });
            // Don't regenerate and reevaluate the model every time, takes too long
            // TODO: Make it regenerate the model if the training data has changed? (Would need to persist a hash of the training data between runs)
            if (!File.Exists(_modelPath))
            {
                var model = Train(mlContext, _trainDataPath); // Generate model from training dataset - TODO: No reason to do this every time!
                Evaluate(mlContext, model);                   // Evaluate model performance against the test data (TODO: Parameterise to allow evaluation against different datasets without modifying source)
            }
            //TestSinglePrediction(mlContext);

            // Let's actually use this as if it was a real service

            // TODO: Make it possible to specify this trip from command line
            var culture = new CultureInfo("en-US");
            var trip    = new TaxiTrip()
            {
                VendorId       = "VTS",
                RateCode       = "1",
                PassengerCount = int.Parse(args[0], culture),   //1,
                TripTime       = int.Parse(args[1], culture),   //1540,
                TripDistance   = float.Parse(args[2], culture), // 5.70f,
                PaymentType    = "CRD",
                FareAmount     = 0                              // To predict.
            };
            var result = Predict(mlContext, trip);

            Console.WriteLine($"Predicted fare: ${result.FareAmount:#.##}");
        }
Exemple #12
0
        private static void TestSinglePrediction(MLContext mLContext, ITransformer model) {
            var predictionFunction = mLContext.Model.CreatePredictionEngine<TaxiTrip, TaxiTripFarePrediction>(model);
            var taxiTripSample = new TaxiTrip() {
                VendorId = "VTS",
                RateCode = "1",
                PassengerCount = 1,
                TripTime = 1140,
                TripDistance = 3.75f,
                PaymentType = "CRD",
                FareAmount = 0 // To predict. Actual/Observed = 15.5
            };
            var prediction = predictionFunction.Predict(taxiTripSample);

            Console.WriteLine($"**********************************************************************");
            Console.WriteLine($"Predicted fare: {prediction.FareAmount:0.####}, actual fare: 15.5");
            Console.WriteLine($"**********************************************************************");
        }
Exemple #13
0
        private static void TestSinglePrediction(TaxiFarePredictionEngine.PredictionEngine predictionEngine)
        {
            TaxiTrip taxiTripSample = new TaxiTrip()
            {
                VendorId       = "VTS",
                RateCode       = "1",
                PassengerCount = 1,
                TripTime       = 1140,
                TripDistance   = 3.75f,
                PaymentType    = "CRD",
                FareAmount     = 0
            };

            TaxiTripFarePrediction prediction = predictionEngine.GetFarePrediction(taxiTripSample);

            Console.WriteLine($"**********************************************************************");
            Console.WriteLine($"Predicted fare: {prediction.FareAmount:0.####}, actual fare: 15.5");
            Console.WriteLine($"**********************************************************************");
        }
Exemple #14
0
        private static void TestSinglePrediction(MLContext mlContext)
        {
            //load the model
            // <Snippet21>
            ITransformer loadedModel;

            using (var stream = new FileStream(_modelPath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                loadedModel = mlContext.Model.Load(stream);
            }
            // </Snippet21>

            //Prediction test
            // Create prediction function and make prediction.
            // <Snippet22>
            var predictionFunction = loadedModel.MakePredictionFunction <TaxiTrip, TaxiTripFarePrediction>(mlContext);
            // </Snippet22>
            //Sample:
            //vendor_id,rate_code,passenger_count,trip_time_in_secs,trip_distance,payment_type,fare_amount
            //VTS,1,1,1140,3.75,CRD,15.5
            // <Snippet23>
            var taxiTripSample = new TaxiTrip()
            {
                VendorId       = "VTS",
                RateCode       = "1",
                PassengerCount = 1,
                TripTime       = 1140,
                TripDistance   = 3.75f,
                PaymentType    = "CRD",
                FareAmount     = 0 // To predict. Actual/Observed = 15.5
            };
            // </Snippet23>
            // <Snippet24>
            var prediction = predictionFunction.Predict(taxiTripSample);

            // </Snippet24>
            // <Snippet25>
            Console.WriteLine($"**********************************************************************");
            Console.WriteLine($"Predicted fare: {prediction.FareAmount:0.####}, actual fare: 15.5");
            Console.WriteLine($"**********************************************************************");
            // </Snippet25>
        }
Exemple #15
0
        /// <summary>
        /// The main application entry point.
        /// </summary>
        /// <param name="args">The command line arguments.</param>
        static void Main(string[] args)
        {
            // create the machine learning context
            var mlContext = new MLContext();

            // set up the text loader
            var textLoader = mlContext.Data.CreateTextLoader(
                new TextLoader.Options()
            {
                Separators = new[] { ',' },
                HasHeader  = true,
                Columns    = new[]
                {
                    new TextLoader.Column("VendorId", DataKind.String, 0),
                    new TextLoader.Column("RateCode", DataKind.String, 5),
                    new TextLoader.Column("PassengerCount", DataKind.Single, 3),
                    new TextLoader.Column("TripDistance", DataKind.Single, 4),
                    new TextLoader.Column("PaymentType", DataKind.String, 9),
                    new TextLoader.Column("FareAmount", DataKind.Single, 10)
                }
            }
                );

            // load the data
            Console.Write("Loading training data....");
            var dataView = textLoader.Load(dataPath);

            Console.WriteLine("done");

            // split into a training and test partition
            var partitions = mlContext.Data.TrainTestSplit(dataView, testFraction: 0.2);

            // set up a learning pipeline
            var pipeline = mlContext.Transforms.CopyColumns(
                inputColumnName: "FareAmount",
                outputColumnName: "Label")

                           // one-hot encode all text features
                           .Append(mlContext.Transforms.Categorical.OneHotEncoding("VendorId"))
                           .Append(mlContext.Transforms.Categorical.OneHotEncoding("RateCode"))
                           .Append(mlContext.Transforms.Categorical.OneHotEncoding("PaymentType"))

                           // combine all input features into a single column
                           .Append(mlContext.Transforms.Concatenate(
                                       "Features",
                                       "VendorId",
                                       "RateCode",
                                       "PassengerCount",
                                       "TripDistance",
                                       "PaymentType"))

                           // cache the data to speed up training
                           .AppendCacheCheckpoint(mlContext)

                           // use the fast tree learner
                           .Append(mlContext.Regression.Trainers.FastTree());

            // train the model
            Console.Write("Training the model....");
            var model = pipeline.Fit(partitions.TrainSet);

            Console.WriteLine("done");

            // get a set of predictions
            Console.Write("Evaluating the model....");
            var predictions = model.Transform(partitions.TestSet);

            // get regression metrics to score the model
            var metrics = mlContext.Regression.Evaluate(predictions, "Label", "Score");

            Console.WriteLine("done");

            // show the metrics
            Console.WriteLine();
            Console.WriteLine($"Model metrics:");
            Console.WriteLine($"  RMSE:{metrics.RootMeanSquaredError:#.##}");
            Console.WriteLine($"  MSE: {metrics.MeanSquaredError:#.##}");
            Console.WriteLine($"  MAE: {metrics.MeanAbsoluteError:#.##}");
            Console.WriteLine();

            // create a prediction engine for one single prediction
            var predictionFunction = mlContext.Model.CreatePredictionEngine <TaxiTrip, TaxiTripFarePrediction>(model);

            // prep a single taxi trip
            var taxiTripSample = new TaxiTrip()
            {
                VendorId       = "2",
                RateCode       = "1",
                PassengerCount = 1,
                TripDistance   = 3.75f,
                PaymentType    = "1",
                FareAmount     = 0 // the model will predict the actual fare for this trip
            };

            // make the prediction
            var prediction = predictionFunction.Predict(taxiTripSample);

            // sho the prediction
            Console.WriteLine($"Single prediction:");
            Console.WriteLine($"  Predicted fare: {prediction.FareAmount:0.####}");
        }
Exemple #16
0
        //static TextLoader _textLoader; not used

        static void Main(string[] args)
        {
            // STEP 1: Common data loading configuration
            MLContext mlContext = new MLContext(seed: 0);

            IDataView baseTrainingDataView = mlContext.Data.LoadFromTextFile <TaxiTrip>(_trainDataPath, hasHeader: true, separatorChar: ','); // ReadFromTextFile is deprecated
            IDataView testDataView         = mlContext.Data.LoadFromTextFile <TaxiTrip>(_testDataPath, hasHeader: true, separatorChar: ',');

            //Sample code of removing extreme data like "outliers" for FareAmounts higher than $150 and lower than $1 which can be error-data
            var       cnt = baseTrainingDataView.GetColumn <float>(mlContext, nameof(TaxiTrip.FareAmount)).Count();
            IDataView trainingDataView = mlContext.Data.FilterRowsByColumn(baseTrainingDataView, nameof(TaxiTrip.FareAmount), lowerBound: 1, upperBound: 150);


            // STEP 2: Common data process configuration with pipeline data transformations
            var dataProcessPipeline = mlContext.Transforms.CopyColumns(outputColumnName: DefaultColumnNames.Label, inputColumnName: nameof(TaxiTrip.FareAmount))
                                      .Append(mlContext.Transforms.Categorical.OneHotEncoding(outputColumnName: "VendorIdEncoded", inputColumnName: nameof(TaxiTrip.VendorId)))
                                      .Append(mlContext.Transforms.Categorical.OneHotEncoding(outputColumnName: "RateCodeEncoded", inputColumnName: nameof(TaxiTrip.RateCode)))
                                      .Append(mlContext.Transforms.Categorical.OneHotEncoding(outputColumnName: "PaymentTypeEncoded", inputColumnName: nameof(TaxiTrip.PaymentType)))
                                      .Append(mlContext.Transforms.Normalize(outputColumnName: nameof(TaxiTrip.PassengerCount), mode: NormalizerMode.MeanVariance))
                                      .Append(mlContext.Transforms.Normalize(outputColumnName: nameof(TaxiTrip.TripTime), mode: NormalizerMode.MeanVariance))
                                      .Append(mlContext.Transforms.Normalize(outputColumnName: nameof(TaxiTrip.TripDistance), mode: NormalizerMode.MeanVariance))
                                      .Append(mlContext.Transforms.Concatenate(DefaultColumnNames.Features, "VendorIdEncoded", "RateCodeEncoded", "PaymentTypeEncoded", nameof(TaxiTrip.PassengerCount)
                                                                               , nameof(TaxiTrip.TripTime), nameof(TaxiTrip.TripDistance)));


            // STEP 3: Set the training algorithm, then create and config the modelBuilder - Selected Trainer (SDCA Regression algorithm)
            var trainer          = mlContext.Regression.Trainers.StochasticDualCoordinateAscent(labelColumnName: DefaultColumnNames.Label, featureColumnName: DefaultColumnNames.Features);
            var trainingPipeline = dataProcessPipeline.Append(trainer);


            //STEP 4: Train the model fitting to the DataSet
            //The pipeline is trained on the dataset that has been loaded and transformed.
            ITransformer trainedModel = trainingPipeline.Fit(trainingDataView); // changed var trainedModel to ITransformer trainedModel


            // STEP 5: Evaluate the model and show accuracy stats
            // create a copy of the testDataView in IDataView format since IDataView is immutable?
            IDataView predictions = trainedModel.Transform(testDataView);
            // assign label column (original regression value) and score column (predictive regression values) to columns in data i.e. predictions
            // apply metrics e.g. root mean squared error on data
            var metrics = mlContext.Regression.Evaluate(predictions, label: DefaultColumnNames.Label, score: DefaultColumnNames.Score);

            // print the results of the metrics
            Common.ConsoleHelper.PrintRegressionMetrics(trainer.ToString(), metrics);


            // STEP 6: Save/persist the trained model to a .ZIP file
            using (var fs = File.Create(_modelPath))
                trainedModel.SaveTo(mlContext, fs);

            Console.WriteLine("The model is saved to {0}", _modelPath);


            // STEP 7: Prediction
            //Sample:
            //vendor_id,rate_code,passenger_count,trip_time_in_secs,trip_distance,payment_type,fare_amount
            //VTS,1,1,1140,3.75,CRD,15.5

            var taxiTripSample = new TaxiTrip()
            {
                VendorId       = "VTS",
                RateCode       = "1",
                PassengerCount = 1,
                TripTime       = 1140,
                TripDistance   = 3.75f,
                PaymentType    = "CRD",
                FareAmount     = 0 // To predict. Actual/Observed = 15.5
            };


            using (var stream = new FileStream(_modelPath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                trainedModel = mlContext.Model.Load(stream);
            }

            // Create prediction engine related to the loaded trained model
            var predEngine = trainedModel.CreatePredictionEngine <TaxiTrip, TaxiTripFarePrediction>(mlContext);

            //Score
            var resultprediction = predEngine.Predict(taxiTripSample);

            Console.WriteLine($"**********************************************************************");
            Console.WriteLine($"Predicted fare: {resultprediction.FareAmount:0.####}, actual fare: 15.5");
            Console.WriteLine($"**********************************************************************");
        }
Exemple #17
0
        static async Task Main(string[] args)
        {
            var regressorTypes = new [] {
                typeof(FastForestRegressor),
                typeof(FastTreeTweedieRegressor),
                typeof(GeneralizedAdditiveModelRegressor),
                // typeof(OnlineGradientDescentRegressor),
                // typeof(OrdinaryLeastSquaresRegressor),
                typeof(PoissonRegressor),
                typeof(StochasticDualCoordinateAscentRegressor),
            };

            var taxiTripTests = File.ReadAllLines(_testdatapath)
                                .Skip(1)
                                .Select(x => {
                var fields   = x.Split(',');
                var taxiTrip = new TaxiTrip {
                    VendorId       = fields[0],
                    RateCode       = fields[1],
                    PassengerCount = float.Parse(fields[2]),
                    TripTime       = float.Parse(fields[3]),
                    TripDistance   = float.Parse(fields[4]),
                    PaymentType    = fields[5],
                    FareAmount     = float.Parse(fields[6])
                };
                return(taxiTrip, taxiTripFarePrediction: (TaxiTripFarePrediction)null);
            })
                                //.Take(500)
                                .ToArray();

            Func <PredictionModel <TaxiTrip, TaxiTripFarePrediction> > trainFunc = Train <FastForestRegressor>;

            foreach (var regressorType in regressorTypes)
            {
                var methodInfo = trainFunc.Method.GetGenericMethodDefinition().MakeGenericMethod(regressorType);
                Console.WriteLine();
                Console.WriteLine("--- " + regressorType.Name + " ---");
                var model = (PredictionModel <TaxiTrip, TaxiTripFarePrediction>)methodInfo.Invoke(null, null);
                Evaluate(model);
                var prediction = model.Predict(TestTrips.Trip1);
                Console.WriteLine("Predicted fare: {0}, actual fare: 29.5", prediction.FareAmount);

                Console.WriteLine();
                Console.Write("Begin test...");
                var what = model.Predict(taxiTripTests.Select(x => x.taxiTrip)).ToList();
                Parallel.ForEach(what, (x, state, i) => taxiTripTests[(int)i].taxiTripFarePrediction = x);
                Console.WriteLine("Done.");

                var diffs = taxiTripTests.Select(x => Math.Abs(x.taxiTrip.FareAmount - x.taxiTripFarePrediction.FareAmount))
                            .AsParallel()
                            .OrderBy(x => x)
                            .ToList();
                var okay = diffs.Take(diffs.Count - 2)
                           .Where((x, i) => x > 0.75 & x < 450)
                           .Where((x, i) => i % 1000 == 0)
                           .Select((x, i) => i + "," + x);

                File.WriteAllLines($@".\Data\{regressorType.Name}_ModelErrors.csv", okay);
                Console.WriteLine(taxiTripTests.Min(x => Math.Abs(x.taxiTrip.FareAmount - x.taxiTripFarePrediction.FareAmount)));
                Console.WriteLine(taxiTripTests.Max(x => Math.Abs(x.taxiTrip.FareAmount - x.taxiTripFarePrediction.FareAmount)));
                Console.WriteLine(taxiTripTests.Average(x => Math.Abs(x.taxiTrip.FareAmount - x.taxiTripFarePrediction.FareAmount)));
            }
        }
Exemple #18
0
        private static void TestRandomPrediction(MLContext mlContext)
        {
            Random rand = new Random();

            //load the model
            ITransformer loadedModel;

            using (var stream = new FileStream(_modelPath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                loadedModel = mlContext.Model.Load(stream);
            }

            //Prediction test
            // Create prediction function and make prediction.
            var predictionFunction = loadedModel.CreatePredictionEngine <TaxiTrip, TaxiTripFarePrediction>(mlContext);
            //
            //Sample:
            //vendor_id,rate_code,passenger_count,trip_time_in_secs,trip_distance,payment_type,fare_amount
            //VTS,1,1,1140,3.75,CRD,15.5

            var taxiTripSample = new TaxiTrip()
            {
                RateCode       = "1",                     // 1 - metered, 2 - fixed
                PassengerCount = rand.Next(1, 6),         // 1 - 6 passengers
                TripTime       = rand.Next(6, 1100) * 10, // nearest 10 secs.
                TripDistance   = ((float)rand.NextDouble() * 100.0f) + 0.1f,
                FareAmount     = 0                        // To predict. Actual/Observed = 15.5
            };

            //
            // Pick a vendor ID
            if (rand.Next() % 2 == 0)
            {
                taxiTripSample.VendorId = "VTS";
            }
            else
            {
                taxiTripSample.VendorId = "CMT";
            }

            if (rand.Next() % 2 == 0)
            {
                taxiTripSample.PaymentType = "CSH";
            }
            else
            {
                taxiTripSample.PaymentType = "CRD";
            }

            var prediction = predictionFunction.Predict(taxiTripSample);

            WriteLine($"**********************************************************************");
            WriteLine($"VendorId:       {taxiTripSample.VendorId}");
            WriteLine($"RateCode:       {taxiTripSample.RateCode}");
            WriteLine($"PassengerCount: {taxiTripSample.PassengerCount}");
            WriteLine($"TripTime:       {taxiTripSample.TripTime}");
            WriteLine($"TripDistance:   {taxiTripSample.TripDistance}");
            WriteLine($"PaymentType:    {taxiTripSample.PaymentType}");
            WriteLine($"Predicted fare: {prediction.FareAmount:0.####}, actual fare: 15.5");
            WriteLine($"**********************************************************************\n");
        }
Exemple #19
0
        static void Main(string[] args)
        {
            Helper.PrintLine("创建 MLContext...");
            MLContext    mlContext = new MLContext(seed: 0);
            ITransformer model;

            if (File.Exists(ModelPath))
            {
                Helper.PrintLine("加载神经网络模型...");
                model = mlContext.Model.Load(ModelPath, out DataViewSchema inputScema);
            }
            else
            {
                // 训练数据集合
                IDataView trainingDataView = mlContext.Data.LoadFromTextFile <TaxiTrip>(TrainingDataPath, hasHeader: true, separatorChar: ',');

                // 创建神经网络管道
                Helper.PrintLine("创建神经网络管道...");
                var pipeline = mlContext.Transforms.CopyColumns(outputColumnName: "Label", inputColumnName: "FareAmount")
                               .Append(mlContext.Transforms.Categorical.OneHotEncoding(outputColumnName: "VendorIdEncoded", inputColumnName: "VendorId"))
                               .Append(mlContext.Transforms.Categorical.OneHotEncoding(outputColumnName: "RateCodeEncoded", inputColumnName: "RateCode"))
                               .Append(mlContext.Transforms.Categorical.OneHotEncoding(outputColumnName: "PaymentTypeEncoded", inputColumnName: "PaymentType"))
                               .Append(mlContext.Transforms.Concatenate("Features", "VendorIdEncoded", "RateCodeEncoded", "PassengerCount", "TripTime", "TripDistance", "PaymentTypeEncoded"))
                               .Append(mlContext.Regression.Trainers.FastTree());

                // 开始训练神经网络
                Helper.PrintSplit();
                Helper.PrintLine("开始训练神经网络...");
                model = pipeline.Fit(trainingDataView);
                Helper.PrintLine("训练神经网络完成");
                Helper.PrintSplit();

                Helper.PrintLine($"导出神经网络模型...");
                mlContext.Model.Save(model, trainingDataView.Schema, ModelPath);
            }

            // 测试
            Helper.PrintLine("评估神经网络:");
            var testDataView = mlContext.Data.LoadFromTextFile <TaxiTrip>(TestDataPath, hasHeader: true, separatorChar: ',');
            var testMetrics  = mlContext.Regression.Evaluate(model.Transform(testDataView), "Label", "Score");

            Helper.PrintLine($"\t=>R^2: {testMetrics.RSquared:0.###}");
            Helper.PrintLine($"\t=>RMS error: {testMetrics.RootMeanSquaredError:0.###}");

            // 预测
            Helper.PrintLine("预测:");
            var predictionEngine = mlContext.Model.CreatePredictionEngine <TaxiTrip, TaxiTripFarePrediction>(model);
            var taxiTripSample   = new TaxiTrip()
            {
                VendorId       = "VTS",
                RateCode       = "1",
                PassengerCount = 1,
                TripTime       = 1140,
                TripDistance   = 3.75f,
                PaymentType    = "CRD",
                FareAmount     = 0 // To predict. Actual/Observed = 15.5
            };
            var prediction = predictionEngine.Predict(taxiTripSample);

            Helper.PrintLine($"预测价格: {prediction.FareAmount:0.####}, actual fare: 15.5");

            Helper.Exit(0);
        }
        private static void Predict(PredictionModel <TaxiTrip, TaxiTripFarePrediction> model, TaxiTrip trip1)
        {
            var prediction = model.Predict(trip1);

            Console.WriteLine("Predicted fare: {0}, actual fare: 29.5", prediction.FareAmount);
        }