0

I have some data in a csv file:

Survived    Pclass  Sex Age
0           3   male    22
1           1   female  38
1           3   male    26
1           1   female  35
...

I loaded the data using:

context.Data.LoadFromTextFile(path: dataPath,...);

Once I loaded the data, I need to add calculated column say, AgeName, so that the:

if (Age < 18)
    AgeName ="Child"
else if(Age < 55)
    AgeNAme = "Man"
else
    AgeNAme = "Grandpa"

Is there builtin method in the ML.NET in order to add the column, or do I need to implement it manually?

bhrnjica
  • 15
  • 3

1 Answers1

0

I think you would want to use the CustomMapping transform.

Below is a sample. First, some input and output classes:

class InputData
{
    public int Age { get; set; }
}

class CustomMappingOutput
{
    public string AgeName { get; set; }
}

class TransformedData
{
    public int Age { get; set; }

    public string AgeName { get; set; }
}

Then, in the ML.NET program:

MLContext mlContext = new MLContext();

var samples = new List<InputData>
{
    new InputData { Age = 16 },
    new InputData { Age = 35 },
    new InputData { Age = 60 },
    new InputData { Age = 28 },
};

var data = mlContext.Data.LoadFromEnumerable(samples);

Action<InputData, CustomMappingOutput> mapping =
    (input, output) =>
    {
        if (input.Age < 18)
        {
            output.AgeName = "Child";
        }
        else if (input.Age < 55)
        {
            output.AgeName = "Man";
        }
        else
        {
            output.AgeName = "Grandpa";
        }
    };

var pipeline = mlContext.Transforms.CustomMapping(mapping, contractName: null);

var transformer = pipeline.Fit(data);
var transformedData = transformer.Transform(data);

var dataEnumerable = mlContext.Data.CreateEnumerable<TransformedData>(transformedData, reuseRowObject: true);

foreach (var row in dataEnumerable)
{
    Console.WriteLine($"{row.Age}\t {row.AgeName}");
}
Jon
  • 2,644
  • 1
  • 22
  • 31
  • Thanx for the answer. It would be natural that I can use something like this: ```data.AgeName=data.Age<5?"child":....``` I found the data processing pipeline in ml.net too complicated for very simple operations. – bhrnjica Jun 15 '19 at 18:58