-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Multi column MapKeyToValue and MapValueToKey #3187
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 all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0cb5c91
Multi column MapKeyToValue and MapValueToKey
sfilipi 5d0ad95
renaming per the new convention
sfilipi 5bf777a
fixing mismatch
sfilipi 1e7cd06
fix reference from xml
sfilipi bff9d9a
reverting name
sfilipi aa091bf
addressing PR issue, and merge.
sfilipi aa526aa
PR comments
sfilipi 4e6c4af
changing namespace and removing the train/test split
sfilipi e111f07
PR review comments
sfilipi 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
3 changes: 1 addition & 2 deletions
3
...ft.ML.Samples/Dynamic/Trainers/MulticlassClassification/StochasticDualCoordinateAscent.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
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
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
3 changes: 2 additions & 1 deletion
3
docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Conversion/Hash.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
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
80 changes: 80 additions & 0 deletions
80
docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Conversion/MapKeyToValueMultiColumn.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,80 @@ | ||
using System; | ||
using Microsoft.ML; | ||
using Microsoft.ML.SamplesUtils; | ||
|
||
namespace Samples.Dynamic | ||
{ | ||
/// This example demonstrates the use of the ValueToKeyMappingEstimator, by mapping KeyType values to the original strings. | ||
/// For more on ML.NET KeyTypes see: https://github.com/dotnet/machinelearning/blob/master/docs/code/IDataViewTypeSystem.md#key-types | ||
/// It is possible to have multiple values map to the same category. | ||
|
||
public class MapKeyToValueMultiColumn | ||
{ | ||
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 data examples. | ||
var examples = DatasetUtils.GenerateRandomMulticlassClassificationExamples(1000); | ||
|
||
// Convert the examples list to an IDataView object, which is consumable by ML.NET API. | ||
var dataView = mlContext.Data.LoadFromEnumerable(examples); | ||
|
||
//////////////////// Data Preview //////////////////// | ||
// Label Features | ||
// AA 0.7262433,0.8173254,0.7680227,0.5581612,0.2060332,0.5588848,0.9060271,0.4421779,0.9775497,0.2737045 | ||
// BB 0.4919063,0.6673147,0.8326591,0.6695119,1.182151,0.230367,1.06237,1.195347,0.8771811,0.5145918 | ||
// CC 1.216908,1.248052,1.391902,0.4326252,1.099942,0.9262842,1.334019,1.08762,0.9468155,0.4811099 | ||
// DD 0.7871246,1.053327,0.8971719,1.588544,1.242697,1.362964,0.6303943,0.9810045,0.9431419,1.557455 | ||
|
||
// Create a pipeline. | ||
var pipeline = | ||
// Convert the string labels into key types. | ||
mlContext.Transforms.Conversion.MapValueToKey("Label") | ||
// Apply StochasticDualCoordinateAscent multiclass trainer. | ||
.Append(mlContext.MulticlassClassification.Trainers.SdcaMaximumEntropy()); | ||
|
||
// Train the model and do predictions on same data set. | ||
// Typically predictions would be in a different, validation set. | ||
var dataWithPredictions = pipeline.Fit(dataView).Transform(dataView); | ||
|
||
// at this point, the Label colum is tranformed from strings, to DataViewKeyType and | ||
// the transformation has added the PredictedLabel column, with | ||
var newPipeline = mlContext.Transforms.Conversion.MapKeyToValue(new[] | ||
{ | ||
new InputOutputColumnPair("LabelOriginalValue","Label"), | ||
new InputOutputColumnPair("PredictedLabelOriginalValue","PredictedLabel") | ||
}); | ||
|
||
var transformedData = newPipeline.Fit(dataWithPredictions).Transform(dataWithPredictions); | ||
|
||
var values = mlContext.Data.CreateEnumerable<TransformedData>(transformedData, reuseRowObject: false); | ||
|
||
// Printing the columns of the transformed data. | ||
Console.WriteLine($" Label LabelOriginalValue PredictedLabel PredictedLabelOriginalValue"); | ||
foreach (var row in values) | ||
Console.WriteLine($"{row.Label}\t\t{row.LabelOriginalValue}\t\t\t{row.PredictedLabel}\t\t\t{row.PredictedLabelOriginalValue}"); | ||
|
||
// Label LabelOriginalValue PredictedLabel PredictedLabelOriginalValue | ||
// 1 AA 2 BB | ||
// 1 AA 1 AA | ||
// 4 DD 4 DD | ||
// 2 BB 2 BB | ||
// 1 AA 1 AA | ||
// 1 AA 1 AA | ||
// 1 AA 1 AA | ||
// 2 BB 2 BB | ||
|
||
} | ||
private class TransformedData | ||
{ | ||
public uint Label { get; set; } | ||
public uint PredictedLabel { get; set; } | ||
public string LabelOriginalValue { get; set; } | ||
public string PredictedLabelOriginalValue { get; set; } | ||
} | ||
} | ||
} |
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
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
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
116 changes: 116 additions & 0 deletions
116
docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Conversion/MapValueToKeyMultiColumn.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,116 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.ML; | ||
|
||
namespace Samples.Dynamic | ||
{ | ||
public static class MapValueToKeyMultiColumn | ||
{ | ||
/// This example demonstrates the use of the ValueToKeyMappingEstimator, by mapping strings to KeyType values. | ||
/// For more on ML.NET KeyTypes see: https://github.com/dotnet/machinelearning/blob/master/docs/code/IDataViewTypeSystem.md#key-types | ||
/// It is possible to have multiple values map to the same category. | ||
public static void Example() | ||
{ | ||
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging, | ||
// as well as the source of randomness. | ||
var mlContext = new MLContext(); | ||
|
||
// Get a small dataset as an IEnumerable. | ||
var rawData = new[] { | ||
new DataPoint() { StudyTime = "0-4yrs" , Course = "CS" }, | ||
new DataPoint() { StudyTime = "6-11yrs" , Course = "CS" }, | ||
new DataPoint() { StudyTime = "12-25yrs" , Course = "LA" }, | ||
new DataPoint() { StudyTime = "0-5yrs" , Course = "DS" } | ||
}; | ||
|
||
var data = mlContext.Data.LoadFromEnumerable(rawData); | ||
|
||
// Constructs the ML.net pipeline | ||
var pipeline = mlContext.Transforms.Conversion.MapValueToKey(new[] { | ||
new InputOutputColumnPair("StudyTimeCategory", "StudyTime"), | ||
new InputOutputColumnPair("CourseCategory", "Course") | ||
}, | ||
keyOrdinality: Microsoft.ML.Transforms.ValueToKeyMappingEstimator.KeyOrdinality.ByValue, | ||
addKeyValueAnnotationsAsText: true); | ||
|
||
// Fits the pipeline to the data. | ||
IDataView transformedData = pipeline.Fit(data).Transform(data); | ||
|
||
// Getting the resulting data as an IEnumerable. | ||
// This will contain the newly created columns. | ||
IEnumerable<TransformedData> features = mlContext.Data.CreateEnumerable<TransformedData>(transformedData, reuseRowObject: false); | ||
|
||
Console.WriteLine($" StudyTime StudyTimeCategory Course CourseCategory"); | ||
foreach (var featureRow in features) | ||
Console.WriteLine($"{featureRow.StudyTime}\t\t{featureRow.StudyTimeCategory}\t\t\t{featureRow.Course}\t\t{featureRow.CourseCategory}"); | ||
|
||
// TransformedData obtained post-transformation. | ||
// | ||
// StudyTime StudyTimeCategory Course CourseCategory | ||
// 0-4yrs 1 CS 1 | ||
// 6-11yrs 4 CS 1 | ||
// 12-25yrs 3 LA 3 | ||
// 0-5yrs 2 DS 2 | ||
|
||
// If we wanted to provide the mapping, rather than letting the transform create it, | ||
// we could do so by creating an IDataView one column containing the values to map to. | ||
// If the values in the dataset are not found in the lookup IDataView they will get mapped to the mising value, 0. | ||
// The keyData are shared among the columns, therefore the keys are not contiguous for the column. | ||
// Create the lookup map data IEnumerable. | ||
var lookupData = new[] { | ||
new LookupMap { Key = "0-4yrs" }, | ||
new LookupMap { Key = "6-11yrs" }, | ||
new LookupMap { Key = "25+yrs" }, | ||
new LookupMap { Key = "CS" }, | ||
new LookupMap { Key = "DS" }, | ||
new LookupMap { Key = "LA" } | ||
}; | ||
|
||
// Convert to IDataView | ||
var lookupIdvMap = mlContext.Data.LoadFromEnumerable(lookupData); | ||
|
||
// Constructs the ML.net pipeline | ||
var pipelineWithLookupMap = mlContext.Transforms.Conversion.MapValueToKey(new[] { | ||
new InputOutputColumnPair("StudyTimeCategory", "StudyTime"), | ||
new InputOutputColumnPair("CourseCategory", "Course") | ||
}, | ||
keyData: lookupIdvMap); | ||
|
||
// Fits the pipeline to the data. | ||
transformedData = pipelineWithLookupMap.Fit(data).Transform(data); | ||
|
||
// Getting the resulting data as an IEnumerable. | ||
// This will contain the newly created columns. | ||
features = mlContext.Data.CreateEnumerable<TransformedData>(transformedData, reuseRowObject: false); | ||
|
||
Console.WriteLine($" StudyTime StudyTimeCategory Course CourseCategory"); | ||
foreach (var featureRow in features) | ||
Console.WriteLine($"{featureRow.StudyTime}\t\t{featureRow.StudyTimeCategory}\t\t\t{featureRow.Course}\t\t{featureRow.CourseCategory}"); | ||
|
||
// StudyTime StudyTimeCategory Course CourseCategory | ||
// 0 - 4yrs 1 CS 4 | ||
// 6 - 11yrs 2 CS 4 | ||
// 12 - 25yrs 0 LA 6 | ||
// 0 - 5yrs 0 DS 5 | ||
|
||
} | ||
|
||
private class DataPoint | ||
{ | ||
public string StudyTime { get; set; } | ||
public string Course { get; set; } | ||
} | ||
|
||
private class TransformedData : DataPoint | ||
{ | ||
public uint StudyTimeCategory { get; set; } | ||
public uint CourseCategory { get; set; } | ||
} | ||
|
||
// Type for the IDataView that will be serving as the map | ||
private class LookupMap | ||
{ | ||
public string Key { get; set; } | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we have inline data like the other sample? #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like the most common use case for this transform would be this one: after multiclass/binary get back the original values, therefore used it in this context.
In reply to: 271977941 [](ancestors = 271977941)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gani's PR is not checked in. I can switch the Generate after he checks in.
In reply to: 272332287 [](ancestors = 272332287,271977941)