dotnet / machinelearning

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

Allow developers to supply their own function to infer column data types from data while loading CSVs #7141

Closed sevenzees closed 2 months ago

sevenzees commented 5 months ago

Is your feature request related to a problem? Please describe. Currently when you use LoadCsv or LoadCsvFromString without supplying data types for each column, the code will try to guess the data types based on the data in the CSV file. This is good, but the problem is that the default type inference code only considers bool, float, DateTime, and string for column types. Sometimes the user may need another data type, such as int, long, or double (see issue 6347 for an example where someone had a problem with the float data type that was chosen by default) but not know the structure of the data ahead of time.

Describe the solution you'd like I would like to be able to pass in my own custom type inference logic to override the default GuessKind implementation that is given in the library right now. If no custom guess type function is provided to the LoadCsv or LoadCsvFromString methods, then the code should work the same as it does today.

Describe alternatives you've considered The alternative is to call LoadCsv/LoadCsvFromString with dataTypes set to an array filled with typeof(string) for each column in your data, and then run your logic on the DataFrame with all string type columns to convert the columns to the data types that make sense for each column based on the data that is in each column.

Additional context I already have implemented a fix for this issue that I would like to merge in with a pull request.