Forest: IBinaryTreeForest;
The Forest property returns an array of trees.
To determine the number of tree in random forest, use the ISmRandomForest.ForestSize property.
Add a link to the Stat system assembly.
Sub UserProc;
Var
rf: SmRandomForest;
Y, OrdX: Array[16] Of Integer;
X: Array[16] Of Double;
res, i: Integer;
Explanatories: ISlSeries;
Explan: ISlSerie;
ExplOrdered: ISlSeriesInt;
ExpOrd: ISlSerieInt;
Forest: IBinaryTreeForest;
TreeNode: IBinaryTreeNode;
Begin
rf := New SmRandomForest.Create;
// Set explained series
Y[00] := 0; Y[08] := 1;
Y[01] := 1; Y[09] := 0;
Y[02] := 0; Y[10] := 2;
Y[03] := 2; Y[11] := 2;
Y[04] := 1; Y[12] := 1;
Y[05] := 1; Y[13] := 2;
Y[06] := 2; Y[14] := 0;
Y[07] := 1; Y[15] := 2;
rf.Dependent.Value := Y;
// Set explained series (ordinal)
OrdX[00] := 0; OrdX[08] := 0;
OrdX[01] := 0; OrdX[09] := 1;
OrdX[02] := 0; OrdX[10] := 1;
OrdX[03] := 0; OrdX[11] := 1;
OrdX[04] := 0; OrdX[12] := 1;
OrdX[05] := 0; OrdX[13] := 1;
OrdX[06] := 0; OrdX[14] := 1;
OrdX[07] := 0; OrdX[15] := 1;
ExplOrdered := rf.ExplanatoriesOrdered;
ExplOrdered.Clear;
ExpOrd:= ExplOrdered.Add;
ExpOrd.Value := OrdX;
// Set explanatory series (quantitative)
X[00] := 34.13; X[08] := 29.27;
X[01] := 21.52; X[09] := 23.39;
X[02] := 25.43; X[10] := 28.28;
X[03] := 43.42; X[11] := 43.55;
X[04] := 40.19; X[12] := 44.80;
X[05] := 24.97; X[13] := 23.23;
X[06] := 20.57; X[14] := 37.14;
X[07] := 30.81; X[15] := 27.44;
Explanatories := rf.ExplanatoriesContinuous;
Explanatories.Clear;
Explan := Explanatories.Add;
Explan.Value := X;
// Number of attributes
rf.NumberOfPredictors := 2;
// Trees size
rf.TreeSizeSpecification.MaximumNumberOfLevels := 10;
rf.TreeSizeSpecification.MinimumNumberOfCases := 2;
// Perform calculation and display error messages
res:=rf.Execute;
If (res = 0) Then
Forest := rf.Forest;
Debug.WriteLine("Number of trees in ensemble: "+Forest.Count.ToString);
For i := 0 To Forest.Count - 1 Do
TreeNode := Forest.Item(i);
Debug.WriteLine("Tree "+(i+1).ToString);
Debug.WriteLine(" Root node");
print(TreeNode);
End For;
Else
Debug.WriteLine(rf.Errors);
End If;
End Sub UserProc;
…
Sub print(node: IBinaryTreeNode);
Begin
Debug.WriteLine(" Number of node in tree: " + node.NodeIndex.ToString);
Debug.WriteLine(" Improvement coefficient: " + node.Improvement.ToString);
If(node.LeftNode <> Null) Then
Debug.WriteLine(" Left branch");
print(node.LeftNode);
End If;
If(node.RightNode <> Null) Then
Debug.WriteLine(" Right branch");
print(node.RightNode);
End If;
End Sub print;
After executing the example the console window displays:
Number of trees in ensemble.
Information about trees in ensemble:
Tree root node.
Left branch of tree.
Right branch of tree.
See also: