forked from ardanlabs/gotraining
/
exercise1c.go
91 lines (73 loc) · 2.09 KB
/
exercise1c.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// go build
// ./exercise1c
// Sample program to validate a trained multiple regression model on a holdout data set.
package main
import (
"bytes"
"encoding/csv"
"fmt"
"log"
"math"
"strconv"
"github.com/pachyderm/pachyderm/src/client"
)
func main() {
// Connect to Pachyderm on our localhost. By default
// Pachyderm will be exposed on port 30650.
c, err := client.NewFromAddress("0.0.0.0:30650")
if err != nil {
log.Fatal(err)
}
defer c.Close()
// Get the holdout dataset from Pachyderm's data
// versioning at the latest commit.
var b bytes.Buffer
if err := c.GetFile("regression_split", "master", "holdout.csv", 0, 0, "", false, nil, &b); err != nil {
log.Fatal()
}
// Create a new CSV reader reading from the opened file.
reader := csv.NewReader(bytes.NewReader(b.Bytes()))
// Read in all of the CSV records
reader.FieldsPerRecord = 11
holdoutData, err := reader.ReadAll()
if err != nil {
log.Fatal(err)
}
// Loop over the holdout data predicting y and evaluating the prediction
// with the mean absolute error.
var mAE float64
for i, record := range holdoutData {
// Skip the header.
if i == 0 {
continue
}
// Parse the observed diabetes progression measure, or "y".
yObserved, err := strconv.ParseFloat(record[10], 64)
if err != nil {
log.Fatal(err)
}
// Parse the bmi value.
bmiVal, err := strconv.ParseFloat(record[2], 64)
if err != nil {
log.Fatal(err)
}
// Parse the ltg value.
ltgVal, err := strconv.ParseFloat(record[8], 64)
if err != nil {
log.Fatal(err)
}
// Predict y with our trained model.
yPredicted := predict(bmiVal, ltgVal)
// Add the to the mean absolute error.
mAE += math.Abs(yObserved-yPredicted) / float64(len(holdoutData))
}
// Output the MAE to standard out.
fmt.Printf("\nMAE = %0.2f\n\n", mAE)
}
// predict uses our trained regression model to made a prediction based on a
// bmi and ltg value.
func predict(bmi, ltg float64) float64 {
return 151.50 + bmi*623.59 + ltg*644.50
}