首页 > 解决方案 > 如何使用 ML.NET 预测多列

问题描述

我正在尝试创建一个应用程序,根据用户的生活方式和药物的限制来预测服药时间。
我是说:

我从一位患者那里获取以下信息:
• 他/她吃饭的次数和时间
• 他/她什么时候醒来和入睡
• 他/她必须服用多少药片


从药物的限制:
• 是否应该空腹
吃药 • 是否应该在用餐或不用餐时吃药
• 患者是否需要在进餐和服药之间休息(它还没有显示在下面的屏幕上)
• 等

样本数据集:
https ://ibb.co/Gvry945

我应该使用什么类型的模型/机制/算法来预测服药时间?回归是正确的吗?我需要预测 1,2,3,4 有时 5 列。

我基于以下内容编写了一个简单的代码:
https
://docs.microsoft.com/pl-pl/dotnet/machine-learning/tutorials/predict-prices 如何使用回归任务使用 ML.NET 预测多个标签?

它工作正常,我可以预测超过 1 列。但是,我的问题仍然是空白单元格。当我试图从该数据中预测某些内容时,它总是显示错误的值,并且只有在所有单元格都完成时才能正常工作。

那么,我应该将我的数据集分散到更少的数据集(所有单元格都是完整的)吗?例如:
https
: //ibb.co/m8HVPvb 当我只预测 TimeToTakeMedicine1


https://ibb.co/qNk9xQL
当我预测 TimeToTakeMedicine1 和 TimeToTakeMedicine2


https://ibb.co/GnRc1c0
当我预测 TimeToTakeMedicine1、TimeToTakeMedicine2、TimeToTakeMedicine3 等时。

有没有更简单更好的方法来解决这个问题?

预测 TimeToTakeMedicine1、TimeToTakeMedicine2、TimeToTakeMedicine3 的工作代码(为了简单起见,我去掉了 OnEmptyStomach、WithMeal 和 IsPossible)

using System;
using System.IO;
using Microsoft.ML;
using Microsoft.ML.Trainers;

namespace NextTry
{
    class Program
    {
        static readonly string _trainDataPath = Path.Combine(Environment.CurrentDirectory, "DataFolder", "DataForPredictT1T2T3.csv");


        static void Main(string[] args)
        {

            MLContext mlContext = new MLContext(seed: 0);
            var model = Train(mlContext, _trainDataPath);

            TestSinglePrediction(mlContext, model);


        }

        public static ITransformer Train(MLContext mlContext, string dataPath)
        {
            IDataView dataView = mlContext.Data.LoadFromTextFile<Medicine>(dataPath, hasHeader: true, separatorChar: ',');

            var pipelineForMeal1 = mlContext.Transforms.CopyColumns(outputColumnName: "Label", inputColumnName: "TimeToTakeMedicine1")      
                .Append(mlContext.Transforms.Concatenate("Features", "MealTime1", "MealTime2", "MealTime3", "MealCount", "ActivityHoursWakeUp",  "ActivityHoursSleep", "PillsCount"))
                .Append(mlContext.Regression.Trainers.FastTree())
                .Append(mlContext.Transforms.CopyColumns(outputColumnName: "timeToTakeMedicine1", inputColumnName: "Score"));


            var pipelineForMeal2 = mlContext.Transforms.CopyColumns(outputColumnName: "Label", inputColumnName: "TimeToTakeMedicine2")
                .Append(mlContext.Transforms.Concatenate("Features", "MealTime1", "MealTime2", "MealTime3", "MealCount", "ActivityHoursWakeUp", "ActivityHoursSleep", "PillsCount"))
                .Append(mlContext.Regression.Trainers.FastTree())
                .Append(mlContext.Transforms.CopyColumns(outputColumnName: "timeToTakeMedicine2", inputColumnName: "Score"));


            var pipelineForMeal3 = mlContext.Transforms.CopyColumns(outputColumnName: "Label", inputColumnName: "TimeToTakeMedicine3")
                .Append(mlContext.Transforms.Concatenate("Features", "MealTime1", "MealTime2", "MealTime3", "MealCount", "ActivityHoursWakeUp", "ActivityHoursSleep",  "PillsCount"))
                .Append(mlContext.Regression.Trainers.FastTree())
                .Append(mlContext.Transforms.CopyColumns(outputColumnName: "timeToTakeMedicine3", inputColumnName: "Score"));


            var model = pipelineForMeal1
                .Append(pipelineForMeal2)
                .Append(pipelineForMeal3)
                .Fit(dataView);
            return model;
        }


        private static void TestSinglePrediction(MLContext mlContext, ITransformer model)
        {
            var predictionFunction = mlContext.Model.CreatePredictionEngine<Medicine, MedicineTimeTakeMedicinePrediction>(model);
            var medicineSample = new Medicine()
            {
                MealTime1 = 6,
                MealTime2 = 12,
                MealTime3 = 22,     
                MealCount = 3,
                PillsCount = 3
            };
            var prediction = predictionFunction.Predict(medicineSample);


            Console.WriteLine($"Predicted TimeToTakePill: {prediction.TimeToTakeMedicine1:0.####} ");
            Console.WriteLine($"Predicted TimeToTakePill: {prediction.TimeToTakeMedicine2:0.####}");
            Console.WriteLine($"Predicted TimeToTakePill: {prediction.TimeToTakeMedicine3:0.####}");


            Console.ReadKey();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ML.Data;

namespace NextTry
{
    public class Medicine

    {
        [LoadColumn(0)]
        public float MealTime1 { get; set; }

        [LoadColumn(1)]
        public float MealTime2 { get; set; }

        [LoadColumn(2)]
        public float MealTime3 { get; set; }

        [LoadColumn(3)]
        public float MealCount { get; set; }

        [LoadColumn(4)]
        public float ActivityHoursWakeUp { get; set; }

        [LoadColumn(5)]
        public float ActivityHoursSleep { get; set; }

        [LoadColumn(6)]
        public float PillsCount { get; set; }

        [LoadColumn(7)]
        public float TimeToTakeMedicine1 { get; set; }

        [LoadColumn(8)]
        public float TimeToTakeMedicine2 { get; set; }

        [LoadColumn(9)]
        public float TimeToTakeMedicine3 { get; set; }




    }
    public class MedicineTimeTakeMedicinePrediction

    {
        [ColumnName("timeToTakeMedicine1")]
        public float TimeToTakeMedicine1 { get; set; }

        [ColumnName("timeToTakeMedicine2")]
        public float TimeToTakeMedicine2 { get; set; }

        [ColumnName("timeToTakeMedicine3")]
        public float TimeToTakeMedicine3 { get; set; }


    }
}

标签: c#machine-learningml.net

解决方案


我遇到了同样的问题。您要做的一件事是立即将所有模型附加到一个管道中,因为您具有相同的功能。


推荐阅读