randomforest

package module
v0.0.0-...-24a7e0a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 22, 2017 License: MIT Imports: 11 Imported by: 0

README

Documentation

Overview

Example
data := [][]float64{
	{1, 1, 1},
	{2, 2, 2},
	{3, 3, 3},
	{4, 4, 4},
}
labels := []string{"ben", "cassie", "floki", "luna"}

rf := New(100)

// Define your own split evaluator
// Return a score of how good this split is
// Default is Gini
//
// rf.SplitEvaluator = func(t *Tree, left, right []int) float64 {
// 	return rand.Float64()
// }

rf.Fit(data, labels)

for idx, vals := range data {
	prediction := rf.Predict(vals)
	log.Printf("Wanted: %s Got: %s", labels[idx], prediction)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Gini

func Gini(t *Tree, left, right []int) float64

func Visualize

func Visualize(tree *Tree) error

Types

type Node

type Node struct {
	Tree *Tree

	FeatureIndex int
	Value        float64

	IdxCnt int

	Left  *Node
	Right *Node

	Label int
}

func NewNode

func NewNode(tree *Tree, indicies []int, level int) *Node

func (*Node) Traverse

func (n *Node) Traverse(data []float64) int

type RandomForest

type RandomForest struct {
	TreeCount    int            // Number of trees to include in forest
	MaxDepth     int            //
	MinSize      int            //
	FeatureCount int            // Number of features to include in each tree
	Trees        []*Tree        // List of trees created in `Fit`
	Evaluator    SplitEvaluator // Function to evaluate each split
	SampleSize   float64        // Ratio of rows to consider in each tree

	Data   [][]float64
	Labels []string
}

func Load

func Load(r io.Reader) (*RandomForest, error)

func New

func New(treeCnt int) *RandomForest

func (*RandomForest) Dump

func (r *RandomForest) Dump(w io.Writer) error

func (*RandomForest) Fit

func (r *RandomForest) Fit(data [][]float64, labels []string) error

func (*RandomForest) Predict

func (r *RandomForest) Predict(data []float64) string

func (*RandomForest) PredictProbability

func (r *RandomForest) PredictProbability(data []float64) map[string]float64

func (*RandomForest) Sample

func (r *RandomForest) Sample() ([][]float64, []string)

type SplitEvaluator

type SplitEvaluator func(*Tree, []int, []int) float64

type Tree

type Tree struct {
	Forest       *RandomForest
	Root         *Node
	Data         [][]float64
	Labels       []int
	UniqueCounts []float64

	MaxDepth int
	// contains filtered or unexported fields
}

func NewTree

func NewTree(rf *RandomForest, data [][]float64, labels []string) *Tree

func (*Tree) Predict

func (t *Tree) Predict(data []float64) string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL