1
0
mirror of https://github.com/sjwhitworth/golearn.git synced 2025-04-28 13:48:56 +08:00
golearn/ensemble/randomforest.go

61 lines
1.6 KiB
Go
Raw Normal View History

package ensemble
import (
"fmt"
2014-08-22 07:21:24 +00:00
"github.com/sjwhitworth/golearn/base"
"github.com/sjwhitworth/golearn/meta"
"github.com/sjwhitworth/golearn/trees"
)
// RandomForest classifies instances using an ensemble
2014-08-02 16:22:14 +01:00
// of bagged random decision trees.
type RandomForest struct {
base.BaseClassifier
ForestSize int
Features int
Model *meta.BaggedModel
}
// NewRandomForest generates and return a new random forests
// forestSize controls the number of trees that get built
2014-08-02 16:22:14 +01:00
// features controls the number of features used to build each tree.
func NewRandomForest(forestSize int, features int) *RandomForest {
ret := &RandomForest{
base.BaseClassifier{},
forestSize,
features,
nil,
}
return ret
}
// Fit builds the RandomForest on the specified instances
2014-08-02 16:22:14 +01:00
func (f *RandomForest) Fit(on base.FixedDataGrid) {
numNonClassAttributes := len(base.NonClassAttributes(on))
if numNonClassAttributes < f.Features {
panic(fmt.Sprintf(
"Random forest with %d features cannot fit data grid with %d non-class attributes",
f.Features,
numNonClassAttributes,
))
}
f.Model = new(meta.BaggedModel)
f.Model.RandomFeatures = f.Features
for i := 0; i < f.ForestSize; i++ {
tree := trees.NewID3DecisionTree(0.00)
2014-05-17 16:20:56 +01:00
f.Model.AddModel(tree)
}
2014-05-17 17:35:10 +01:00
f.Model.Fit(on)
}
2014-08-02 16:22:14 +01:00
// Predict generates predictions from a trained RandomForest.
func (f *RandomForest) Predict(with base.FixedDataGrid) base.FixedDataGrid {
return f.Model.Predict(with)
}
2014-08-02 16:22:14 +01:00
// String returns a human-readable representation of this tree.
func (f *RandomForest) String() string {
return fmt.Sprintf("RandomForest(ForestSize: %d, Features:%d, %s\n)", f.ForestSize, f.Features, f.Model)
}