<jupyter_start><jupyter_text>This example comes from the ML.NET documentation: https://docs.microsoft.com/en-us/dotnet/api/microsoft.ml.conversionsextensionscatalog.mapkeytovalue?view=ml-dotnet<jupyter_code>class DataPoint
{
    public string Label { get; set; }
    [VectorType(10)]
    public float[] Features { get; set; }
}

class TransformedData
{
    public uint Label { get; set; }
    public uint PredictedLabel { get; set; }
    public string LabelOriginalValue { get; set; }
    public string PredictedLabelOriginalValue { get; set; }
}
static List<DataPoint> GenerateRandomDataPoints(int count, int featureVectorLenght)
{
    var examples = new List<DataPoint>();
    var rnd = new Random(0);
    for (int i = 0; i < count; ++i)
    {
        var example = new DataPoint();
        example.Features = new float[featureVectorLenght];
        var res = i % 4;
        // Generate random float feature values.
        for (int j = 0; j < featureVectorLenght; ++j)
        {
            var value = (float)rnd.NextDouble() + res * 0.2f;
            example.Features[j] = value;
        }

        // Generate label based on feature sum.
        if (res == 0)
            example.Label = "AA";
        else if (res == 1)
            example.Label = "BB";
        else if (res == 2)
            example.Label = "CC";
        else
            example.Label = "DD";
        examples.Add(example);
    }
    return examples;
}
var mlContext = new MLContext(seed: 0);
var examples = GenerateRandomDataPoints(1000, 10);
var dataView = mlContext.Data.LoadFromEnumerable(examples);
var pipeline = mlContext
    // Convert the string labels into key types.
    .Transforms.Conversion.MapValueToKey("Label")
    // Apply StochasticDualCoordinateAscent multiclass trainer.
    .Append(mlContext.MulticlassClassification.Trainers.SdcaMaximumEntropy());
var dataWithPredictions = pipeline.Fit(dataView).Transform(dataView);<jupyter_output><empty_output><jupyter_text>At this point, the Label column is transformed from strings, to DataViewKeyType and the transformation has added the PredictedLabel column, with same DataViewKeyType as transformed Label column. MapKeyToValue would take columns with DataViewKeyType and convert them back to their original values<jupyter_code>var newPipeline = mlContext.Transforms.Conversion.MapKeyToValue(new[]
{
    new InputOutputColumnPair("LabelOriginalValue","Label"),
    new InputOutputColumnPair("PredictedLabelOriginalValue", "PredictedLabel")

});
var transformedData = newPipeline.Fit(dataWithPredictions).Transform(dataWithPredictions);
// Let's iterate over first 5 items.
transformedData = mlContext.Data.TakeRows(transformedData, 5);
mlContext.Data.CreateEnumerable<TransformedData>(transformedData, reuseRowObject: false)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10053667262969589, 'fraction_numerical': 0.007155635062611807, 'mean_word_length': 3.7632027257240206, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 12, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '648639', 'license': 'MIT', 'n_tokens_mistral': 825, 'n_tokens_neox': 791, 'n_words': 215, 'path': 'MapKeyToValue.ipynb', 'repo_name': 'rvernagus/simple-ml.net-examples', 'size': '2795'}