Skip to content

Commit

Permalink
Merge pull request #2 from timkaye11/master
Browse files Browse the repository at this point in the history
added some comments on readme & cars example
  • Loading branch information
fxsjy committed Jul 2, 2014
2 parents 15cce47 + b63cd6d commit fc68bab
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 43 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ RF.go
* RF.go supports both Classification and Regression. The examples can be found in the repository.

* RF.go supports dumpping and loading the forest data structure between RAM and disk, in a JSON format file

### Installation
1. [Install Go](http://www.golang.org)
2. ```$ go get github.com/fxsjy/RF.go/RF ``` This will put the binary in ```$GOROOT/bin```
83 changes: 40 additions & 43 deletions example_iris/test_iris.go
Original file line number Diff line number Diff line change
@@ -1,87 +1,84 @@
package main

import (
"../RF"
"fmt"
"io/ioutil"
"strings"
"strconv"
"os"

"strconv"
"strings"
"time"

//"math"
"../RF"
)


func main(){
func main() {

start := time.Now()
f,_ := os.Open("iris2.data")
f, _ := os.Open("iris2.data")
defer f.Close()
content,_ := ioutil.ReadAll(f)
content, _ := ioutil.ReadAll(f)
s_content := string(content)
lines := strings.Split(s_content,"\n")

inputs := make([][]interface{},0)
targets := make([]string,0)
for _,line := range lines{
lines := strings.Split(s_content, "\n")

line = strings.TrimRight(line,"\r\n")
// set up variables for random forest
inputs := make([][]interface{}, 0)
targets := make([]string, 0)
for _, line := range lines {

if len(line)==0{
line = strings.TrimRight(line, "\r\n")

if len(line) == 0 {
continue
}
tup := strings.Split(line,",")
tup := strings.Split(line, ",")
pattern := tup[:len(tup)-1]
target := tup[len(tup)-1]
X := make([]interface{},0)
for _,x := range pattern{
f_x,_:= strconv.ParseFloat(x,64)
X = append(X,f_x)
X := make([]interface{}, 0)
for _, x := range pattern {
f_x, _ := strconv.ParseFloat(x, 64)
X = append(X, f_x)
}
inputs = append(inputs,X)
targets = append(targets,target)
inputs = append(inputs, X)

targets = append(targets, target)
}
train_inputs := make([][]interface{},0)
train_targets := make([]string,0)
train_inputs := make([][]interface{}, 0)
train_targets := make([]string, 0)

test_inputs := make([][]interface{},0)
test_targets := make([]string,0)
test_inputs := make([][]interface{}, 0)
test_targets := make([]string, 0)

for i,x := range inputs{
if i%3==0{
for i, x := range inputs {
if i%3 == 0 {
test_inputs = append(test_inputs, x)
}else{
} else {
train_inputs = append(train_inputs, x)
}
}

for i,y := range targets{
if i%3==0{
test_targets = append(test_targets,y)
}else{
train_targets = append(train_targets,y)
for i, y := range targets {
if i%3 == 0 {
test_targets = append(test_targets, y)
} else {
train_targets = append(train_targets, y)
}
}

forest := RF.DefaultForest(inputs,targets,100)//100 trees
forest := RF.DefaultForest(inputs, targets, 100) //100 trees

RF.DumpForest(forest,"rf.bin")
RF.DumpForest(forest, "rf.bin")

forest = RF.LoadForest("rf.bin")

err_count := 0.0
for i:=0;i<len(test_inputs);i++{
for i := 0; i < len(test_inputs); i++ {
output := forest.Predicate(test_inputs[i])
expect := test_targets[i]
fmt.Println(output,expect)
if output!=expect{
fmt.Println(output, expect)
if output != expect {
err_count += 1
}
}
fmt.Println("success rate:",1.0 - err_count/float64(len(test_inputs)))
fmt.Println("success rate:", 1.0-err_count/float64(len(test_inputs)))

fmt.Println(time.Since(start))

Expand Down

0 comments on commit fc68bab

Please sign in to comment.