How to predict multiple labels with ML.NET using regression task?
Asked Answered
G

1

6

I'm very new to machine learning and I've stumbled upon the following problem. Considering an official NYC Taxi fare amount prediction tutorial, let's say I'd like to predict another real value, e.g. TripTime. I've modified my code as follows:

public class TripFarePrediction // this class is used to store prediction result
{
    [ColumnName("Score")]
    public float FareAmount { get; set; }

    [ColumnName("Score2")]
    public float TripTime { get; set; }
}


private static ITransformer Train(MLContext mlContext, string trainDataPath)
{
    IDataView dataView = _textLoader.Read(trainDataPath);
    var pipelineForTripTime = mlContext.Transforms.CopyColumns("Label", "TripTime")
    .Append(mlContext.Transforms.Categorical.OneHotEncoding("VendorId"))
    .Append(mlContext.Transforms.Categorical.OneHotEncoding("RateCode"))
    .Append(mlContext.Transforms.Categorical.OneHotEncoding("PaymentType"))
    .Append(mlContext.Transforms.Concatenate("Features", "VendorId", "RateCode", "PassengerCount", "TripDistance", "PaymentType"))
    .Append(mlContext.Regression.Trainers.FastTree());

    var pipelineForFareAmount = mlContext.Transforms.CopyColumns("Label", "FareAmount")
    .Append(mlContext.Transforms.Categorical.OneHotEncoding("VendorId"))
    .Append(mlContext.Transforms.Categorical.OneHotEncoding("RateCode"))
    .Append(mlContext.Transforms.Categorical.OneHotEncoding("PaymentType"))
    .Append(mlContext.Transforms.Concatenate("Features", "VendorId", "RateCode", "PassengerCount", "TripDistance", "PaymentType"))
    .Append(mlContext.Regression.Trainers.FastTree());



    var model = pipelineForTripTime.Append(pipelineForFareAmount).Fit(dataView);
    SaveModelAsFile(mlContext, model);
    return model;
}

The first value (FareAmount) is predicted 'correctly' (value is other than zero), but the second one (TripTime) is zero. My question is how do I predict two or more labels at once or at least using the same model? Is this even possible? I'm using .NET Core 2.2 and ML.NET 0.10.0 to accomplish this task. Thank you in advance for any help.

Galiot answered 14/2, 2019 at 13:29 Comment(0)
B
6

Probably it's not working, because Fit() only returns "Label" and "Score"

Look here: here

Your Score from "TripTime" is overwritten by "FareAmount".

I guess, you have to build two models.

edited: you can try this. Copy "Score" to the right place.

public class TripFarePrediction // this class is used to store prediction result
{
    [ColumnName("fareAmount")]
    public float FareAmount { get; set; }

    [ColumnName("tripTime")]
    public float TripTime { get; set; }
}


private static ITransformer Train(MLContext mlContext, string trainDataPath)
{
    IDataView dataView = _textLoader.Read(trainDataPath);
    var pipelineForTripTime = mlContext.Transforms.CopyColumns("Label", "TripTime")
    .Append(mlContext.Transforms.Categorical.OneHotEncoding("VendorId"))
    .Append(mlContext.Transforms.Categorical.OneHotEncoding("RateCode"))
    .Append(mlContext.Transforms.Categorical.OneHotEncoding("PaymentType"))
    .Append(mlContext.Transforms.Concatenate("Features", "VendorId", "RateCode", "PassengerCount", "TripDistance", "PaymentType"))
    .Append(mlContext.Regression.Trainers.FastTree())
    .Append(mlContext.Transforms.CopyColumns(outputcolumn: "tripTime", inputcolumn: "Score"));

    var pipelineForFareAmount = mlContext.Transforms.CopyColumns("Label", "FareAmount")
    .Append(mlContext.Transforms.Categorical.OneHotEncoding("VendorId"))
    .Append(mlContext.Transforms.Categorical.OneHotEncoding("RateCode"))
    .Append(mlContext.Transforms.Categorical.OneHotEncoding("PaymentType"))
    .Append(mlContext.Transforms.Concatenate("Features", "VendorId", "RateCode", "PassengerCount", "TripDistance", "PaymentType"))
    .Append(mlContext.Regression.Trainers.FastTree())
    .Append(mlContext.Transforms.CopyColumns(outputcolumn: "fareAmount", inputcolumn: "Score"));



    var model = pipelineForTripTime.Append(pipelineForFareAmount).Fit(dataView);
    SaveModelAsFile(mlContext, model);
    return model;
}
Babara answered 14/2, 2019 at 15:32 Comment(4)
Thanks for sharing this link. Is there any way to train model once (without using "Label" column) and then predict its different labels? This seems to be a good idea since you have one model you're working on, and training multiple models feels counterintuitive. Or am I wrong and multi-model approach is popular and widely used?Gunzburg
Can you try my suggested solution?Babara
Your solution works fine, but still predictions occur for different models. My question was how to use one single model to predict multiple labels, but I guess it's rather impossible or very hard for a beginner like me. Thanks for your time!Gunzburg
@WaldiTUD, I have a similar issue to the OP, but should the label column of each model not be included as a feature column in the other model in order to ensure that the values from that column in the data set are involved in the prediction in the model? I've asked this as a separate question here.Betrothed

© 2022 - 2024 — McMap. All rights reserved.