Пример #1
0
        private static void TestSinglePrediction(LocalEnvironment mlcontext, ITransformer model)
        {
            //Prediction test
            // Create prediction engine and make prediction.
            var engine = model.MakePredictionFunction <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 = 1,
                TripTime       = 1140,
                TripDistance   = 3.75f,
                PaymentType    = "CRD",
                FareAmount     = 0 // To predict. Actual/Observed = 15.5
            };

            var prediction = engine.Predict(taxiTripSample);

            Console.WriteLine($"**********************************************************************");
            Console.WriteLine($"Predicted fare: {prediction.FareAmount:0.####}, actual fare: 29.5");
            Console.WriteLine($"**********************************************************************");
        }
Пример #2
0
        private static void TestSinglePrediction(MLContext 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 = 1,
                TripTime       = 1140,
                TripDistance   = 3.75f,
                PaymentType    = "CRD",
                FareAmount     = 0 // To predict. Actual/Observed = 15.5
            };

            ///
            ITransformer 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($"**********************************************************************");
        }
Пример #3
0
        private static void TestSinglePrediction(MLContext 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 = 1,
                TripTime = 1140,
                TripDistance = 3.75f,
                PaymentType = "CRD",
                FareAmount = 0 // To predict. Actual/Observed = 15.5
            };

            ///
            ITransformer trainedModel;
            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 predFunction = trainedModel.MakePredictionFunction<TaxiTrip, TaxiTripFarePrediction>(mlContext);

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

            Console.WriteLine($"**********************************************************************");
            Console.WriteLine($"Predicted fare: {resultprediction.FareAmount:0.####}, actual fare: 15.5");
            Console.WriteLine($"**********************************************************************");
        }
Пример #4
0
        private static void TestSinglePrediction(MLContext 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 = 1,
                TripTime       = 1140,
                TripDistance   = 3.75f,
                PaymentType    = "CRD",
                FareAmount     = 0 // To predict. Actual/Observed = 15.5
            };

            var modelScorer = new Common.ModelScorer <TaxiTrip, TaxiTripFarePrediction>(mlContext);

            modelScorer.LoadModelFromZipFile(ModelPath);
            var resultprediction = modelScorer.PredictSingle(taxiTripSample);

            Console.WriteLine($"**********************************************************************");
            Console.WriteLine($"Predicted fare: {resultprediction.FareAmount:0.####}, actual fare: 15.5");
            Console.WriteLine($"**********************************************************************");
        }
Пример #5
0
        private static void LoadInitialDataInCache(string initialData)
        {
            var    currentLine = string.Empty;
            string datapath    = ConfigurationManager.AppSettings["BaseDataRelativePath"];

            if (String.IsNullOrEmpty(datapath))
            {
                Console.WriteLine("Data path cannot be null or empty.");
                return;
            }

            string filepath = GetAbsolutePath($"{datapath}/taxi-fare-initial.csv");
            IDistributedList <TaxiTrip> initialDataList = Cache.DataTypeManager.GetList <TaxiTrip>(initialData);

            if (initialDataList == null)
            {
                initialDataList = Cache.DataTypeManager.CreateList <TaxiTrip>(initialData);

                using (StreamReader r = new StreamReader(filepath))
                {
                    while ((currentLine = r.ReadLine()) != null)
                    {
                        string[] incomingData = currentLine.Split(",");
                        TaxiTrip data         = new TaxiTrip();

                        data.RateCode       = float.Parse(incomingData[0]);
                        data.PassengerCount = float.Parse(incomingData[1]);
                        data.TripTime       = float.Parse(incomingData[2]);
                        data.TripDistance   = float.Parse(incomingData[3]);
                        data.FareAmount     = float.Parse(incomingData[4]);

                        initialDataList.Add(data);
                        TrainingData.Add(data);
                    }
                }
            }
        }