-
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
Changes from 8 commits
0cb5c91
5d0ad95
5bf777a
1e7cd06
bff9d9a
aa091bf
aa526aa
4e6c4af
e111f07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using Microsoft.ML; | ||
using Microsoft.ML.SamplesUtils; | ||
|
||
namespace Samples.Dynamic | ||
{ | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe 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) |
||
|
||
// 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; } | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.ML; | ||
|
||
namespace Samples.Dynamic | ||
{ | ||
public static class MapValueToKeyMultiColumn | ||
{ | ||
/// This example demonstrates the use of the ValueMappingEstimator by mapping strings to other string values, or floats to strings. | ||
/// This is useful to map types to a grouping. | ||
/// 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 DevelopmentTime DevelopmentTimeCategory | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
copy paste error #Resolved |
||
// 0-4yrs 1 6-11yrs 3 | ||
// 6-11yrs 4 6-11yrs 3 | ||
// 12-25yrs 3 25+yrs 2 | ||
// 0-5yrs 2 0-5yrs 1 | ||
|
||
// 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 DevelopmentTime DevelopmentTimeCategory"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Course #Resolved There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
CourseCategory #Resolved |
||
foreach (var featureRow in features) | ||
Console.WriteLine($"{featureRow.StudyTime}\t\t{featureRow.StudyTimeCategory}\t\t\t{featureRow.Course}\t\t{featureRow.CourseCategory}"); | ||
|
||
// StudyTime StudyTimeCategory DevelopmentTime DevelopmentTimeCategory | ||
// 0 - 4yrs 1 6 - 11yrs 2 | ||
// 6 - 11yrs 2 6 - 11yrs 2 | ||
// 12 - 25yrs 0 25 + yrs 3 | ||
// 0 - 5yrs 0 0 - 5yrs 0 | ||
} | ||
|
||
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; } | ||
} | ||
} | ||
} |
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.
why are we removing this? we're using the long namespace for trainers to prevent name conflicts #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 thought every class had its own distinctive name?
In reply to: 271977575 [](ancestors = 271977575)
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.
ic, we call them the same between tasks. Reverting.
In reply to: 272288927 [](ancestors = 272288927,271977575)