-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Adding samples for data save and load from text and binary files #3745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
docs/samples/Microsoft.ML.Samples/Dynamic/DataOperations/SaveAndLoadFromBinary.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using Microsoft.ML; | ||
|
|
||
| namespace Samples.Dynamic | ||
| { | ||
| public static class SaveAndLoadFromBinary | ||
| { | ||
| public static void Example() | ||
| { | ||
| // Create a new context for ML.NET operations. It can be used for exception tracking and logging, | ||
| // as a catalog of available operations and as the source of randomness. | ||
| // Setting the seed to a fixed number in this example to make outputs deterministic. | ||
| var mlContext = new MLContext(seed: 0); | ||
|
|
||
| // Create a list of training data points. | ||
| IEnumerable<DataPoint> dataPoints = GenerateRandomDataPoints(10); | ||
|
|
||
| // Convert the list of data points to an IDataView object, which is consumable by ML.NET API. | ||
| IDataView data = mlContext.Data.LoadFromEnumerable(dataPoints); | ||
|
|
||
| // Inspect the data before saving to a binary file. | ||
| PrintPreviewRows(dataPoints); | ||
|
|
||
| // The rows in the data. | ||
| // 0, 0.7262433 | ||
| // 1, 0.8173254 | ||
| // 0, 0.7680227 | ||
| // 1, 0.5581612 | ||
| // 0, 0.2060332 | ||
| // 1, 0.5588848 | ||
| // 0, 0.9060271 | ||
| // 1, 0.4421779 | ||
| // 0, 0.9775497 | ||
| // 1, 0.2737045 | ||
|
|
||
| // Create a FileStream object and write the IDataView to it as a binary IDV file. | ||
| using (FileStream stream = new FileStream("data.idv", FileMode.Create)) | ||
| { | ||
| mlContext.Data.SaveAsBinary(data, stream); | ||
| } | ||
|
|
||
| // Create an IDataView object by loading the binary IDV file. | ||
| IDataView loadedData = mlContext.Data.LoadFromBinary("data.idv"); | ||
|
|
||
| // Inspect the data that is loaded from the previously saved binary file. | ||
| var loadedDataEnumerable = mlContext.Data.CreateEnumerable<DataPoint>(loadedData, reuseRowObject: false); | ||
| PrintPreviewRows(loadedDataEnumerable); | ||
|
|
||
| // The rows in the data. | ||
| // 0, 0.7262433 | ||
| // 1, 0.8173254 | ||
| // 0, 0.7680227 | ||
| // 1, 0.5581612 | ||
| // 0, 0.2060332 | ||
| // 1, 0.5588848 | ||
| // 0, 0.9060271 | ||
| // 1, 0.4421779 | ||
| // 0, 0.9775497 | ||
| // 1, 0.2737045 | ||
|
|
||
| File.Delete("data.idv"); | ||
| } | ||
|
|
||
| private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed = 0) | ||
| { | ||
| var random = new Random(seed); | ||
| for (int i = 0; i < count; i++) | ||
| { | ||
| yield return new DataPoint | ||
| { | ||
| Label = i % 2, | ||
|
|
||
| // Create random features that are correlated with label. | ||
| Features = (float)random.NextDouble() | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| // Example with label and feature values. A data set is a collection of such examples. | ||
| private class DataPoint | ||
| { | ||
| public float Label { get; set; } | ||
|
|
||
| public float Features { get; set; } | ||
| } | ||
|
|
||
| // Print helper. | ||
| private static void PrintPreviewRows(IEnumerable<DataPoint> data) | ||
| { | ||
| Console.WriteLine($"The rows in the data."); | ||
| foreach (var row in data) | ||
| Console.WriteLine($"{row.Label}, {row.Features}"); | ||
| } | ||
| } | ||
| } | ||
98 changes: 98 additions & 0 deletions
98
docs/samples/Microsoft.ML.Samples/Dynamic/DataOperations/SaveAndLoadFromText.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using Microsoft.ML; | ||
|
|
||
| namespace Samples.Dynamic | ||
| { | ||
| public static class SaveAndLoadFromText | ||
| { | ||
| public static void Example() | ||
| { | ||
| // Create a new context for ML.NET operations. It can be used for exception tracking and logging, | ||
| // as a catalog of available operations and as the source of randomness. | ||
| // Setting the seed to a fixed number in this example to make outputs deterministic. | ||
| var mlContext = new MLContext(seed: 0); | ||
|
|
||
| // Create a list of training data points. | ||
| IEnumerable<DataPoint> dataPoints = GenerateRandomDataPoints(10); | ||
|
najeeb-kazmi marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Convert the list of data points to an IDataView object, which is consumable by ML.NET API. | ||
| IDataView data = mlContext.Data.LoadFromEnumerable(dataPoints); | ||
|
|
||
| // Inspect the data before saving to a binary file. | ||
| PrintPreviewRows(dataPoints); | ||
|
najeeb-kazmi marked this conversation as resolved.
Outdated
|
||
|
|
||
| // The rows in the data. | ||
| // 0, 0.7262433 | ||
| // 1, 0.8173254 | ||
| // 0, 0.7680227 | ||
| // 1, 0.5581612 | ||
| // 0, 0.2060332 | ||
| // 1, 0.5588848 | ||
| // 0, 0.9060271 | ||
| // 1, 0.4421779 | ||
| // 0, 0.9775497 | ||
| // 1, 0.2737045 | ||
|
|
||
| // Create a FileStream object and write the IDataView to it as a binary IDV file. | ||
|
najeeb-kazmi marked this conversation as resolved.
Outdated
|
||
| using (FileStream stream = new FileStream("data.tsv", FileMode.Create)) | ||
| { | ||
|
najeeb-kazmi marked this conversation as resolved.
Outdated
|
||
| mlContext.Data.SaveAsText(data, stream); | ||
|
najeeb-kazmi marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // Create an IDataView object by loading the binary IDV file. | ||
|
najeeb-kazmi marked this conversation as resolved.
Outdated
|
||
| IDataView loadedData = mlContext.Data.LoadFromTextFile("data.tsv"); | ||
|
najeeb-kazmi marked this conversation as resolved.
|
||
|
|
||
| // Inspect the data that is loaded from the previously saved binary file. | ||
|
najeeb-kazmi marked this conversation as resolved.
Outdated
|
||
| var loadedDataEnumerable = mlContext.Data.CreateEnumerable<DataPoint>(loadedData, reuseRowObject: false); | ||
| PrintPreviewRows(loadedDataEnumerable); | ||
|
|
||
| // The rows in the data. | ||
| // 0, 0.7262433 | ||
| // 1, 0.8173254 | ||
| // 0, 0.7680227 | ||
| // 1, 0.5581612 | ||
| // 0, 0.2060332 | ||
| // 1, 0.5588848 | ||
| // 0, 0.9060271 | ||
| // 1, 0.4421779 | ||
| // 0, 0.9775497 | ||
| // 1, 0.2737045 | ||
|
|
||
| File.Delete("data.tsv"); | ||
| } | ||
|
|
||
| private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed = 0) | ||
| { | ||
| var random = new Random(seed); | ||
| for (int i = 0; i < count; i++) | ||
| { | ||
| yield return new DataPoint | ||
| { | ||
| Label = i % 2, | ||
|
|
||
| // Create random features that are correlated with label. | ||
| Features = (float)random.NextDouble() | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| // Example with label and feature values. A data set is a collection of such examples. | ||
| private class DataPoint | ||
| { | ||
| public float Label { get; set; } | ||
|
|
||
| public float Features { get; set; } | ||
| } | ||
|
|
||
| // Print helper. | ||
| private static void PrintPreviewRows(IEnumerable<DataPoint> data) | ||
| { | ||
| Console.WriteLine($"The rows in the data."); | ||
| foreach (var row in data) | ||
| Console.WriteLine($"{row.Label}, {row.Features}"); | ||
|
najeeb-kazmi marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.