TreeNode
A node in a DecisionTree: either a leaf carrying a boolean prediction, or an internal node that branches on a boolean feature (false -> left, true -> right).
Operations
- New
- NewInternal
- NewLeaf
- Fit
- Gini
- IsFitted
- Load
- LoadCsv
- Matches
- Mismatches
- Predict
- PredictAll
- Query
- ReadFrom
- Score
- Split
- Store
- Train
- WriteTo
Fit #
Fits a real recursive binary tree to boolean training data. Each row is a set of boolean features with the class label in the LAST column. Nodes branch on the feature with the lowest weighted Gini impurity and stop at max_depth, min_samples, or a pure node (leaf = majority class).
method : public : Fit(input:Bool[,]) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| input | Bool | training matrix (features..., label) of Bool |
Example
data := [
[false, false, false],
[false, true, false],
[true, false, false],
[true, true, true]]; # label = f0 AND f1
tree := DecisionTree->New(4, 1);
tree->Fit(data);
tree->Predict([true, true])->PrintLine(); # trueGini # native
Calculates the Gini index
function : native : Gini(acheived:Float, goal:Float) ~ FloatParameters
| Name | Type | Description |
|---|---|---|
| acheived | Float | number acheived |
| goal | Float | achievement target |
Return
| Type | Description |
|---|---|
| Float | Gini index |
IsFitted #
Whether the tree has been fitted.
method : public : IsFitted() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if fitted |
Load # function
Loads a fitted tree from a file.
function : Load(filename:String) ~ DecisionTreeParameters
| Name | Type | Description |
|---|---|---|
| filename | String | file to load from |
Return
| Type | Description |
|---|---|
| DecisionTree | fitted tree, or Nil on failure |
LoadCsv # function
Loads a boolean input matrix from a CSV file of 1s and 0s
function : LoadCsv(filename:String) ~ Bool[,]Parameters
| Name | Type | Description |
|---|---|---|
| filename | String | CSV file to load |
Return
| Type | Description |
|---|---|
| Bool | boolean matrix |
Matches # function
Count matches in a column
function : Matches(index:Int, matrix:Bool[,]) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| index | Int | column index |
| matrix | Bool | matrix matrix to inspect |
Return
| Type | Description |
|---|---|
| Int | number of matches |
Mismatches # function
Count mismatches in a column
function : Mismatches(index:Int, matrix:Bool[,]) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| index | Int | column index |
| matrix | Bool | matrix matrix to inspect |
Return
| Type | Description |
|---|---|
| Int | number of mismatches |
New # constructor
Creates an (unfitted) decision tree classifier.
New(max_depth:Int, min_samples:Int)Parameters
| Name | Type | Description |
|---|---|---|
| max_depth | Int | maximum tree depth |
| min_samples | Int | minimum samples required to split a node |
Example
tree := DecisionTree->New(4, 1);NewInternal # function
Creates an internal split node.
function : NewInternal(feature:Int) ~ TreeNodeParameters
| Name | Type | Description |
|---|---|---|
| feature | Int | feature index to branch on |
Return
| Type | Description |
|---|---|
| TreeNode | internal node |
NewLeaf # function
Creates a leaf node.
function : NewLeaf(prediction:Bool) ~ TreeNodeParameters
| Name | Type | Description |
|---|---|---|
| prediction | Bool | class the leaf predicts |
Return
| Type | Description |
|---|---|
| TreeNode | leaf node |
Predict #
Predicts the class for a single feature row by walking the tree.
method : public : Predict(row:Bool[]) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| row | Bool | feature values (trailing label column, if present, is ignored) |
Return
| Type | Description |
|---|---|
| Bool | predicted class |
PredictAll #
Predicts classes for every row of an input matrix.
method : public : PredictAll(input:Bool[,]) ~ Bool[]Parameters
| Name | Type | Description |
|---|---|---|
| input | Bool | matrix of feature rows (a trailing label column is ignored) |
Return
| Type | Description |
|---|---|
| Bool | per-row predictions |
Query # native
Splits a matrix based on a list of decisions
function : native : Query(decisions:Vector<Split>, input:Bool[,]) ~ Bool[,]Parameters
| Name | Type | Description |
|---|---|---|
| decisions | Vector<Split> | list of decisions |
| input | Bool | matrix to be split |
Return
| Type | Description |
|---|---|
| Bool | split matrix |
ReadFrom # function
Reads a tree previously written with WriteTo from a deserializer.
function : ReadFrom(deserializer:System.IO.Deserializer) ~ DecisionTreeParameters
| Name | Type | Description |
|---|---|---|
| deserializer | Deserializer | source deserializer |
Return
| Type | Description |
|---|---|
| DecisionTree | fitted tree, or Nil on failure |
Score #
Computes classification accuracy on labeled data, comparing predictions against the class label in the LAST column.
method : public : Score(input:Bool[,]) ~ FloatParameters
| Name | Type | Description |
|---|---|---|
| input | Bool | labeled matrix (features..., label) of Bool |
Return
| Type | Description |
|---|---|
| Float | fraction of correctly classified rows, or 0.0 if not fitted |
Split # native
Splits a matrix along the given column
function : native : Split(index:Int, input:Bool[,]) ~ Bool[,]Parameters
| Name | Type | Description |
|---|---|---|
| index | Int | index used to split |
| input | Bool | matrix to split |
Return
| Type | Description |
|---|---|
| Bool | split matrix |
Store #
Saves the fitted tree to a file.
method : public : Store(filename:String) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| filename | String | file to store to |
Return
| Type | Description |
|---|---|
| Bool | true if successful, false otherwise |
Train # native
Calculates a list of decision splits
function : native : Train(input:Bool[,]) ~ Vector<Split>Parameters
| Name | Type | Description |
|---|---|---|
| input | Bool | training matrix |
Return
| Type | Description |
|---|---|
| Vector<Split> | list of decision tree splits |
WriteTo #
Serializes the tree (hyperparameters + nodes in pre-order) onto an existing serializer; used by Store and by ensembles that embed trees (RandomForest).
method : public : WriteTo(serializer:System.IO.Serializer) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| serializer | Serializer | destination serializer |