Skip to content

Better docs/samples for Data.SaveAsText & Data.SaveAsBinary #3661

Closed

Description

@sergey-tihon

I do classification of MS Office document, in my case feature generation is relatively slow process, that why I want to do in once and save my data set to file like data.tsv and then experiment with different models and parameters.

Let's say we I defined model class

private class IrisInputAllFeatures
{
    [ColumnName("Label"), LoadColumn(4)]
    public string IgnoredLabel { get; set; }

    [LoadColumn(0, 3)]
    public float Features { get; set; }
}

then I wrote the code that generate / export data

var dataSet = new [] {
   new IrisInputAllFeatures() { ... },
   new IrisInputAllFeatures() { ... } 
}

the next thing that I expect is to be able to save all my data in file in format supported by LoadFromTextFile method (to be able reuse IrisInputAllFeatures class in both places), like

Data.SaveToTextFile("DataSet.tsv", dataSet);

Is there a way to save data to the file based on ColumnName, LoadColumn attributes?

Activity

  1. sfilipi commented on May 6, 2019

    @sfilipi
    Member

    I don't think ML.NET has an api to save the data.
    The best way to go about it is to use C# csv libraries to save the data in csv format.
    We can probably add such API. Adding the enhancement label.

  2. najeeb-kazmi commented on May 6, 2019

    @najeeb-kazmi
    Member

    @sfilipi Don't we have MLContext.Data.SaveAsText extension in the TextLoaderSaverCatalog?

    public static void SaveAsText(this DataOperationsCatalog catalog,

  3. sergey-tihon commented on May 7, 2019

    @sergey-tihon
    Author

    @najeeb-kazmi Thank you, it was not easy to find at all.

    Looks like we can save & load IDataView to text or binary format

    var mlContext = new MLContext();
    
    IEnumerable<IrisInputAllFeatures> myData = GenerateData();
    IDataView dataView = mlContext.Data.LoadFromEnumerable(myData);
     
    // Save & Load from text file
    using (var stream = File.Create(textFileName))
        mlContext.Data.SaveAsText(data, stream);
    // Note: Here is important to use non-generic version of LoadFromTextFile
    IDataView dataViewFromTextFile = mlContext.Data.LoadFromTextFile(textFileName);
    
    // Save & Load from binary file
    using (var stream = File.Create(binFileName))
        mlContext.Data.SaveAsBinary(data, stream);
    IDataView dataViewFromBinFile = mlContext.Data.LoadFromBinary(binFileName);

    @sfilipi Can we mark this issue as documentation improvement request?

    Thanks @sfilipi & @najeeb-kazmi

  4. changed the title [-]Data.SaveToTextFile[/-] [+]Better docs/samples for Data.SaveAsText & Data.SaveAsBinary[/+] on May 7, 2019
  5. jwood803 commented on May 7, 2019

    @jwood803
    Contributor

    Would #2811 help with the SaveAsBinary part?

  6. added
    documentationRelated to documentation of ML.NET
    and removed
    enhancementNew feature or request
    on May 8, 2019
  7. ghost locked as resolved and limited conversation to collaborators on Mar 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

documentationRelated to documentation of ML.NET

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions