public List <TaxiFarePrediction> RunMultiplePredictions(int numberOfPredictions)
        {
            // Load data as input for predictions.
            IDataView inputDataForPredictions = context.Data.LoadFromTextFile <TaxiTrip>(_datasetFile, hasHeader: true, separatorChar: ',');

            Console.WriteLine("Predictions from saved model:");

            Console.WriteLine($"\n \n Test {numberOfPredictions} transactions, from the test datasource, that should be predicted as fraud (true):");

            var transactionList = new List <TaxiFarePrediction>();
            TaxiTripFarePredictionWithContribution prediction;
            TaxiFarePrediction explainedPrediction;

            context.Data.CreateEnumerable <TaxiTrip>(inputDataForPredictions, reuseRowObject: false)
            .Take(numberOfPredictions)
            .Select(testData => testData)
            .ToList()
            .ForEach(testData =>
            {
                testData.PrintToConsole();
                prediction          = predictionEngine.Predict(testData);
                explainedPrediction = new TaxiFarePrediction(prediction.FareAmount, prediction.GetFeatureContributions(model.GetOutputSchema(inputDataForPredictions.Schema)));
                transactionList.Add(explainedPrediction);
            });

            return(transactionList);
        }
        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();
        }
Example #3
0
        // 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 TaxiFarePrediction CreateSingleDataSample(string dataFilePath)
        {
            // Create MLContext
            MLContext mlContext = new MLContext();

            // Load dataset
            IDataView dataView = mlContext.Data.LoadFromTextFile <TaxiFarePrediction>(
                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)
            TaxiFarePrediction sampleForPrediction = mlContext.Data.CreateEnumerable <TaxiFarePrediction>(dataView, false)
                                                     .First();

            return(sampleForPrediction);
        }