简体   繁体   English

ML.NET 如何从 linq 查询中获取 IDataView?

[英]ML.NET How can I get my IDataView from a linq query?

I want to get the data with a LINQ query that I want to use to create and train a model.我想通过 LINQ 查询获取数据,我想用它来创建和训练 model。

I need the data to be in float to work with the machine learning model, but in the database and my model to get it from the database it isn't all a float.我需要数据处于浮动状态才能与机器学习 model 一起使用,但在数据库和我的 model 中从数据库中获取它并不都是浮动的。

My code:我的代码:

        var tempquery = from m in context.Message
                        where m.Unit == 1
                        select new
                        {
                            MessageId = (float)m.MessageId,
                            DateTime = m.DateTime,
                            FillLevel = (float)m.FillLevel,
                            Temperature = (float)m.Temperature,
                            Latitude = (float)m.Latitude,
                            Longitude = (float)m.Longitude,
                            Unit = (float)m.Unit
                        };

The Message model class:消息 model class:

  public class Message
{
    public int MessageId { get; set; }
    public DateTime DateTime { get; set; }
    public double? FillLevel { get; set; }
    public int? Temperature { get; set; }
    public double? Latitude { get; set; }
    public double? Longitude { get; set; }
    public int Unit { get; set; }

}

For my Machine learning model I use:对于我的机器学习 model 我使用:

       public class MessageML
    {
        public int MessageId { get; set; }       
        public DateTime DateTime { get; set; }
        public float FillLevel { get; set; }
        public float Temperature { get; set; }
        public float Latitude { get; set; }
        public float Longitude { get; set; }
        public float Unit { get; set; }
    }

The problem is to convert the var tempQuery to a IDataView object.问题是将 var tempQuery 转换为 IDataView object。

I tried:我试过了:

IDataView data = (IDataView)tempQuery.ToList()

And

   MLContext mlContext = new MLContext();
        DatabaseLoader loader = mlContext.Data.CreateDatabaseLoader<MessageML>();

        IDataView data = loader.Load(tempquery);

When I use this it works (but it isn't what I want, because I want to use LINQ)当我使用它时它可以工作(但这不是我想要的,因为我想使用 LINQ)

  DatabaseLoader loader = mlContext.Data.CreateDatabaseLoader<MessageML>();
        string connectionString = @"ServerTEST;Database=TEST;Trusted_Connection=True";
        string sqlCommand = "SELECT CAST(MessageId as REAL) as MessageId, DateTime, CAST(FillLevel as REAL) as FillLevel, " +
            "CAST(Temperature as REAL) as Temperature, CAST(Latitude as REAL) as Latitude, CAST(Longitude as REAL) as Longitude, " +
            "CAST(Unit as REAL) as Unit from Message";


        DatabaseSource dbSource = new DatabaseSource(SqlClientFactory.Instance, connectionString, sqlCommand);
        IDataView trainingDataView = loader.Load(dbSource);

Use LoadFromEnumerable instead of CreateDatabaseLoader if you want to use LINQ before training如果要在训练前使用 LINQ,请使用LoadFromEnumerable而不是 CreateDatabaseLoader

IDataView traindata = mlContext.Data.LoadFromEnumerable(tempquery);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM