-1

While using ML.net package the error "System.ArgumentOutOfRangeException: 'Schema mismatch for feature column 'Features': expected Vector, got Vector Parameter name: inputSchema'" was thrown on the line "var model = trainingPipeline.Fit(trainData);". While understanding the error I cannot understand why it was thrown for the code

 class Program
    {

        static void Main(string[] args)
        {



            MLContext mlcontext = new MLContext();
            IDataView trainData = mlcontext.Data.LoadFromTextFile<ModelInput>("StockTrain.csv", separatorChar: ',', hasHeader: false);           
            var dataProcessPipeline = mlcontext.Transforms.Concatenate(outputColumnName: "Features", "age", "blPressure", "BiSkinthck", "NoPreg");





            var trainer = mlcontext.Regression.Trainers.LightGbm(labelColumnName: "bmi", featureColumnName: "Features");
            var trainingPipeline = dataProcessPipeline.Append(trainer);
            var model = trainingPipeline.Fit(trainData);

            IDataView testData = mlcontext.Data.LoadFromTextFile<ModelInput>("StockTest.csv", separatorChar: ',', hasHeader: false);
            IDataView predictions = model.Transform(testData);
            var metrics = mlcontext.Regression.Evaluate(predictions, "bmi");




                                                 //(0)     (2)  (3)       (5)         (7)
            var input = new ModelInput           //<4>,144,<58>,<28>,140,<29.5>,0.287,<37>,0
            {
                NoPreg = 4,
                blPressure = 58,
                BiSkinthck = 28,
                age = 37
            };
            var result = mlcontext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(model).Predict(input);
            Console.WriteLine($"Predicted bmi " + $"{result.bmi}");
        }

        public class ModelOutput
        {
            [ColumnName("Score")]
            public int bmi;
        }

        public class ModelInput
        {
            [LoadColumn(0)]
            public int NoPreg;
            [LoadColumn(2)]
            public int blPressure;
            [LoadColumn(3)]
            public int BiSkinthck;
            [LoadColumn(5)]
            public int bmi;
            [LoadColumn(7)]
            public int age;
        }
    }

Some sample data being used is:

6,148,72,35,0,33.6,0.627,50,1
1,85,66,29,0,26.6,0.351,31,0
8,183,64,0,0,23.3,0.672,32,1
1,89,66,23,94,28.1,0.167,21,0
0,137,40,35,168,43.1,2.288,33,1
5,116,74,0,0,25.6,0.201,30,0

Where only some of the data is used.

J-school
  • 19
  • 7
  • You probably don't need the data, but try to add the items missing in the ModelInput class. – Jon Feb 14 '20 at 18:03
  • I only input those 5 values into it so there isn't anymore of the class to show :) – J-school Feb 15 '20 at 23:07
  • I mean add the missing items in the class and see if that fixes the error. :) – Jon Feb 16 '20 at 00:35
  • would that not be like adding a class thats never called, or setting variables that are never used? I'll definitely try because I'm still learning but im not sure it would do a whole load! – J-school Feb 22 '20 at 20:36

1 Answers1

0

You have some decimals in the data; change your ModelInput and ModelOutput to use floats instead (Also, the score of the prediction is a float in a regression)

public class ModelOutput
{
    [ColumnName("Score")]
    public float bmi;
}

public class ModelInput
{
    [LoadColumn(0)]
    public float NoPreg;
    [LoadColumn(2)]
    public float blPressure;
    [LoadColumn(3)]
    public float BiSkinthck;
    [LoadColumn(5)]
    public float bmi;
    [LoadColumn(7)]
    public float age;
}

Hope it helps!