How to Build Machine Learning Models in C#

In this article, Yoon Hyup Hwang, the author of the book, C# Machine Learning Projects, discusses how to build machine learning models using C#. 

With 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 business 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 has become so large, many legacy systems and infrastructures do not typically support building machine learning models and systems very well.

For example,large number of machine learning models and systems are typically built using Python and/or R, but your company’s production system may supportonly C#. Many financial institutions have hard restrictions on programming languages that you can use in the production systems, and C# is one of the very few allowed programming languages. 

The objective of this article is to give you a brief summary of how you can use the Accord.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 using NuGet in Visual Studio.In order to install,open the package manager and install 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 corresponding classes in the framework.

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

Figure 1. ANaiveBayes classifier code snippet from the code repository of C# Machine Learning Projects

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

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

Note thatthe Accord.NET framework has a detailed documentation with examples. You can take a look at these examples 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 Accord.NET 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 using theOrdinaryLeastSquaresand SimpleLinearRegression classes. Use the following code to train a Simple Linear Regression model and make a prediction with the trained model:

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

Similarly, training an SVM modelwith the Accord.NET 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 FanChenLinSupportVectorRegression class, instead of LinearRegressionNewtonMethod. The following code shows how to train and make predictions with a Linear SVM regression model:

Figure 4. A 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. A KMeans class is used for training, and a KMeansClusterCollection class is used to contain the cluster analysis results. The following code shows how to train a K-Means Clustering algorithm:

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


This article gave you an introductory look on how to use the Accord.NET framework for building machine learning models in C#.  As you might have noticed, 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.This framework has a very detailed documentation with examples, so it is recommended that you take a look at it.


Also, the code repository of C# Machine Learning Projects will be a good resource for your future machine learning projects in C#, as it has a large number of advanced machine learning codes that range from data munging to feature engineering and model validations in C# with the Accord.NET framework. You can find this code repository at https://github.com/yoonhwang/CSharp-Machine-Learning-Projects.

Commentaires

Posts les plus consultés de ce blog

How to Build Machine Learning Models in C#

Artificial Intelligence By Example

25 Start-ups using Machine Learning differently in 2018: From farming to brewing beer to elder care