<jupyter_start><jupyter_text>ML.Net - Taxi Prediction Price Scenario: Regression model for Taxi faresRegression is a ML task type of supervised machine learning algorithms. A regression ML model predicts continuous value outputs (such as numbers). For instance, predicting the fare of a Taxi trip or predicting the price of a car is a regression problem.  <NAME> -> Cientista de Dados 👋(<EMAIL>)[![Linkedin Badge](https://img.shields.io/badge/-LinkedIn-blue?style=flat-square&logo=Linkedin&logoColor=white&link=https://www.linkedin.com/in/davi-ramos/)](https://www.linkedin.com/in/davi-ramos/)[![Twitter Badge](https://img.shields.io/badge/-Twitter-1DA1F2?style=flat-square&logo=Twitter&logoColor=white&link=https://twitter.com/Daviinfo/)](https://twitter.com/Daviinfo/)<jupyter_code>// ML.NET Nuget packages installation
#r "nuget:Microsoft.ML"

//Install XPlot package
#r "nuget:XPlot.Plotly"
using System;
using Microsoft.ML;
using System.IO;
using Microsoft.ML.Data;
using Microsoft.ML.Transforms;
using static Microsoft.ML.DataOperationsCatalog;
using XPlot.Plotly;
/// <summary>
/// The TaxiTrip class represents a single taxi trip.
/// </summary>
public class TaxiTrip
{
    [LoadColumn(0)] public string VendorId;
    [LoadColumn(5)] public string RateCode;
    [LoadColumn(3)] public float PassengerCount;
    [LoadColumn(4)] public float TripDistance;
    [LoadColumn(9)] public string PaymentType;
    [LoadColumn(10)] public float FareAmount;
}

/// <summary>
/// The TaxiTripFarePrediction class represents a single far prediction.
/// </summary>
public class TaxiTripFarePrediction
{
    [ColumnName("Score")] public float FareAmount;
}
// file paths to data files
static readonly string dataPath = Path.Combine(Environment.CurrentDirectory, "./Datasets/taxi/taxi-fare-train-small.csv");

/// <summary>
/// The main application entry point.
/// </summary>
/// <param name="args">The command line arguments.</param>

// create the machine learning context
var mlContext = new MLContext();

// set up the text loader 
var textLoader = mlContext.Data.CreateTextLoader(
    new TextLoader.Options() 
    {
        Separators = new[] { ',' },
        HasHeader = true,
        Columns = new[] 
        {
            new TextLoader.Column("VendorId", DataKind.String, 0),
            new TextLoader.Column("RateCode", DataKind.String, 5),
            new TextLoader.Column("PassengerCount", DataKind.Single, 3),
            new TextLoader.Column("TripDistance", DataKind.Single, 4),
            new TextLoader.Column("PaymentType", DataKind.String, 9),
            new TextLoader.Column("FareAmount", DataKind.Single, 10)
        }
    }
);

// load the data 
Console.Write("Loading training data....");
var dataView = textLoader.Load(dataPath);
Console.WriteLine("done");

// split into a training and test partition
TrainTestData partitions  = mlContext.Data.TrainTestSplit(dataView, testFraction: 0.2);
display(h4("Schema of training DataView:"));
display(partitions.TrainSet.Schema);<jupyter_output><empty_output><jupyter_text>Show a few rows of loaded data<jupyter_code>//Util class to preview loaded data in IDataView
public static List<TaxiTrip> Head(MLContext mlContext, IDataView dataView, int numberOfRows = 4)
{
    string msg = string.Format("DataView: Showing {0} rows with the columns", numberOfRows.ToString());
    display(msg);
          
    var rows = mlContext.Data.CreateEnumerable<TaxiTrip>(dataView, reuseRowObject: false)
                    .Take(numberOfRows)
                    .ToList();
    
    return rows;
}

display(h4("Showing a few rows from training DataView:"));

var fewRows = Head(mlContext, partitions.TrainSet, 5);
display(fewRows);<jupyter_output><empty_output><jupyter_text>Extract important input variables as arrays to be used for plotting<jupyter_code>//Extract some data into arrays for plotting:

int numberOfRows = 1000;
float[] fares = partitions.TrainSet.GetColumn<float>("FareAmount").Take(numberOfRows).ToArray();
float[] distances = partitions.TrainSet.GetColumn<float>("TripDistance").Take(numberOfRows).ToArray();
float[] passengerCounts = partitions.TrainSet.GetColumn<float>("PassengerCount").Take(numberOfRows).ToArray();<jupyter_output><empty_output><jupyter_text>Show a histogram: Distribution of taxi trips per fare cost<jupyter_code>// Distribution of taxi trips per cost
//XPlot Histogram reference: http://tpetricek.github.io/XPlot/reference/xplot-plotly-graph-histogram.html

var faresHistogram = Chart.Plot(new Graph.Histogram(){x = fares, autobinx = false, nbinsx = 20});
var layout = new Layout.Layout(){title="Distribution of taxi trips per cost"};
faresHistogram.WithLayout(layout);
faresHistogram.WithXTitle("Fare ranges");
faresHistogram.WithYTitle("Number of trips");
display(faresHistogram);<jupyter_output><empty_output><jupyter_text>Plot Fares depending on trip's passengers<jupyter_code>// Plot Fare depending on Passengers

int numberOfRows = 2000;
float[] fares = partitions.TrainSet.GetColumn<float>("FareAmount").Take(numberOfRows).ToArray();
float[] passengerCounts = partitions.TrainSet.GetColumn<float>("PassengerCount").Take(numberOfRows).ToArray();

float[] distances = partitions.TrainSet.GetColumn<float>("TripDistance").Take(numberOfRows).ToArray();


var chartFareVsPassengers = Chart.Plot(
    new Graph.Scatter()
    {
        x = passengerCounts,
        y = fares,
        mode = "markers",
    }
);

var layout = new Layout.Layout(){title="Plot Fare depending on Passengers"};
chartFareVsPassengers.WithLayout(layout);
chartFareVsPassengers.Width = 500;
chartFareVsPassengers.Height = 500;
chartFareVsPassengers.WithXTitle("Passengers");
chartFareVsPassengers.WithYTitle("Fares");
chartFareVsPassengers.WithLegend(false);

display(chartFareVsPassengers);
display(h1("Apply Data Transformations pipeline"));
// set up a learning pipeline
var pipeline = mlContext.Transforms.CopyColumns(
    inputColumnName:"FareAmount", 
    outputColumnName:"Label")

    // one-hot encode all text features
    .Append(mlContext.Transforms.Categorical.OneHotEncoding("VendorId"))
    .Append(mlContext.Transforms.Categorical.OneHotEncoding("RateCode"))
    .Append(mlContext.Transforms.Categorical.OneHotEncoding("PaymentType"))

    // combine all input features into a single column 
    .Append(mlContext.Transforms.Concatenate(
        "Features", 
        "VendorId", 
        "RateCode", 
        "PassengerCount", 
        "TripDistance", 
        "PaymentType"))

    // cache the data to speed up training
    .AppendCacheCheckpoint(mlContext)

    // use the fast tree learner 
    .Append(mlContext.Regression.Trainers.OnlineGradientDescent(labelColumnName: "FareAmount", featureColumnName: "Features"));


// train the model
Console.Write("Training the model....");
var model = pipeline.Fit(partitions.TrainSet);
Console.WriteLine("done");
// get a set of predictions 
Console.Write("Evaluating the model....");
var predictions = model.Transform(partitions.TestSet);


// get regression metrics to score the model
var metrics = mlContext.Regression.Evaluate(predictions, labelColumnName: "FareAmount", scoreColumnName: "Score");
display(metrics);
// create a prediction engine for one single prediction
var prediction  = mlContext.Model.CreatePredictionEngine<TaxiTrip, TaxiTripFarePrediction>(model).Predict(
    new TaxiTrip()
    {
        VendorId = "VTS",
        RateCode = "1",
        PassengerCount = 1,
        TripDistance = 3.75f,
        PaymentType = "1",
        FareAmount = 0 // actual fare for this trip = 15.5
    });

// show the prediction
Console.WriteLine($"Single prediction:");
Console.WriteLine($"  Predicted fare: {prediction.FareAmount:0.####}");
Console.WriteLine($"  Actual fare: 15.5");<jupyter_output>Single prediction:
  Predicted fare: 0
  Actual fare: 15.5
{'fraction_non_alphanumeric': 0.1243423585268831, 'fraction_numerical': 0.007570896958809188, 'mean_word_length': 4.551282051282051, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 40, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '433099', 'license': 'MIT', 'n_tokens_mistral': 2433, 'n_tokens_neox': 2309, 'n_words': 557, 'path': 'Notebooks/ML.Net - Taxi Prediction Price.ipynb', 'repo_name': 'DaviRamos/ML.Net', 'size': '7809'}