Machine Learning Models With C# – Part Two

Introduction

This is part two article series of “Machine Learning Models With C#”, before getting into the part two series, if you have not yet read the. part one, please have a look at Machine Learning Models with C# Part I.

In this article, we will have a detailed view on Setting Up the C# environment for Machine Learning.

ToC

  • Setting Visual Studio IDE for ML
  • Accord .NET Library
  • Deedle .NET Library
  • Conclusion

Setting Visual Studio IDE for ML

We will use Visual Studio as our Environment to develop ML using C#. You can Download latest version from here and install.

Accord .NET Library

  • Accord.NET is a framework for scientific computing in .NET
  • NuGet Package is available for this Library, we will this library
  • The main areas covered include numerical linear algebra, numerical optimisation, statistics, machine learning, artificial neural networks, signal and image processing, and support libraries

Let’s start installing the ML framework for C#, Accord.NET. It is easiest to install it through NuGet. To install it, open the package manager Tools | NuGet Package Manager | Package Manager Console and install Accord.MachineLearning and Accord.Controls by typing in the following commands:

PM> Install-Package Accord.MachineLearning
PM> Install-Package Accord.Controls

Now, let’s build a sample ML application using these Accord.NET packages. Open your Visual Studio and create a new Console Application under the Visual C# category. Use the preceding commands to install those Accord.NET packages through NuGet and add references to our project. You should see some Accord.NET packages added to your references in your Solutions Explorer and the result should look something like the following screenshot:

The code for this sample logistic regression classifier is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 using Accord.Controls;
using Accord.Statistics;
using Accord.Statistics.Models.Regression;
using Accord.Statistics.Models.Regression.Fitting;
 
namespace SampleAccordNETApp
{
    class Program
    {
        static void Main(string[] args)
        {
            double[][] inputs =
            {
                new double[] { 0, 0 },
                new double[] { 0.25, 0.25 }, 
                new double[] { 0.5, 0.5 }, 
                new double[] { 1, 1 },
            };
 int[] outputs =
            { 
                0,
                0,
                1,
                1,
            };
 
// Train a Logistic Regression model
            var learner = new IterativeReweightedLeastSquares<LogisticRegression>()
            {
                MaxIterations = 100
            };
            var logit = learner.Learn(inputs, outputs);
 // Predict output
            bool[] predictions = logit.Decide(inputs);
 // Plot the results
            ScatterplotBox.Show("Expected Results", inputs, outputs);
            ScatterplotBox.Show("Actual Logistic Regression Output", inputs, predictions.ToZeroOne());
 Console.ReadKey();
        }
    }
}

Deedle .NET Library

Deedle is an open-source .NET library for data frame programming. Deedle lets you do data manipulation in a way that is similar to R data frames and pandas data frames in Python. We will be using this package to load and manipulate the data for our ML projects in the following chapters. Similar to how we installed Accord.NET, we can install the Deedle package from NuGet. Open the package manager Tools | NuGet Package Manager | Package Manager Console and install Deedle using the following command:

PM> Install-Package Deedle

We are going to use daily AAPL stock price data from 2010 to 2013 for this exercise. Open your Visual Studio and create a new Console Application under the Visual C# category. Use the preceding command to install the Deedle library through NuGet and add references to your project. You should see the Deedle package added to your references in your Solutions Explorer.

Now, we are going to load the CSV data into a Deedle data frame and then do some data manipulations. First, we are going to update the index of the data frame with the Date field. Then, we are going to apply some arithmetic operations on the Open and Close columns to calculate the percentage changes from open to close prices. Lastly, we will calculate daily returns by taking the differences between the close and the previous close prices, dividing them by the previous close prices, and then multiplying it by 100. The code for this sample Deedle program is shown as follows:

using Deedle;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeedleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Read AAPL stock prices from a CSV file
            var root = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
            var aaplData = Frame.ReadCsv(Path.Combine(root, "table_aapl.csv"));
            // Print the data
            Console.WriteLine("-- Raw Data --");
            aaplData.Print();

            // Set Date field as index
            var aapl = aaplData.IndexRows<String>("Date").SortRowsByKey();
            Console.WriteLine("-- After Indexing --");
            aapl.Print();

            // Calculate percent change from open to close
            var openCloseChange = 
                ((
                    aapl.GetColumn<double>("Close") - aapl.GetColumn<double>("Open")
                ) / aapl.GetColumn<double>("Open")) * 100.0;
            aapl.AddColumn("openCloseChange", openCloseChange);
            Console.WriteLine("-- Simple Arithmetic Operations --");
            aapl.Print();

            // Shift close prices by one row and calculate daily returns
            var dailyReturn = aapl.Diff(1).GetColumn<double>("Close") / aapl.GetColumn<double>("Close") * 100.0;
            aapl.AddColumn("dailyReturn", dailyReturn);
            Console.WriteLine("-- Shift --");
            aapl.Print();

            Console.ReadKey();
        }
    }
}

Note: We will have a detailed code walk through of these two snippets in the next article. You all copy this code in you machine and let us debug each line in the next article.

Conclusion

In this article we had a look at the environment for ML using C# and installed two NuGet packages. And also we have wrote a sample code, and we will debug the application in the next article. Please do share your feedback in the comment section.

Happy Learning !!!! Happy Coding !!!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Website Built with WordPress.com.

Up ↑

%d bloggers like this: