machinelearning icon indicating copy to clipboard operation
machinelearning copied to clipboard

Ignore a column on Training

Open RussellKirkwood opened this issue 7 years ago • 4 comments

System information

  • Win10
  • .net core 2.1

Can I tell Training to Ignore Columns in Training Data

I tried just leaving them out of Features but that does not appear to work. I thought by not including them in Features it would ignore, but prediction results are not good. If I totaly remove those fields from Data, then my predictions are really good.

I would like to ignore IncidentReportedByID, IncidentReportedMethod and ID.

public class MyData
    {
        [LoadColumn(0)]
        public float State;

        [LoadColumn(1)]
        public float City;

        [LoadColumn(2)]
        public float IncidentType;

        [LoadColumn(3)]
        public float IncidentReportedByID;

        [LoadColumn(4)]
        public float IncidentReportedMethod;
        
        [LoadColumn(5)]
        public float Label;
    }

my training looks like this

var dataProcessPipeline = mlContext.Transforms.Concatenate(DefaultColumnNames.Features, nameof(MyData.State),                                                                                   nameof(MyData.City),
                                                                                   nameof(MyData.IncidentType),                                                                                   
                                                                       .AppendCacheCheckpoint(mlContext);
           
            var trainer = mlContext.MulticlassClassification.Trainers.StochasticDualCoordinateAscent(labelColumnName: DefaultColumnNames.Label, featureColumnName: DefaultColumnNames.Features);
            var trainingPipeline = dataProcessPipeline.Append(trainer);

RussellKirkwood avatar Mar 13 '19 19:03 RussellKirkwood

So here is small experiment (I'm using master branch, so it has slightly different names, and I also need to use MapValueToKey transform to convert label into keys for multiclass)

var mlContext = new MLContext(seed: 1);
int n = 1000;
var data = new List<MyData>();
Random rnd = new Random(1);
for (int i = 0; i < n; i++)
{
    data.Add(new MyData()
    {
        City = rnd.Next(),
        IncidentReportedByID = rnd.Next(),
        IncidentReportedMethod = rnd.Next(),
        IncidentType = rnd.Next(),
        State = rnd.Next(),
        Label = rnd.Next(0, 4)
    });
}

var dataView = mlContext.Data.LoadFromEnumerable(data);
var dataProcessPipeline = mlContext.Transforms.Concatenate("Features", nameof(MyData.State), nameof(MyData.City),
                                                                       nameof(MyData.IncidentType))
                                                                       .Append(mlContext.Transforms.Conversion.MapValueToKey("Label"))
                                                           .AppendCacheCheckpoint(mlContext);

var trainer = mlContext.MulticlassClassification.Trainers.Sdca(new Trainers.SdcaMulticlassClassificationTrainer.Options() { NumberOfThreads = 1});
var trainingPipeline = dataProcessPipeline.Append(trainer);
var model = trainingPipeline.Fit(dataView);
var scored = model.Transform(dataView);
var metrics = mlContext.MulticlassClassification.Evaluate(scored);

I get following metrics: image

Let me change data generation code to following:

data.Add(new MyData()
{
     City = rnd.Next(),
     IncidentReportedByID = rnd.Next(),
     IncidentReportedMethod = rnd.Next(),
     IncidentType = rnd.Next(),
     State = rnd.Next(),
     Label = rnd.Next(0, 4)
 });
data[i].IncidentReportedByID = 0;
data[i].IncidentReportedMethod = 0;

see data[i].IncidentReportedByID = 0; data[i].IncidentReportedMethod = 0; I can put any values into this properties, float.PositiveInfinity, 0, whatever you want. If I run same code again I'm getting same metrics. image

Which makes me believe our code actually ignores them (I also debug trainer, and make sure we work only with 3 features in both cases, but that thing is harder to show up).

Ivanidzo4ka avatar Mar 13 '19 20:03 Ivanidzo4ka

Okay, so you confirmed via your recent test that column will be IGNORED if you don't specify them in "Features". I will move forward with my own test and report back to you

thank you very much

On Wed, Mar 13, 2019 at 4:42 PM Ivan Matantsev [email protected] wrote:

So here is small experiment (I'm using master branch, so it has slightly different names, and I also need to use MapValueToKey transform to convert label into keys for multiclass)

var mlContext = new MLContext(seed: 1); int n = 1000; var data = new List(); Random rnd = new Random(1); for (int i = 0; i < n; i++) { data.Add(new MyData() { City = rnd.Next(), IncidentReportedByID = rnd.Next(), IncidentReportedMethod = rnd.Next(), IncidentType = rnd.Next(), State = rnd.Next(), Label = rnd.Next(0, 4) }); }

var dataView = mlContext.Data.LoadFromEnumerable(data); var dataProcessPipeline = mlContext.Transforms.Concatenate("Features", nameof(MyData.State), nameof(MyData.City), nameof(MyData.IncidentType)) .Append(mlContext.Transforms.Conversion.MapValueToKey("Label")) .AppendCacheCheckpoint(mlContext);

var trainer = mlContext.MulticlassClassification.Trainers.Sdca(new Trainers.SdcaMulticlassClassificationTrainer.Options() { NumberOfThreads = 1}); var trainingPipeline = dataProcessPipeline.Append(trainer); var model = trainingPipeline.Fit(dataView); var scored = model.Transform(dataView); var metrics = mlContext.MulticlassClassification.Evaluate(scored);

I get following metrics: [image: image] https://user-images.githubusercontent.com/1523833/54312617-99d72180-4594-11e9-96ce-6e2fba948324.png

Let me change data generation code to following:

data.Add(new MyData() { City = rnd.Next(), IncidentReportedByID = rnd.Next(), IncidentReportedMethod = rnd.Next(), IncidentType = rnd.Next(), State = rnd.Next(), Label = rnd.Next(0, 4) }); data[i].IncidentReportedByID = 0; data[i].IncidentReportedMethod = 0;

see

data[i].IncidentReportedByID = 0; data[i].IncidentReportedMethod = 0; I can put any values into this properties, float.PositiveInfinity, 0, whatever you want. If I run same code again I'm getting same metrics. [image: image] https://user-images.githubusercontent.com/1523833/54312617-99d72180-4594-11e9-96ce-6e2fba948324.png

Which makes me believe our code actually ignores them (I also debug trainer, and make sure we work only with 3 features in both cases, but that thing is harder to show up).

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/dotnet/machinelearning/issues/2945#issuecomment-472597187, or mute the thread https://github.com/notifications/unsubscribe-auth/AEzpkA9jlZROqoK5E1OV-ng5zXpN2VSUks5vWWK8gaJpZM4bwADn .

RussellKirkwood avatar Mar 14 '19 14:03 RussellKirkwood

@RussellKirkwood I am closing this issue. If this is not resolved, please re-open.

singlis avatar Mar 18 '19 19:03 singlis

Thanks , yes looked like it does ignore columns

RussellKirkwood avatar Mar 18 '19 19:03 RussellKirkwood