Reader small image

You're reading from  Hands-On Neural Network Programming with C#

Product typeBook
Published inSep 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781789612011
Edition1st Edition
Languages
Right arrow
Author (1)
Matt Cole
Matt Cole
author image
Matt Cole

Matt R. Cole is a developer and author with 30 years' experience. Matt is the owner of Evolved AI Solutions, a provider of advanced Machine Learning/Bio-AI, Microservice and Swarm technologies. Matt is recognized as a leader in Microservice and Artificial Intelligence development and design. As an early pioneer of VOIP, Matt developed the VOIP system for NASA for the International Space Station and Space Shuttle. Matt also developed the first Bio Artificial Intelligence framework which completely integrates mirror and canonical neurons. In his spare time Matt authors books, and continues his education taking every available course in advanced mathematics, AI/ML/DL, Quantum Mechanics/Physics, String Theory and Computational Neuroscience.
Read more about Matt Cole

Right arrow

Decision Trees and Random Forests

Decision trees and random forests are powerful techniques that you can use to add power to your applications. Let's walk through some concepts and some code and hopefully have you up and running in no time.

In this chapter, we are going to learn about decision trees and random forests. We will:

  • Work through a lot of code samples to show you how you can add this powerful functionality to your applications
  • Discuss decision trees
  • Discuss random forests

Technical requirements

Decision trees

Decision trees can be used for both classification and regression. Decision trees answer sequential questions with a yes/no, true/false response. Based upon those responses, the tree follows predetermined paths to reach its goal. Trees are more formally a version of what is known as a directed acyclic graph. Finally, a decision tree is built using the entire dataset and all features.

Here is an example of a decision tree. You may not know it as a decision tree, but for sure you know the process. Anyone for a doughnut?

As you can see, the flow of a decision tree starts at the top and works its way downward until a specific result is achieved. The root of the tree is the first decision that splits the dataset. The tree recursively splits the dataset according to what is known as the splitting metric at each node. Two of the most popular metrics are Gini Impurity...

Random forests

We have talked about decision trees, and now it’s time to discuss random forests. Very basically, a random forest is a collection of decision trees. In random forests, a fraction of the number of total rows and features are selected at random to train on. A decision tree is then built upon this subset. This collection will then have the results aggregated into a single result.

Random forests can also reduce bias and variance. How do they do this? By training on different data samples, or by using a random subset of features. Let’s take an example. Let’s say we have 30 features. A random forest might only use 10 of these features. That leaves 20 features unused, but some of those 20 features might be important. Remember that a random forest is a collection of decision trees. Therefore, in each tree, if we utilize 10 features, over time most if...

SharpLearning

Let’s now turn our attention to an incredible open source package, SharpLearning. SharpLearning is an excellent machine learning framework for individuals to learn about many aspects of machine learning, including decision trees and random forests as we described in the preceding sections. Let’s spend a few minutes getting familiar with a few things before we dive into some code samples and example applications.

Terminology

Throughout this chapter you are going to see the following terms used. Here is the context for what each of them means:

  • Learner: This refers to a machine learning algorithm.
  • Model: This refers to a machine learning model.
  • Hyper-parameters: These are the parameters used to adjust...

Example code and applications

In the next few sections, we are going to look at some code samples without all the verbosity. This will be pure C# code so it should be something easily understood by all.

Let’s take a quick look at how we can use SharpLearning to predict observations. I’ll show you an entire code sample without the verbosity:

var parser = new CsvParser(() =>new StringReader(Resources.AptitudeData));
var observations = parser.EnumerateRows(v => v != "Pass").ToF64Matrix();
var targets = parser.EnumerateRows("Pass").ToF64Vector();
var rows = targets.Length;
var learner = new ClassificationDecisionTreeLearner(100, 1, 2, 0.001, 42);
varsut = learner.Learn(observations, targets);
var predictions = sut.Predict(observations);
var evaluator = new TotalErrorClassificationMetric<double>();
var error = evaluator.Error(targets, predictions...

Summary

In this chapter, we learned about decision trees and random forests. We also learned how to use the open source framework, SharpLearn, to add these powerful features to our applications. In the next chapter, we are going to learn about facial and motion detection and show you how you can enable your application with this exciting technology! You’ll meet Frenchie, my pet French Bulldog, who will demonstrate most of the samples we will show. Also, we have a guest poser you will just have to see!

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Neural Network Programming with C#
Published in: Sep 2018Publisher: PacktISBN-13: 9781789612011
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Matt Cole

Matt R. Cole is a developer and author with 30 years' experience. Matt is the owner of Evolved AI Solutions, a provider of advanced Machine Learning/Bio-AI, Microservice and Swarm technologies. Matt is recognized as a leader in Microservice and Artificial Intelligence development and design. As an early pioneer of VOIP, Matt developed the VOIP system for NASA for the International Space Station and Space Shuttle. Matt also developed the first Bio Artificial Intelligence framework which completely integrates mirror and canonical neurons. In his spare time Matt authors books, and continues his education taking every available course in advanced mathematics, AI/ML/DL, Quantum Mechanics/Physics, String Theory and Computational Neuroscience.
Read more about Matt Cole