dotnet / dotnet/machinelearning-samples
The prediction of taxiTripSample dont work
- Dominant language
- PowerShell
- Stars
- 4.7k
- Forks
- 2.7k
- Avg merge
- 2d 22h
- Merged PRs (30d)
- 1
Description
https://docs.microsoft.com/en-us/dotnet/machine-learning/tutorials/predict-prices
Goal:
Predict that you retrieve the value 15.5 based on the instruction
```
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
};
Problem:
When I do the prediction based on the data above, I get value 0 for the FareAmount after the prediction process.
What part am I missing from the source code?
Info:
*I have used the data from the link
https://github.com/dotnet/machinelearning/blob/master/test/data/taxi-fare-train.csv
Thank you!
```
```
//*****************************************************************************************
//* *
//* This is an auto-generated file by Microsoft ML.NET CLI (Command-Line Interface) tool. *
//* *
//*****************************************************************************************
using System;
using System.IO;
using System.Linq;
using Microsoft.ML;
using Machinelearning_test2ML.Model.DataModels;
using machinelearning_test2ML.ConsoleApp;
namespace Machinelearning_test2ML.ConsoleApp
{
class Program
{
//Machine Learning model to load and use for predictions
private const string MODEL_FILEPATH = @"MLModel.zip";
//Dataset to use for predictions
private const string DATA_FILEPATH = @"C:{ }";
static void Main(string[] args)
{
MLContext mlContext = new MLContext();
// Training code used by ML.NET CLI and AutoML to generate the model
//ModelBuilder.CreateModel();
ITransformer mlModel = mlContext.Model.Load(GetAbsolutePath(MODEL_FILEPATH), out DataViewSchema inputSchema);
var predEngine = mlContext.Model.CreatePredictionEngine(mlModel);
// Create sample data to do a single prediction with it
ModelInput sampleData = CreateSingleDataSample(mlContext, DATA_FILEPATH);
// Try a single prediction
ModelOutput predictionResult = predEngine.Predict(sampleData);
Console.WriteLine($"Single Prediction --> Actual value: {sampleData.Fare_amount} | Predicted value: {predictionResult.Score}");
//------------
IDataView dataView = mlContext.Data.LoadFromTextFile(DATA_FILEPATH, hasHeader: true, separatorChar: ',');
var pipeline = mlContext.Transforms.CopyColumns(outputColumnName: "Label", inputColumnName: "FareAmount");
var model = pipeline.Fit(dataView);
var predictionFunction = mlContext.Model.CreatePredictionEngine(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("=============== End of process, hit any key to finish ===============");
Console.ReadKey();
}
// Method to load single row of data to try a single prediction
// You can change this code and create your own sample data here (Hardcoded or from any source)
private static ModelInput CreateSingleDataSample(MLContext mlContext, string dataFilePath)
{
// Read dataset to get a single row for trying a prediction
IDataView dataView = mlContext.Data.LoadFromTextFile(
path: dataFilePath,
hasHeader: true,
separatorChar: '\t',
allowQuoting: true,
allowSparse: false);
// Here (ModelInput object) you could provide new test data, hardcoded or from the end-user application, instead of the row from the file.
ModelInput sampleForPrediction = mlContext.Data.CreateEnumerable(dataView, false)
.First();
return sampleForPrediction;
}
public static string GetAbsolutePath(string relativePath)
{
FileInfo _dataRoot = new FileInfo(typeof(Program).Assembly.Location);
string assemblyFolderPath = _dataRoot.Directory.FullName;
string fullPath = Path.Combine(assemblyFolderPath, relativePath);
return fullPath;
}
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.