【问题标题】:ML.NET thows an error Column 'Time' has values of DateTime, which is not the same as earlier observed type of SingleML.NET 抛出错误列“时间”具有 DateTime 值,这与之前观察到的 Single 类型不同
【发布时间】:2020-07-06 20:47:50
【问题描述】:

我试图使用 ML.NET 使用小型数据集(760 个项目)进行一些简单的线性回归测试,但是当我调用 Fit() 来获取变压器时,它会抛出错误“列‘时间’的值为DateTime,与之前观察到的 Single 类型不同。"

好像函数混淆了csv中每一列的类型?

代码


        public class PriceData
        {
            [ColumnName("Time")]
            [LoadColumn(0)]
            public DateTime Time;

            [ColumnName("ClosePrice")]
            [LoadColumn(1)]
            public float ClosePrice;

        }
       
       


        public class DemandPrediction
        {
            [ColumnName("Score")]
            public float ApproximationScore;
        }



        static string URL = "https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=USD-BTC&tickInterval=day";
        public static void Main(string[] args)
        {
            try
            {
                List<PriceData> priceData = new List<PriceData>();
                DownloadData(URL,ref priceData);

                var context = new MLContext();
                var data = context.Data.LoadFromTextFile<PriceData>("data.csv",hasHeader:false,separatorChar:',');

                var pipeline = context.Transforms.Concatenate(outputColumnName:"PriceData",
                     nameof(PriceData.Time),
                     nameof(PriceData.ClosePrice)
                   ).AppendCacheCheckpoint(context);

               
                var trainerType = context.Regression.Trainers.OnlineGradientDescent(lossFunction: new TweedieLoss());
                var fullPipeline = pipeline.Append(trainerType);
                var model = pipeline.Fit(data);
                var predictions = model.Transform(data);
                var metrics = context.Regression.Evaluate(
                                                            data: predictions,
                                                            labelColumnName: "ClosePrice",
                                                            scoreColumnName: "Score");

                
                var sample = new PriceData { Time =Convert.ToDateTime("7/6/2020 12:00:00 AM"), ClosePrice = 9273.18f };

                // create a prediction engine
                var engine = context.Model.CreatePredictionEngine<PriceData, DemandPrediction>(model);

                // make the prediction
                var prediction = engine.Predict(sample);
                

                Console.ReadKey();

            }
            catch (WebException e)
            {
                Console.WriteLine(e.Message);
            }
        }```

【问题讨论】:

标签: c# .net .net-core ml.net


【解决方案1】:

如果您在记事本中打开 csv,Time 的值是什么样的?它们是整数值吗?您可能需要将整数值转换为DateTime。见:How do I convert an Excel serial date number to a .NET DateTime?

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-02-24
  • 1970-01-01
  • 2018-05-04
  • 1970-01-01
  • 2017-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多