Skip to content

GalaktaGlareDT

Wesley Yan Soares Brehmer edited this page Jun 11, 2024 · 5 revisions

GalaktaGlareDT Documentation

Introduction

GalaktaGlareDT is a decision tree implementation package in Go designed for machine learning tasks. Decision trees are a popular method for both classification and regression tasks due to their simplicity and interpretability.

Installation

To use GalaktaGlareDT in your Go project, you can install it using go get:

go get github.com/simplyYan/GalaktaGlare/src/GalaktaGlareDT

Usage

Import the package into your Go code:

import "github.com/simplyYan/GalaktaGlare/GalaktaGlareDT"

Types

Node:

  • IsLeaf bool: Indicates whether the node is a leaf node.
  • Prediction interface{}: Predicted value if the node is a leaf.
  • SplitFeature int: Index of the feature used for splitting at this node.
  • SplitValue interface{}: Value of the feature used for splitting at this node.
  • Left *Node: Left child node.
  • Right *Node: Right child node.

DecisionTree:

  • Root *Node: Root node of the decision tree.

Functions

*NewDecisionTree() DecisionTree:

  • Creates a new instance of DecisionTree.

Fit(data [][]interface{}, labels []interface{}, features []int, maxDepth int) error:

  • Fits the decision tree to the given data.
  • Parameters:
    • data [][]interface{}: Training data.
    • labels []interface{}: Labels corresponding to the training data.
    • features []int: Indices of features to consider for splitting.
    • maxDepth int: Maximum depth of the decision tree.
  • Returns:
    • error: Error if data or labels are invalid.

Predict(input []interface{}) (interface{}, error):

  • Predicts the label for a given input.
  • Parameters:
    • input []interface{}: Input data to predict.
  • Returns:
    • interface{}: Predicted label.
    • error: Error if prediction fails.

Examples

package main

import (
	"fmt"
	"github.com/your-username/GalaktaGlareDT"
)

func main() {
	// Create a new decision tree
	dt := GalaktaGlareDT.NewDecisionTree()

	// Sample data
	data := [][]interface{}{
		{6.0, "Male"},
		{5.7, "Female"},
		{5.5, "Female"},
		{5.8, "Male"},
		{6.1, "Male"},
	}
	labels := []interface{}{"Tall", "Short", "Short", "Tall", "Tall"}
	features := []int{0} // Consider only height for splitting
	maxDepth := 2

	// Fit the decision tree
	err := dt.Fit(data, labels, features, maxDepth)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	// Predict label for new input
	input := []interface{}{5.9}
	prediction, err := dt.Predict(input)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	fmt.Println("Prediction:", prediction)
}

Explanation

  • Fit: This method builds the decision tree recursively using the provided training data, labels, features, and maximum depth. It stops recursion when it reaches the maximum depth or when there is no more data to split.

  • Predict: This method traverses the decision tree based on the input features until it reaches a leaf node, and then returns the prediction.

Conclusion

GalaktaGlareDT provides a simple and efficient implementation of decision trees in Go, suitable for classification tasks. With its easy-to-use interface and flexibility in defining features and maximum depth, it can be seamlessly integrated into your machine learning projects.