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

旧巷老猫 提交于 2020-01-11 10:25:08

问题


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.


回答1:


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;
}


来源:https://stackoverflow.com/questions/54691584/how-to-predict-multiple-labels-with-ml-net-using-regression-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!