Tree: IBinaryTreeNode;
The Tree property returns the created decision tree.
A decision tree is a hierarchical tree of rules, where each object corresponds to a single node giving the decision. A rule is a logical construction represented as "if... then ...".
To execute the example add links to the Stat system assembly.
Public Sub UserProc;
Var
CART: SmCART;
y: Array[16] Of Integer;
x1: Array[16] Of Integer;
x2: Array[16] Of Integer;
x3dbl: array[16] Of double;
x4cat: array[16] Of integer;
TreeSizeSpecification: ITreeSpecification;
res: integer;
i: integer;
str: string;
d: double;
CatList: Array Of Integer;
Begin
// Create object to calculate classification tree
CART := New SmCART.Create;
// Set explained series values
y[0] := 1000; y[4] := 201; y[8] := -1; y[12] := 5002;
y[1] := 1000; y[5] := 201; y[9] := 5002; y[13] := 5002;
y[2] := 1000; y[6] := 201; y[10] := 5002; y[14] := -1;
y[3] := 1000; y[7] := 201; y[11] := 5002; y[15] := 5002;
// Set value of explanatory ordinal series x1
x1[0] := 0; x1[4] := 0; x1[8] := 0; x1[12] := 1;
x1[1] := 0; x1[5] := 0; x1[9] := 1; x1[13] := 1;
x1[2] := 0; x1[6] := 0; x1[10] := 1; x1[14] := 1;
x1[3] := 0; x1[7] := 0; x1[11] := 1; x1[15] := 1;
// Set value of explanatory ordinal series x2
x2[0] := 10; x2[4] := 10; x2[8] := 20; x2[12] := 20;
x2[1] := 10; x2[5] := 20; x2[9] := 10; x2[13] := 20;
x2[2] := 10; x2[6] := 20; x2[10] := 10; x2[14] := 20;
x2[3] := 10; x2[7] := 20; x2[11] := 20; x2[15] := 20;
// Set values of explanatory quantitative series
x3dbl[0] := 1; x3dbl[4] := 4; x3dbl[8] := 9; x3dbl[12] := 11;
x3dbl[1] := 2; x3dbl[5] := 6; x3dbl[9] := 9; x3dbl[13] := 12;
x3dbl[2] := 3; x3dbl[6] := 7; x3dbl[10] := 10; x3dbl[14] := 13;
x3dbl[3] := 5; x3dbl[7] := 8; x3dbl[11] := 10; x3dbl[15] := 14;
// Set values of explanatory categorical series
x4cat[0] := 1; x4cat[4] := 1; x4cat[8] := 2; x4cat[12] := 3;
x4cat[1] := 1; x4cat[5] := 1; x4cat[9] := 2; x4cat[13] := 3;
x4cat[2] := 1; x4cat[6] := 1; x4cat[10] := 2; x4cat[14] := 3;
x4cat[3] := 1; x4cat[7] := 2; x4cat[11] := 2; x4cat[15] := 3;
// Set parameters describing tree
TreeSizeSpecification := CART.TreeSizeSpecification;
TreeSizeSpecification.MaximumNumberOfLevels := 10;
TreeSizeSpecification.MinimumNumberOfCases := 2;
// Set explained series
CART.Dependent.Value := y;
// Set explanatory ordinal series
CART.ExplanatoriesOrdered.Add.Value := x1;
CART.ExplanatoriesOrdered.Add.Value := x2;
// Set explanatory quantitative series
cart.ExplanatoriesContinuous.Add.Value := x3dbl;
// Set explanatory categorical series
CART.ExplanatoriesCategorical.Add.Value := x4cat;
// Execute calculation
res := CART.Execute;
If res <> 0 Then
Debug.WriteLine("An error occurred");
Debug.WriteLine(CART.Errors);
// If calculation is executed without errors, display results
Else
Debug.WriteLine("Initial values - processed values:");
Debug.Indent;
For i := 0 To CART.Dependent.Value.Length - 1 Do
str := i.ToString + ": ";
d := CART.Dependent.Value[i];
str := str + d.ToString + " - ";
d := CART.FilledDependent[i];
str := str + d.ToString + " ";
Debug.WriteLine(str);
End For;
Debug.Unindent;
// Display list of categories
CatList := CART.CategoriesList;
If CatList.Length > 0 Then
Debug.WriteLine("List of categories:");
Debug.Indent;
For i := 0 To CatList.Length - 1 Do
Debug.WriteLine(CatList[i]);
End For;
Debug.Unindent;
End If;
// Display decision tree
Debug.WriteLine("Decision tree:");
print(CART.Tree);
End If;
End Sub UserProc;
// Procedure of outputting decision tree
Sub print(node: IBinaryTreeNode);
Var
i: Integer;
Categorical: Boolean = False;
Begin
Debug.Indent;
Debug.WriteLine("Node number: " + node.NodeIndex.ToString);
Debug.WriteLine("Improvement: " + node.Improvement.ToString);
Debug.WriteLine("Number of cases in node: " + node.Total.ToString);
Debug.WriteLine("Criterion index: " + node.ExplanatorieIndex.ToString);
Debug.WriteLine("Criterion name: " + node.Name);
Debug.Write("Criterion type: ");
Select Case node.PropertyType
Case
DecisionTreePropertyType.Categorical: Debug.WriteLine("categorical");
Categorical := True;
Case DecisionTreePropertyType.NoProperty: Debug.WriteLine("-");
Case DecisionTreePropertyType.Ordered: Debug.WriteLine("ordinal");
Case DecisionTreePropertyType.Value: Debug.WriteLine("quantitative");
End Select;
If Not Categorical Then
Debug.WriteLine("Criterion value: " + node.Value.ToString);
End If;
If (node.LeftNode <> Null) Then
Debug.WriteLine("Left branch: ");
print(node.LeftNode);
End If;
If (node.LeftNodeCategories.Length <> 0) And Categorical Then
Debug.WriteLine("List of categories which are sorted to the left branch: ");
For i := 0 To node.LeftNodeCategories.Length - 1 Do
Debug.WriteLine(node.LeftNodeCategories[i]);
End For;
End If;
If (node.RightNode <> Null) Then
Debug.WriteLine("Right branch: ");
print(node.RightNode);
End If;
If (node.RightNodeCategories.Length <> 0) And Categorical Then
Debug.WriteLine("List of categories which are sorted to the right branch: ");
For i := 0 To node.RightNodeCategories.Length - 1 Do
Debug.WriteLine(node.RightNodeCategories[i]);
End For;
End If;
Debug.Unindent;
End Sub print;
After executing the example task will be calculated using classification tree. Calculation results are displayed to the browser console.
See also: