如何使用回归任务使用ML.NET预测多个标签? [英] How to predict multiple labels with ML.NET using regression task?

查看:310
本文介绍了如何使用回归任务使用ML.NET预测多个标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是机器学习的新手,我偶然发现了以下问题.考虑纽约市出租车的正式票价预测指南,假设我想预测另一个实际价值,例如TripTime.我修改了我的代码,如下所示:

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

第一个值(FareAmount)被正确地"预测(值不是零),但是第二个值(TripTime)为零.我的问题是,如何一次或至少使用同一模型来预测两个或多个标签?这有可能吗?我正在使用.NET Core 2.2和ML.NET 0.10.0来完成此任务.预先感谢您的任何帮助.

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.

推荐答案

可能不起作用,因为Fit()仅返回"Label"和"Score"

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

查看此处:此处

您从旅行时间"中获得的分数将被"FareAmount"覆盖.

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

这篇关于如何使用回归任务使用ML.NET预测多个标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆