Click or drag to resize
Accord.NET (logo)

RandomForestLearning Class

Random Forest learning algorithm.
Inheritance Hierarchy
SystemObject
  Accord.MachineLearningParallelLearningBase
    Accord.MachineLearning.DecisionTreesRandomForestLearning

Namespace:  Accord.MachineLearning.DecisionTrees
Assembly:  Accord.MachineLearning (in Accord.MachineLearning.dll) Version: 3.8.0
Syntax
[SerializableAttribute]
public class RandomForestLearning : ParallelLearningBase, 
	ISupervisedLearning<RandomForest, double[], int>
Request Example View Source

The RandomForestLearning type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyAttributes
Gets or sets the collection of attributes to be processed by the induced decision tree.
Public propertyCoverageRatio
Gets or sets the proportion of variables that can be used at maximum by each tree in the decision forest. Default is 1 (always use all variables).
Public propertyJoin
Gets or sets how many times the same variable can enter a tree's decision path. Default is 100.
Public propertyNumberOfTrees
Gets or sets the number of trees in the random forest.
Public propertyParallelOptions
Gets or sets the parallelization options for this algorithm.
(Inherited from ParallelLearningBase.)
Public propertySampleRatio
Gets the proportion of samples used to train each of the trees in the decision forest. Default is 0.632.
Public propertyToken
Gets or sets a cancellation token that can be used to cancel the algorithm while it is running.
(Inherited from ParallelLearningBase.)
Public propertyTrees Obsolete.
Gets or sets the number of trees in the random forest.
Top
Methods
  NameDescription
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodLearn(Double, Int32, Double)
Learns a model that can map the given inputs to the given outputs.
Public methodLearn(Int32, Int32, Double)
Learns a model that can map the given inputs to the given outputs.
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodRun Obsolete.
Runs the learning algorithm with the given data.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(Type)Overloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Top
Examples

This example shows the simplest way to induce a decision tree with continuous variables.

// Fix random seed for reproducibility
Accord.Math.Random.Generator.Seed = 1;

// In this example, we will process the famous Fisher's Iris dataset in 
// which the task is to classify weather the features of an Iris flower 
// belongs to an Iris setosa, an Iris versicolor, or an Iris virginica:
// 
//  - https://en.wikipedia.org/wiki/Iris_flower_data_set
// 

// First, let's load the dataset:
var iris = new DataSets.Iris();
double[][] inputs = iris.Instances; // flower features
int[] outputs = iris.ClassLabels; // flower categories

// Create the forest learning algorithm
var teacher = new RandomForestLearning()
{
    NumberOfTrees = 10, // use 10 trees in the forest
};

// Finally, learn a random forest from data
var forest = teacher.Learn(inputs, outputs);

// We can estimate class labels using
int[] predicted = forest.Decide(inputs);

// And the classification error (0.0006) can be computed as 
double error = new ZeroOneLoss(outputs).Loss(forest.Decide(inputs));

The next example shows how to induce a decision tree with continuous variables using a codebook to manage how input variables should be encoded.

// Fix random seed for reproducibility
Accord.Math.Random.Generator.Seed = 1;

// This example uses the Nursery Database available from the University of
// California Irvine repository of machine learning databases, available at
// 
//   http://archive.ics.uci.edu/ml/machine-learning-databases/nursery/nursery.names
// 
// The description paragraph is listed as follows.
// 
//   Nursery Database was derived from a hierarchical decision model
//   originally developed to rank applications for nursery schools. It
//   was used during several years in 1980's when there was excessive
//   enrollment to these schools in Ljubljana, Slovenia, and the
//   rejected applications frequently needed an objective
//   explanation. The final decision depended on three subproblems:
//   occupation of parents and child's nursery, family structure and
//   financial standing, and social and health picture of the family.
//   The model was developed within expert system shell for decision
//   making DEX (M. Bohanec, V. Rajkovic: Expert system for decision
//   making. Sistemica 1(1), pp. 145-157, 1990.).
// 

// Let's begin by loading the raw data. This string variable contains
// the contents of the nursery.data file as a single, continuous text.
// 
var nursery = new DataSets.Nursery(path: localPath);
int[][] inputs = nursery.Instances;
int[] outputs = nursery.ClassLabels;

// Now, let's create the forest learning algorithm
var teacher = new RandomForestLearning(nursery.VariableNames)
{
    NumberOfTrees = 1,
    SampleRatio = 1.0
};

// Finally, learn a random forest from data
var forest = teacher.Learn(inputs, outputs);

// We can estimate class labels using
int[] predicted = forest.Decide(inputs);

// And the classification error (0) can be computed as 
double error = new ZeroOneLoss(outputs).Loss(forest.Decide(inputs));
See Also