-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain.go
More file actions
57 lines (47 loc) · 1.05 KB
/
train.go
File metadata and controls
57 lines (47 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"fmt"
"strconv"
"github.com/sjwhitworth/golearn/base"
. "github.com/sjwhitworth/golearn/linear_models"
)
func errexit(err error) {
if err != nil {
panic(err)
}
}
func main() {
trainData1, err := base.ParseCSVToInstances("datatrain1.csv", true)
errexit(err)
testData, err := base.ParseCSVToInstances("data1.csv", true)
errexit(err)
lr := NewLinearRegression()
err1 := lr.Fit(trainData1)
errexit(err1)
predictions, err2 := lr.Predict(testData)
errexit(err2)
_, rows := predictions.Size()
total := 0.0
m := 0.0
//n := 0.0
for i := 0; i < rows; i++ {
actualValue, _ := strconv.ParseFloat(base.GetClass(testData, i), 64)
expectedValue, _ := strconv.ParseFloat(base.GetClass(predictions, i), 64)
if expectedValue <= 0 && actualValue == 0 {
continue
}
if expectedValue < 0 {
expectedValue = 0
}
if expectedValue > 20 || actualValue > 20 {
d := expectedValue / actualValue
if d > 1 {
d = 1 / d
}
total += d
m++
fmt.Println(expectedValue, actualValue)
}
}
fmt.Println(total / m)
}