gpt4 book ai didi

c# - 如何使用 ML.NET 预测多列

转载 作者:行者123 更新时间:2023-12-03 22:30:24 28 4
gpt4 key购买 nike

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

我从一个病人那里得到如下信息:
• 他/她吃饭的次数和时间
• 他/她什么时候醒来并休眠
• 他/她必须服用多少药片

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

示例数据集:
https://ibb.co/Gvry945

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

我写了一个简单的代码基于:
https://docs.microsoft.com/pl-pl/dotnet/machine-learning/tutorials/predict-prices
How to predict multiple labels with ML.NET using regression task?

它工作正常,我可以预测超过 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# - 如何使用 ML.NET 预测多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58222711/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com