我很难使用Microsoft .ML将每日销售数据作为输入来预测月度销售。
class Data { [Column(ordinal: "0", name: "Label")] public float PredictedProfit; [Column(ordinal: "Month")] public int Month; [Column(ordinal: "DayOfMonth")] public int DayOfMonth; [Column(ordinal: "Sales")] public double[] Sales; [Column(ordinal: "MonthlyProfit")] public double MonthlyProfit; } ........................... MLContext mlContext = new MLContext(seed: 0); List<VData> listData; VData row=new VData(); ..... fill row ..... listData.Add(row); var trainData = mlContext.CreateStreamingDataView<VData>(listData); var pipeline = mlContext.Transforms.CopyColumns("Label", "MonthlyProfit"); pipeline.Append(mlContext.Transforms.Concatenate("Features", "MonthlyProfit", "Sales", "Month", "DayOfMonth"); pipeline.Append(mlContext.Regression.Trainers.FastTree()); var model = pipeline.Fit(trainData); var dataView = mlContext.CreateStreamingDataView<VData>(listData); var predictions = model.Transform(dataView); var metrics = mlContext.Regression.Evaluate(predictions, "Label", "MonthlyProfit");
metrics的值总是零,并且没有预测数据
回答:
ML.NET中的管道是不可变的:对pipeline.Append
的调用会返回一个新的更新后的管道,但不会更改原始管道。
修改你的代码如下:
var pipeline = mlContext.Transforms.CopyColumns("Label", "MonthlyProfit"); pipeline = pipeline.Append(mlContext.Transforms.Concatenate("Features", "MonthlyProfit", "Sales", "Month", "DayOfMonth");pipeline = pipeline.Append(mlContext.Regression.Trainers.FastTree());
此外,你使用的[Column]
属性没有任何效果。为了更改标签列的名称,你可以使用[ColumnName("Label")]
。所有其他属性都是完全不必要的。