简体   繁体   English

如何将带有数据的string [] []转换为IDataView-Object? (ML.NET)

[英]How to convert a string[][] with data into an IDataView-Object? (ML.NET)

I actually did all the Microsoft Tutorials for ML.NET and want to build my own models now. 我实际上完成了ML.NET的所有Microsoft教程,现在想构建自己的模型。 I want to convert string[][] data to an IDataView-Object , as I want to use it in an ML.NET-Model for binary classification. 我想将string[][] data转换为IDataView-Object ,因为我想在ML.NET-Model中使用它进行二进制分类。

So far I have always used data from external text or CSV files for the training. 到目前为止,我一直在使用来自外部文本或CSV文件的数据进行培训。 Now I want to use data stored in string[][] data . 现在,我要使用存储在string[][] data In data[0][] are the text values and in data[1][] are the boolean values. data[0][]中是文本值,在data[1][]中是boolean值。

I fail to convert the existing nested array into an IDataView object . 我无法将现有的嵌套数组转换为IDataView object I have already tried to use the following code: 我已经尝试使用以下代码:

 public class BinaryData
        {

            public string Text { get; set; }


            public bool Label { get; set; }
        }

// The data is collected from an Excel-Table with some functions and saved in this nested array: 

string[][] data = form.GetDataSelection().GetDataContainer().textCols;



BinaryData[] inMemoryCollection = new BinaryData[data[0].Length];
            for (int i = 0; i < data[0].Length-1; i++)
            {

                inMemoryCollection[i] = new BinaryData
                {
                    Text = data[0][i],
                    Label = Convert.ToBoolean(Convert.ToInt64(data[1][i]))
                };                             
            } 


IDataView dataView = mlContext.Data.LoadFromEnumerable<BinaryData>(inMemoryCollection);

My implementation is based on the tutorial from Microsoft . 我的实现基于Microsoft教程

It works until I want to use the Fit()-Method . 在我要使用Fit()-Method之前,它一直有效。 I get the following error-message: 我收到以下错误消息:

System.InvalidOperationException: 'Splitter/consolidator worker encountered exception while consuming source data' System.InvalidOperationException:'拆分器/合并器工作程序在使用源数据时遇到异常'

I hope somebody can help me out here. 我希望有人可以在这里帮助我。 Many thanks in advance! 提前谢谢了!

it works with an two-dimensional array [,] . 它适用于two-dimensional array [,] I used the Method from this post to convert the jagged array [][] into a two-dimensional array and changed my code a little bit: 我使用了这篇文章中Method来将jagged array [][]转换为二维数组,并稍微修改了我的代码:

string[][] data_jagged = form.GetDataSelection().GetDataContainer().textCols;
string[,] data = To2D(data_jagged);

BinaryData[] inMemoryCollection = new BinaryData[data_jagged[0].Length];
            for (int i = 0; i < data_jagged[0].Length; i++)
            {

                inMemoryCollection[i] = new BinaryData
                {
                    Text = data[0,i],
                    Label = Convert.ToBoolean(Convert.ToInt64(data[1,i]))
                };                             
            } 


IDataView dataView = mlContext.Data.LoadFromEnumerable<BinaryData>(inMemoryCollection);

Thanks to Eric for his help. 感谢Eric的帮助。

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

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