dotnet / machinelearning

ML.NET is an open source and cross-platform machine learning framework for .NET.
https://dot.net/ml
MIT License
8.93k stars 1.86k forks source link

Reading a CSV file without strongly typed structure #6853

Open viksing opened 9 months ago

viksing commented 9 months ago

How can I read a csv to data table, and then use it in something like

var dataView = mlContext.Data.LoadFromEnumerable(new List { dynamicDataObject })

Why Do I need to provide a concrete class for the type of the data?

I tired to use DynamicData like var dynamicDataObject = new DynamicData { DataTable = dynamicDataTable };

but it doesn't work. I do not know how to create SchemaDefinition.

static SchemaDefinition GenerateSchemaDefinition(Type dataType)
{
    var properties = dataType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

    var columns = properties.Select(p => new SchemaDefinition.Column
    {
        ColumnName = p.Name,
        ColumnType = p.PropertyType
    }).ToArray();

    return new SchemaDefinition(columns);
}

this says p.PropertyType is not of type DataViewType.

It is  just too complex.

Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

asmirnov82 commented 9 months ago

Hi @viksing, have you tried to use a dataframe for this?

IDataView dv = DataFrame.LoadCsv("filename.csv");

You may find more info about using DataFrame with ML .Net here: https://learn.microsoft.com/en-us/dotnet/machine-learning/how-to-guides/getting-started-dataframe