static void Main(string[] args) { try { var context = new MLContext(seed: 0); var modelFile = AppDomain.CurrentDomain.BaseDirectory + "MLModel.zip"; var model = context.Model.Load(filePath: modelFile, out DataViewSchema inputSchema); var predictionEngine = context.Model.CreatePredictionEngine <TaxiFareInput, TaxiFarePrediction>(model); //CMT,1,1,581,5.4,CSH,16.5 var modelInput = new TaxiFareInput { Rate_code = 1, Passenger_count = 1, Trip_time_in_secs = 581, Trip_distance = 5.4F, Payment_type = "CSH", Fare_amount = 0 }; var modelOutput = predictionEngine.Predict(modelInput); Console.WriteLine("Predicted Fare : " + modelOutput.Score.ToString() + ", " + "Actual Fare : 16.5"); } catch (Exception exceptionObject) { Console.WriteLine("Error Occurred, Details : " + exceptionObject.Message); } Console.WriteLine("End of App!"); Console.ReadLine(); }
public TaxiFarePrediction GetTaxiFare(TaxiFareInput taxiFareInput) { var validation = taxiFareInput != default(TaxiFareInput) && taxiFareInput.Passenger_count >= 1 && taxiFareInput.Trip_distance >= 1 && taxiFareInput.Trip_time_in_secs >= 1; if (!validation) { throw new ArgumentException("Invalid Taxi Fare Input Specified!"); } var prediction = predictionEngine.Predict(taxiFareInput); return(prediction); }
static void Main(string[] args) { // Create single instance of sample data from first line of dataset for model input TaxiFareInput sampleData = CreateSingleDataSample(DATA_FILEPATH); // Make a single prediction on the sample data and print results TaxiFarePrediction predictionResult = ConsumeModel.Predict(sampleData); Console.WriteLine("Using model to make single prediction -- Comparing actual Fare_amount with predicted Fare_amount from sample data...\n\n"); Console.WriteLine($"vendor_id: {sampleData.Vendor_id}"); Console.WriteLine($"rate_code: {sampleData.Rate_code}"); Console.WriteLine($"passenger_count: {sampleData.Passenger_count}"); Console.WriteLine($"trip_time_in_secs: {sampleData.Trip_time_in_secs}"); Console.WriteLine($"trip_distance: {sampleData.Trip_distance}"); Console.WriteLine($"payment_type: {sampleData.Payment_type}"); Console.WriteLine($"\n\nActual Fare_amount: {sampleData.Fare_amount} \nPredicted Fare_amount: {predictionResult.Score}\n\n"); Console.WriteLine("=============== End of process, hit any key to finish ==============="); Console.ReadKey(); }
// Change this code to create your own sample data #region CreateSingleDataSample // Method to load single row of dataset to try a single prediction private static TaxiFareInput CreateSingleDataSample(string dataFilePath) { // Create MLContext MLContext mlContext = new MLContext(); // Load dataset IDataView dataView = mlContext.Data.LoadFromTextFile <TaxiFareInput>( path: dataFilePath, hasHeader: true, separatorChar: ',', allowQuoting: true, allowSparse: false); // Use first line of dataset as model input // You can replace this with new test data (hardcoded or from end-user application) TaxiFareInput sampleForPrediction = mlContext.Data.CreateEnumerable <TaxiFareInput>(dataView, false) .First(); return(sampleForPrediction); }
public IActionResult CalculateTaxiFare([FromBody] TaxiFareInput taxiFareInput) { var validation = this.taxiFarePredictionService != default(ITaxiFarePredictionService) && taxiFareInput != default(TaxiFareInput) && taxiFareInput.Passenger_count >= MIN_PASSENGERS && taxiFareInput.Trip_distance >= MIN_TRIP_DISTANCE && taxiFareInput.Trip_time_in_secs >= MIN_TRIP_IN_SECS; if (!validation) { return(new BadRequestResult()); } try { var taxiFarePrediction = this.taxiFarePredictionService.GetTaxiFare(taxiFareInput); return(Ok(taxiFarePrediction)); } catch (Exception exceptionObject) { return(new BadRequestObjectResult(exceptionObject.Message)); } }