How to Build Machine Learning Models in C#

I wanted to share this article by author Yoon Hyup Hwang of the book C# Machine Learning Projects. It's a good practical approach to C# ML.

With the rising importance of data and its usage in solving business problems, data science and machine learning have become a hot topic amongst various industries. As the desire to use and implement machine learning for various businesses use cases increases, more and more organizations are facing numerous challenges. For some, it might be difficult to find and source data scientists for their projects. For some others, the challenge can come from their technology infrastructure restrictions. Since the field of machine learning and data science is relatively new and the size of data is large, many legacy systems and infrastructures do not typically support building machine learning models and systems very well. One example can be the fact that a large number of machine learning models and systems are typically built using Python and/or R, but your company’s production system only supports C#. For example, many financial institutions have hard restrictions on programming languages that you can use in production systems, and C# is one of the very few allowed programming languages for them. 

In this article, Yoon Hyup Hwang, the author of the bookC# Machine Learning Projects, discusses how to build machine learning models using C#. The objective of this article is to give you a brief summary of how you can use theAccord.NET framework and its different modules to build various machine learning models for classification, regression, and clustering problems.

Installing Accord.NET
Accord.NET is a .NET framework that has various modules for machine learning, mathematics, statistics, computer vision, and other scientific computing. You can easily install this framework through NuGet in Visual Studio. For installation, open the package manager and install the Accord.NET machine learning module by typing the following command:

PM> Install-Package Accord.MachineLearning


Classification Models in Accord.NET
There are numerous classification models that you can build with the Accord.NET framework, such as Naive Bayes and RandomForest. Let’s briefly discuss how to build such models using the corresponding classes in the Accord.NET framework.

First, you can easily train a NaiveBayes classifier with the framework, using NaiveBayesLearning and NaiveBayes classes. Use the following code to train a NaiveBayes classifier and make a prediction with the trained classifier:

var learner =newNaiveBayesLearning<NormalDistribution>();
var model =learner.Learn(inputs, outputs);
varpreds=model.Decide(inputs);
Console.WriteLine("\n\n*Binary Naive Bayes Predictions: {0}", String.Join(", ", preds));

Code 1.NaiveBayes classifier code snippet from the code repository of C# Machine Learning Projects

Similarly, training a RandomForest classifier with the framework is also straightforward. The two main classes to use for training a RandomForest classifier are RandomForestLearning and RandomForest. The following code shows how to train and make predictions with a RandomForest classifier:

var learner =newRandomForestLearning()
{
       NumberOfTrees=3,
       CoverageRatio=0.9,
       SampleRatio=0.9
};
var model =learner.Learn(inputs, outputs);
varpreds=model.Decide(inputs);
Console.WriteLine("\n\n*Binary RandomForest Classifier Predictions: {0}", String.Join(", ", preds));

Code 2.RandomForest model code snippet from the code repository of C# Machine Learning Projects

The Accord.NET framework has a detailed documentation with examples, and you can take a look at them at http://accord-framework.net/docs/html/R_Project_Accord_NET.htm.

Regression Models in Accord.NET
The options for regression models in the Accord.NET framework are also comprehensive. You can easily train and build regression models using the framework. Let’s briefly discuss how you can train Linear Regression and Support Vector Machine (SVM) for regression problems.

First, you can easily train a Simple Linear Regression model with the framework, using the OrdinaryLeastSquaresand SimpleLinearRegression classes. Use the following code to train a Simple Linear Regression model and make a prediction with the trained model:

var learner =newOrdinaryLeastSquares()
{
       UseIntercept=true
};
var model =learner.Learn(inputs, outputs);
varpreds=model.Transform(inputs);
Console.WriteLine("\n\n* Linear Regression Preds: {0}", String.Join(", ", preds));

Code 3. Simple Linear Regression model code snippet from the code repository of C# Machine Learning Projects

Similarly, training anSVM with the framework is also straightforward. The two main classes to use for training an SVM regression model are LinearRegressionNewtonMethodand SupportVectorMachine. To train an SVM regression model with different kernels, such as polynomial or Gaussian, you can use theFanChenLinSupportVectorRegression class, in place of LinearRegressionNewtonMethod. The following code shows how to train and make predictions with a Linear SVM regression model:

var learner =newLinearRegressionNewtonMethod()
{
       Epsilon =2.1,
       Tolerance = 1e-5,
       UseComplexityHeuristic=true
};
var model =learner.Learn(inputs, outputs);
varpreds=model.Score(inputs);
Console.WriteLine("\n\n* Linear SVM Preds: {0}", String.Join(", ", preds));

Code 4. Linear SVM regression model code snippet from the code repository of C# Machine Learning Projects

Clustering Models in Accord.NET
Lastly, let’s briefly discuss how to build a K-Means Clustering algorithm with the Accord.NET framework. The two main classes to be used for training a K-Means Clustering algorithm are KMeans and KMeansClusterCollection. The KMeans class is used for training and the KMeansClusterCollection class is used to contain the cluster analysis results. The following code shows how to train a K-Means Clustering algorithm:

KMeanskmeans=newKMeans(2);
KMeansClusterCollection clusters =kmeans.Learn(sampleSet);

Console.WriteLine("\n\n* Clusters: {0}", String.Join(",", clusters.Decide(sampleSet)));

Code 5. K-Means Clustering algorithm code snippet from the code repository of C# Machine Learning Projects


In Closing

This article was written to give you an introductory look on how to use the Accord.NET framework for building machine learning models in C#. As you might have noticed from this article, building such machine learning models in C# with the Accord.NET framework is fairly straightforward and not too different from other machine learning libraries in other programming languages. The Accord.NET framework has a very detailed documentation with examples, so it is recommended that you take a look at their documentation as well. Also, the code repository of C# Machine Learning Projectswill also be a good resource for your future machine learning projects in C#, as it has a large number of advanced machine learning code that range from data munging to feature engineering and model validations in C# with the Accord.NET framework. You can find this code repository athttps://github.com/yoonhwang/CSharp-Machine-Learning-Projects.

Commentaires

Posts les plus consultés de ce blog

Artificial Intelligence By Example