Click or drag to resize
Accord.NET (logo)

RandomForest Class

Random Forest.
Inheritance Hierarchy
SystemObject
  Accord.MachineLearningTransformBaseDouble, Int32
    Accord.MachineLearningClassifierBaseDouble, Int32
      Accord.MachineLearningMulticlassClassifierBaseDouble
        Accord.MachineLearningMulticlassClassifierBase
          Accord.MachineLearning.DecisionTreesRandomForest

Namespace:  Accord.MachineLearning.DecisionTrees
Assembly:  Accord.MachineLearning (in Accord.MachineLearning.dll) Version: 3.8.0
Syntax
[SerializableAttribute]
public class RandomForest : MulticlassClassifierBase, 
	IParallel, ISupportsCancellation
Request Example View Source

The RandomForest type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyClasses Obsolete.
Gets the number of classes that can be recognized by this random forest.
Public propertyNumberOfClasses
Gets the number of classes expected and recognized by the classifier.
(Inherited from ClassifierBaseTInput, TClasses.)
Public propertyNumberOfInputs
Gets the number of inputs accepted by the model.
(Inherited from TransformBaseTInput, TOutput.)
Public propertyNumberOfOutputs
Gets the number of outputs generated by the model.
(Inherited from TransformBaseTInput, TOutput.)
Public propertyParallelOptions
Gets or sets the parallelization options for this algorithm.
Public propertyToken
Gets or sets a cancellation token that can be used to cancel the algorithm while it is running.
Public propertyTrees
Gets the trees in the random forest.
Top
Methods
  NameDescription
Public methodCompute Obsolete.
Computes the decision output for a given input vector.
Public methodDecide(TInput)
Computes class-label decisions for a given set of input vectors.
(Inherited from ClassifierBaseTInput, TClasses.)
Public methodDecide(Int32)
Computes a class-label decision for a given input.
(Inherited from MulticlassClassifierBase.)
Public methodDecide(Int32)
Computes a class-label decision for a given input.
(Inherited from MulticlassClassifierBase.)
Public methodDecide(Single)
Computes a class-label decision for a given input.
(Inherited from MulticlassClassifierBase.)
Public methodDecide(Single)
Computes a class-label decision for a given input.
(Inherited from MulticlassClassifierBase.)
Public methodDecide(Double)
Computes a class-label decision for a given input.
(Overrides ClassifierBaseTInput, TClassesDecide(TInput).)
Public methodDecide(TInput, TClasses)
Computes a class-label decision for a given input.
(Inherited from ClassifierBaseTInput, TClasses.)
Public methodDecide(Int32, Boolean)
Computes class-label decisions for the given input.
(Inherited from MulticlassClassifierBase.)
Public methodDecide(Int32, Double)
Computes class-label decisions for the given input.
(Inherited from MulticlassClassifierBase.)
Public methodDecide(Int32, Int32)
Computes class-label decisions for the given input.
(Inherited from MulticlassClassifierBase.)
Public methodDecide(Int32, Boolean)
Computes a class-label decision for a given input.
(Inherited from MulticlassClassifierBase.)
Public methodDecide(Int32, Double)
Computes a class-label decision for a given input.
(Inherited from MulticlassClassifierBase.)
Public methodDecide(Int32, Double)
Computes a class-label decision for a given input.
(Inherited from MulticlassClassifierBase.)
Public methodDecide(Int32, Int32)
Computes a class-label decision for a given input.
(Inherited from MulticlassClassifierBase.)
Public methodDecide(Int32, Int32)
Computes a class-label decision for a given input.
(Inherited from MulticlassClassifierBase.)
Public methodDecide(Single, Boolean)
Computes class-label decisions for the given input.
(Inherited from MulticlassClassifierBase.)
Public methodDecide(Single, Double)
Computes class-label decisions for the given input.
(Inherited from MulticlassClassifierBase.)
Public methodDecide(Single, Int32)
Computes class-label decisions for the given input.
(Inherited from MulticlassClassifierBase.)
Public methodDecide(Single, Boolean)
Computes a class-label decision for a given input.
(Inherited from MulticlassClassifierBase.)
Public methodDecide(Single, Double)
Computes a class-label decision for a given input.
(Inherited from MulticlassClassifierBase.)
Public methodDecide(Single, Double)
Computes a class-label decision for a given input.
(Inherited from MulticlassClassifierBase.)
Public methodDecide(Single, Int32)
Computes a class-label decision for a given input.
(Inherited from MulticlassClassifierBase.)
Public methodDecide(Single, Int32)
Computes a class-label decision for a given input.
(Inherited from MulticlassClassifierBase.)
Public methodDecide(TInput, Boolean)
Computes class-label decisions for the given input.
(Inherited from MulticlassClassifierBaseTInput.)
Public methodDecide(TInput, Double)
Computes class-label decisions for the given input.
(Inherited from MulticlassClassifierBaseTInput.)
Public methodDecide(TInput, Int32)
Computes class-label decisions for the given input.
(Inherited from MulticlassClassifierBaseTInput.)
Public methodDecide(TInput, Double)
Computes a class-label decision for a given input.
(Inherited from MulticlassClassifierBaseTInput.)
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.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Protected methodOnDeserializingMethod
Called when the object is being deserialized.
Public methodToMultilabel
Views this instance as a multi-label classifier, giving access to more advanced methods, such as the prediction of one-hot vectors.
(Inherited from MulticlassClassifierBaseTInput.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodTransform(TInput)
Applies the transformation to an input, producing an associated output.
(Inherited from ClassifierBaseTInput, TClasses.)
Public methodTransform(Int32)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBase.)
Public methodTransform(Single)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBase.)
Public methodTransform(Single)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBase.)
Public methodTransform(TInput)
Applies the transformation to a set of input vectors, producing an associated set of output vectors.
(Inherited from TransformBaseTInput, TOutput.)
Public methodTransform(TInput, TClasses)
Applies the transformation to an input, producing an associated output.
(Inherited from ClassifierBaseTInput, TClasses.)
Public methodTransform(Int32, Double)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBase.)
Public methodTransform(Int32, Int32)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBase.)
Public methodTransform(Int32, Boolean)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBase.)
Public methodTransform(Int32, Double)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBase.)
Public methodTransform(Int32, Double)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBase.)
Public methodTransform(Int32, Int32)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBase.)
Public methodTransform(Int32, Int32)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBase.)
Public methodTransform(Single, Boolean)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBase.)
Public methodTransform(Single, Double)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBase.)
Public methodTransform(Single, Int32)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBase.)
Public methodTransform(Single, Int32)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBase.)
Public methodTransform(Single, Boolean)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBase.)
Public methodTransform(Single, Double)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBase.)
Public methodTransform(Single, Double)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBase.)
Public methodTransform(Single, Int32)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBase.)
Public methodTransform(Single, Int32)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBase.)
Public methodTransform(TInput, Boolean)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.)
Public methodTransform(TInput, Double)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.)
Public methodTransform(TInput, Int32)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.)
Public methodTransform(TInput, Boolean)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.)
Public methodTransform(TInput, Double)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.)
Public methodTransform(TInput, Double)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.)
Public methodTransform(TInput, Int32)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.)
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
Remarks

Represents a random forest of DecisionTrees. For sample usage and example of learning, please see the documentation page for RandomForestLearning.

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