예제 #1
0
        private static async Task runTaxiFarePredictionAsync()
        {
            var modelInput = new List <TaxiFarePrediction.ModelInput>
            {
                new TaxiFarePrediction.ModelInput
                {
                    VendorId       = getTaxiFareInputStringValue(TaxiFareInputType.VendorId),
                    RateCode       = getTaxiFareInputStringValue(TaxiFareInputType.RateCode),
                    PassengerCount = getTaxiFareInputFloatValue(TaxiFareInputType.PassengerCount),
                    TripDistance   = getTaxiFareInputFloatValue(TaxiFareInputType.TripDistance),
                    PaymentType    = getTaxiFareInputStringValue(TaxiFareInputType.PaymentType)
                }
            };

            var runnerRequest = new TaxiFarePrediction.RunnerRequest
            {
                ModelInput = modelInput
            };

            var runnerResponse = await TaxiFarePrediction.ModelRunner.Instance.RunClassificationAsync(runnerRequest);

            if (!runnerResponse.Success)
            {
                Console.WriteLine($"Text sentiment analysis failed: {runnerResponse.Message}");
            }
        }
예제 #2
0
        /// <summary>
        /// Uses ML.NET to predict taxi fare based on input an trained model.
        /// </summary>
        /// <param name="runnerRequest">Request with input needed for taxi fare prediction.</param>
        /// <returns>Taxi fare prediction result.</returns>
        public async Task <RunnerResponse> RunClassificationAsync(RunnerRequest runnerRequest)
        {
            try
            {
                var modelBuilder = new ModelBuilder();
                var trainedModel = await modelBuilder.TrainAsync();

                var modelMetrics = modelBuilder.Evaluate(trainedModel);

                var modelInput = runnerRequest.ModelInput
                                 .Select(p => new DataModel
                {
                    VendorId       = p.VendorId,
                    RateCode       = p.RateCode,
                    PassengerCount = p.PassengerCount,
                    TripDistance   = p.TripDistance,
                    PaymentType    = p.PaymentType
                })
                                 .ToList();
                var modelOutput = modelBuilder.Predict(trainedModel, modelInput)
                                  .Select(p => new ModelOutput
                {
                    PredictedFareAmount = p.FareAmount
                })
                                  .ToList();

                return(new RunnerResponse
                {
                    Success = true,
                    ModelOutput = modelOutput
                });
            }
            catch (Exception ex)
            {
                return(new RunnerResponse
                {
                    Success = false,
                    Message = ex.ToExceptionMessage()
                });
            }
        }