static void Main(string[] args)
        {
            var mlContext = new MLContext();

            // Creates a loader which will help format the data
            var reader = mlContext.Data.CreateTextLoader(
                columns: new TextLoader.Column[]
            {
                new TextLoader.Column(CocoaPercent, DataKind.Single, 1),
                new TextLoader.Column("Label", DataKind.Single, 4) // Customer happiness is the label. The predicted features is called the Label.
            },
                hasHeader: true                                    // First line of the data file is a header and not data row
                );

            IDataView trainingData = reader.Load(TrainDataPath);

            PreviewUtil.Show(trainingData);

            // Creates a pipeline - All operations like data operations, transformation, training are bundled as a pipeline
            var pipeline =
                mlContext.Transforms.Concatenate(outputColumnName: "Features", inputColumnNames: CocoaPercent)
                .Append(mlContext.Regression.Trainers.LbfgsPoissonRegression());

            // Train the model
            var trainingModel = pipeline.Fit(trainingData);

            // Using the training for one-time prediction
            var predictionEngine = mlContext.Model.CreatePredictionEngine <ChocolateInput, ChocolateOutput>(trainingModel);

            // Get the prediction
            ChocolateOutput prediction = predictionEngine.Predict(new ChocolateInput()
            {
                CocoaPercent = 100
            });

            Console.WriteLine($"Predicted happiness: {prediction.CustomerHappiness}");

            ChartGeneratorUtil.PlotRegressionChart(new PlotChartGeneratorModel()
            {
                Title      = "Chocolate Consumer Happiness Prediction",
                LabelX     = "Cocoa Percent",
                LabelY     = "Customer Happiness",
                ImageName  = "CocoaPercentToHappiness.png",
                PointsList = new List <PlotChartPointsList>
                {
                    new PlotChartPointsList {
                        Points = ChartGeneratorUtil.GetChartPointsFromFile(TrainDataPath, 1, 4).ToList()
                    }
                },
                MaxLimitY = ChartGeneratorUtil.GetMaxColumnValueFromFile(TrainDataPath, 4) + 10
            });

            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            // Create MLContext to be shared across the model creation workflow objects
            MLContext mlContext = new MLContext();

            // Define a reader: specify the data columns, types, and where to find them in the text file
            var reader = mlContext.Data.CreateTextLoader(
                columns: new TextLoader.Column[]
            {
                new TextLoader.Column("AverageGoalsPerMatch", DataKind.Single, 0),
                new TextLoader.Column("Label", DataKind.Boolean, 1)
            },
                // First line of the file is a header, not a data row
                hasHeader: true
                );

            // Read the training data
            var trainingData = reader.Load(TrainDataPath);
            var preview      = trainingData.Preview();

            Console.WriteLine($"******************************************");
            Console.WriteLine($"Loaded training data: {preview}");
            Console.WriteLine($"******************************************");

            /* Build the basic graph */
            ChartGeneratorUtil.PlotRegressionChart(new PlotChartGeneratorModel
            {
                Title      = string.Empty,
                LabelX     = "Average number of goals scored per match",
                LabelY     = "Competition Win",
                ImageName  = "CompetitionWin.png",
                PointsList = new List <PlotChartPointsList>
                {
                    new PlotChartPointsList {
                        Points = ChartGeneratorUtil.GetChartPointsFromFile(TrainDataPath, 0, 1, 1, 1).ToList(), Color = CommonConstants.PPLplotColorRed
                    },
                    new PlotChartPointsList {
                        Points = ChartGeneratorUtil.GetChartPointsFromFile(TrainDataPath, 0, 1, 1, 0).ToList()
                    }
                },
                MaxLimitY          = 1.25,
                MinLimitY          = -0.25,
                MinLimitX          = -0.25,
                MaxLimitX          = ChartGeneratorUtil.GetMaxColumnValueFromFile(TrainDataPath, 0) + 0.25,
                DrawRegressionLine = false
            });

            // Apply standard ML.NET normalization to the raw data
            var pipeline =
                mlContext.Transforms.Concatenate("Features", "AverageGoalsPerMatch")
                .Append(mlContext.BinaryClassification.Trainers.LogisticRegression(
                            new Microsoft.ML.Trainers.LogisticRegression.Options
            {
                ShowTrainingStats = true
            }
                            ));

            // Train the model
            var model = pipeline.Fit(trainingData);

            // View training stats
            var linearModel = model.LastTransformer.Model.SubModel as LinearBinaryModelParameters;

            // This works out the loss
            var coefficient = linearModel.Weights.FirstOrDefault();
            var intercept   = linearModel.Bias;
            var step        = 3 / (double)300;
            var testX       = Enumerable.Range((int)0, 300).Select(v => (v * step) + 0).ToList();
            var loss        = new List <double>();

            foreach (var x in testX)
            {
                loss.Add(Sigmoid(x * coefficient + intercept));
            }

            // Get an array of the average data points
            var lossPoints = GetAvgChartPointsFromData(testX, loss);

            /* Build the Competition Win Likelihood graph */
            ChartGeneratorUtil.PlotRegressionChart(new PlotChartGeneratorModel
            {
                Title      = string.Empty,
                LabelX     = "Average number of goals per match",
                LabelY     = "Competition Win Likelihood",
                ImageName  = "CompetitionWinLikelihood.png",
                PointsList = new List <PlotChartPointsList>
                {
                    new PlotChartPointsList {
                        Points = ChartGeneratorUtil.GetChartPointsFromFile(TrainDataPath, 0, 1, 1, 1).ToList(), Color = CommonConstants.PPLplotColorRed
                    },
                    new PlotChartPointsList {
                        Points = ChartGeneratorUtil.GetChartPointsFromFile(TrainDataPath, 0, 1, 1, 0).ToList()
                    }
                },
                MaxLimitY            = 1.25,
                MinLimitY            = -0.25,
                MinLimitX            = -0.25,
                MaxLimitX            = ChartGeneratorUtil.GetMaxColumnValueFromFile(TrainDataPath, 0) + 0.25,
                DrawRegressionLine   = true,
                RegressionPointsList = new PlotChartPointsList {
                    Points = lossPoints.ToList(), Color = CommonConstants.PPLplotColorBlack
                }
            });

            // Use the trained model for one-time prediction
            var predictionEngine = model.CreatePredictionEngine <FootballInput, FootballOutput>(mlContext);

            // Obtain the prediction
            var goalsPerMatch = 2.422870462f;
            var prediction    = predictionEngine.Predict(new FootballInput
            {
                AverageGoalsPerMatch = goalsPerMatch
            });

            Console.WriteLine($"*************************************");
            Console.WriteLine($"Probability of winning this year: { prediction.WonCompetition * 100 }%");
            Console.WriteLine($"*************************************");

            /* Build the Probability of winning this year graph */
            ChartGeneratorUtil.PlotRegressionChart(new PlotChartGeneratorModel
            {
                Title      = "Probability of winning this year",
                LabelX     = "Average number of goals per match",
                LabelY     = "Competition Win Likelihood",
                ImageName  = "ProbabilityOfWinningThisYear.png",
                PointsList = new List <PlotChartPointsList>
                {
                    new PlotChartPointsList {
                        Points = ChartGeneratorUtil.GetChartPointsFromFile(TrainDataPath, 0, 1, 1, 1).ToList(), Color = CommonConstants.PPLplotColorRed
                    },
                    new PlotChartPointsList {
                        Points = ChartGeneratorUtil.GetChartPointsFromFile(TrainDataPath, 0, 1, 1, 0).ToList()
                    }
                },
                MaxLimitY          = 1.25,
                MinLimitY          = -0.25,
                MinLimitX          = -0.25,
                MaxLimitX          = ChartGeneratorUtil.GetMaxColumnValueFromFile(TrainDataPath, 0) + 0.25,
                DrawRegressionLine = true,
                DashedPoint        = new PlotChartPoint {
                    X = goalsPerMatch, Y = prediction.WonCompetition
                },
                RegressionPointsList = new PlotChartPointsList {
                    Points = lossPoints.ToList(), Color = CommonConstants.PPLplotColorBlack
                }
            });
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            // Create MLContext to be shared across the model creation workflow objects
            MLContext mlContext = new MLContext();

            // Read the training data
            var trainingData = mlContext.Data.LoadFromTextFile <TreeInput>(TrainDataPath, separatorChar: '\t', hasHeader: true);

            // Read the training data
            var preview = trainingData.Preview();

            Console.WriteLine($"******************************************");
            Console.WriteLine($"Loaded training data: {preview}");
            Console.WriteLine($"******************************************");

            /* Build the classification plot for leaf features graph */
            ChartGeneratorUtil.PlotRegressionChart(new PlotChartGeneratorModel
            {
                Title      = "Classification plot for leaf features",
                LabelX     = "leaf width",
                LabelY     = "leaf length",
                ImageName  = "ClassificationPlotForLeafFeatures.png",
                PointsList = new List <PlotChartPointsList>
                {
                    new PlotChartPointsList {
                        Points = ChartGeneratorUtil.GetChartPointsFromFile(TrainDataPath, 0, 1, 4, 1).ToList(), Color = CommonConstants.PPLplotColorGreen
                    },
                    new PlotChartPointsList {
                        Points = ChartGeneratorUtil.GetChartPointsFromFile(TrainDataPath, 0, 1, 4, 0).ToList(), Color = CommonConstants.PPLplotColorBlue
                    }
                },
                MaxLimitX          = ChartGeneratorUtil.GetMaxColumnValueFromFile(TrainDataPath, 0) + 0.25,
                MaxLimitY          = ChartGeneratorUtil.GetMaxColumnValueFromFile(TrainDataPath, 1) + 0.25,
                DrawRegressionLine = false
            });

            /* Build the classification plot for trunk features graph */
            ChartGeneratorUtil.PlotRegressionChart(new PlotChartGeneratorModel
            {
                Title      = "Classification plot for trunk features",
                LabelX     = "trunk girth",
                LabelY     = "trunk height",
                ImageName  = "ClassificationPlotForTrunkFeatures.png",
                PointsList = new List <PlotChartPointsList>
                {
                    new PlotChartPointsList {
                        Points = ChartGeneratorUtil.GetChartPointsFromFile(TrainDataPath, 2, 3, 4, 1).ToList(), Color = CommonConstants.PPLplotColorGreen
                    },
                    new PlotChartPointsList {
                        Points = ChartGeneratorUtil.GetChartPointsFromFile(TrainDataPath, 2, 3, 4, 0).ToList(), Color = CommonConstants.PPLplotColorBlue
                    }
                },
                MaxLimitX = ChartGeneratorUtil.GetMaxColumnValueFromFile(TrainDataPath, 0) + 0.25,
                MaxLimitY = ChartGeneratorUtil.GetMaxColumnValueFromFile(TrainDataPath, 1) + 0.25
            });

            // Apply standard ML.NET normalization to the raw data
            var pipeline =
                // Specify the support vector machine trainer
                mlContext.Transforms.Concatenate("Features", "LeafWidth", "LeafLength", "TrunkGirth", "TrunkHeight")
                .Append(mlContext.BinaryClassification.Trainers.LinearSupportVectorMachines());

            // Train the model
            var model = pipeline.Fit(trainingData);

            // Use the trained model for one-time prediction
            var predictionEngine = model.CreatePredictionEngine <TreeInput, TreeOutput>(mlContext);

            // Obtain the prediction
            var prediction = predictionEngine.Predict(new TreeInput
            {
                LeafWidth   = 5.13E+00f,
                LeafLength  = 6.18E+00f,
                TrunkGirth  = 8.26E+00f,
                TrunkHeight = 8.74E+00f
            });

            Console.WriteLine($"*************************************");
            Console.WriteLine("Tree type {0}", prediction.TreeType ? "0" : "1");
            Console.WriteLine($"Score: {prediction.Score}");
            Console.WriteLine($"Probability: {prediction.Probability}");
            Console.WriteLine($"*************************************");

            // Build the SVM plot for leaf features graph
            ChartGeneratorUtil.PlotRegressionChart(new PlotChartGeneratorModel
            {
                Title      = "SVM plot for leaf features",
                LabelX     = "leaf width",
                LabelY     = "leaf length",
                ImageName  = "SVMPlotForLeafFeatures.png",
                PointsList = new List <PlotChartPointsList>
                {
                    new PlotChartPointsList {
                        Points = ChartGeneratorUtil.GetChartPointsFromFile(TrainDataPath, 0, 1, 4, 1).ToList(), Color = CommonConstants.PPLplotColorGreen
                    },
                    new PlotChartPointsList {
                        Points = ChartGeneratorUtil.GetChartPointsFromFile(TrainDataPath, 0, 1, 4, 0).ToList(), Color = CommonConstants.PPLplotColorBlue
                    }
                },
                MaxLimitX               = ChartGeneratorUtil.GetMaxColumnValueFromFile(TrainDataPath, 0) + 0.25,
                MaxLimitY               = ChartGeneratorUtil.GetMaxColumnValueFromFile(TrainDataPath, 1) + 0.25,
                DrawRegressionLine      = false,
                DrawVectorMachinesAreas = true,
            });

            // Build the SVM plot for trunk features graph
            ChartGeneratorUtil.PlotRegressionChart(new PlotChartGeneratorModel
            {
                Title      = "SVM plot for trunk features",
                LabelX     = "trunk girth",
                LabelY     = "trunk height",
                ImageName  = "SVMPlotForTrunkFeatures.png",
                PointsList = new List <PlotChartPointsList>
                {
                    new PlotChartPointsList {
                        Points = ChartGeneratorUtil.GetChartPointsFromFile(TrainDataPath, 2, 3, 4, 1).ToList(), Color = CommonConstants.PPLplotColorGreen
                    },
                    new PlotChartPointsList {
                        Points = ChartGeneratorUtil.GetChartPointsFromFile(TrainDataPath, 2, 3, 4, 0).ToList(), Color = CommonConstants.PPLplotColorBlue
                    }
                },
                MaxLimitX               = ChartGeneratorUtil.GetMaxColumnValueFromFile(TrainDataPath, 0) + 0.25,
                MaxLimitY               = ChartGeneratorUtil.GetMaxColumnValueFromFile(TrainDataPath, 1) + 0.25,
                DrawRegressionLine      = false,
                DrawVectorMachinesAreas = true,
            });
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            MLContext mlContext = new MLContext();

            // Define a reader: specify the data columns, types, and where to find them in the text file
            var reader = mlContext.Data.CreateTextLoader(
                columns: new TextLoader.Column[]
            {
                new TextLoader.Column("CocoaPercent", DataKind.Single, 1),
                new TextLoader.Column("Label", DataKind.Single, 4)
            },
                // First line of the file is a header, not a data row
                hasHeader: true
                );

            // Create a preview and print out like a CSV
            var trainingData = reader.Load(TrainDataPath);
            var preview      = trainingData.Preview(10);

            Console.WriteLine($"******************************************");
            Console.WriteLine($"Loaded training data: {preview.ToString()}");
            Console.WriteLine($"******************************************");
            foreach (var columnInfo in preview.ColumnView)
            {
                Console.Write($"{columnInfo.Column.Name},");
            }
            Console.WriteLine();
            foreach (var rowInfo in preview.RowView)
            {
                foreach (var row in rowInfo.Values)
                {
                    Console.Write($"{row.Value},");
                }
                Console.WriteLine();
            }

            /* Create pipeline */
            // Apply standard ML.NET normalization to the raw data
            var pipeline =
                // Specify the PoissonRegression regression trainer
                mlContext.Transforms.Concatenate("Features", "CocoaPercent")
                .Append(mlContext.Regression.Trainers.PoissonRegression());

            /* Train the model */
            var model = pipeline.Fit(trainingData);

            /* Get the prediction */
            // Use the trained model for one-time prediction
            var predictionEngine = model.CreatePredictionEngine <ChocolateInput, ChocolateOutput>(mlContext);

            // Obtain the prediction
            var prediction = predictionEngine.Predict(new ChocolateInput
            {
                CocoaPercent = 65, // trained value 65
            });

            Console.WriteLine($"*************************************");
            Console.WriteLine($"Predicted customer happiness: {prediction.CustomerHappiness:0.##}");
            Console.WriteLine($"*************************************");

            // Generate graph with training data
            ChartGeneratorUtil.PlotRegressionChart(new PlotChartGeneratorModel
            {
                Title      = "Chocolate Consumer Happiness Prediction",
                LabelX     = "Cocoa Percent",
                LabelY     = "Customer Happiness",
                ImageName  = "CocoaPercentToHappiness.png",
                PointsList = new List <PlotChartPointsList>
                {
                    new PlotChartPointsList {
                        Points = ChartGeneratorUtil.GetChartPointsFromFile(TrainDataPath, 1, 4).ToList()
                    }
                },
                MaxLimitY = ChartGeneratorUtil.GetMaxColumnValueFromFile(TrainDataPath, 4) + 10,
            });
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            var mlContext = new MLContext();

            var reader = mlContext.Data.CreateTextLoader(
                columns: new TextLoader.Column[]
            {
                new TextLoader.Column("Weight", DataKind.Single, 0),
                new TextLoader.Column("CocoaPercent", DataKind.Single, 1),
                new TextLoader.Column("Cost", DataKind.Single, 2),
                new TextLoader.Column("Label", DataKind.Single, 3)
            },
                // First line of the file is a header, not a data row
                hasHeader: true
                );

            var trainingData = reader.Load(TrainDataPath);
            var preview      = trainingData.Preview();

            Console.WriteLine($"******************************************");
            Console.WriteLine($"Loaded training data: {preview.ToString()}");
            Console.WriteLine($"******************************************");

            var pipeline =
                // Features to include in the prediction
                mlContext.Transforms.Concatenate("Features", "Weight", "CocoaPercent", "Cost")
                // Specify the regression trainer
                .Append(mlContext.Regression.Trainers.PoissonRegression());

            // Train the model
            var model = pipeline.Fit(trainingData);

            // The model's feature weight coefficients
            var regressionModel = model.LastTransformer.Model;
            var weights         = regressionModel.Weights;
            var intercept       = regressionModel.Bias;

            Console.WriteLine($"Coefficients: Weight={weights[0]:0.##}, CocoaPercent={weights[1]:0.##}, Cost={weights[2]:0.##}");

            /* Build the graph */
            var featureColumn = reader.GetOutputSchema().GetColumnOrNull("CocoaPercent").Value;

            ChartGeneratorUtil.PlotRegressionChart(new PlotChartGeneratorModel
            {
                Title      = "Chocolate Consumer Happiness Prediction",
                LabelX     = featureColumn.Name,
                LabelY     = "Customer Happiness",
                ImageName  = "FeatureToHappiness.png",
                PointsList = new List <PlotChartPointsList>
                {
                    new PlotChartPointsList {
                        Points = ChartGeneratorUtil.GetChartPointsFromFile(TrainDataPath, featureColumn.Index, 3).ToList()
                    }
                },
                MinLimitX = ChartGeneratorUtil.GetMinColumnValueFromFile(TrainDataPath, featureColumn.Index),
                MaxLimitX = ChartGeneratorUtil.GetMaxColumnValueFromFile(TrainDataPath, featureColumn.Index) + 0.25,
                MaxLimitY = ChartGeneratorUtil.GetMaxColumnValueFromFile(TrainDataPath, 3) + 5
            });

            // Compute the quality metrics of our prediction model
            var predictions = model.Transform(trainingData);

            PrintMetrics(mlContext, predictions);

            Console.ReadKey();
        }