forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example1.go
52 lines (41 loc) · 1.34 KB
/
example1.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
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// go build
// ./example1
// Sample program to illustrate the calculation of principal components.
package main
import (
"bytes"
"fmt"
"log"
"github.com/gonum/stat"
"github.com/kniren/gota/dataframe"
"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 Iris dataset from Pachyderm's data
// versioning at the latest commit.
var b bytes.Buffer
if err := c.GetFile("iris", "master", "iris.csv", 0, 0, "", false, nil, &b); err != nil {
log.Fatal()
}
// Parse the file into a Gota dataframe.
irisDF := dataframe.ReadCSV(bytes.NewReader(b.Bytes()))
// Form a matrix from the dataframe.
mat := irisDF.Select([]string{"sepal_length", "sepal_width", "petal_length", "petal_width"}).Matrix()
// Calculate the principal component direction vectors
// and variances.
_, vars, ok := stat.PrincipalComponents(mat, nil)
if !ok {
log.Fatal("Could not calculate principal components")
}
// Output the principal component direction variances to standard out.
fmt.Printf("\nvariances = %.4f\n\n", vars)
}