forked from ardanlabs/gotraining
/
example2.go
100 lines (83 loc) · 2.38 KB
/
example2.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
92
93
94
95
96
97
98
99
100
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// go build
// ./example2
// Sample program to visualize the impact of dimensionality reduction.
package main
import (
"image/color"
"log"
"os"
"github.com/gonum/floats"
"github.com/gonum/matrix/mat64"
"github.com/gonum/plot"
"github.com/gonum/plot/plotter"
"github.com/gonum/plot/plotutil"
"github.com/gonum/plot/vg"
"github.com/gonum/stat"
"github.com/kniren/gota/dataframe"
)
func main() {
// Open the iris dataset file.
f, err := os.Open("../data/iris.csv")
if err != nil {
log.Fatal(err)
}
defer f.Close()
// Parse the CSV file into a dataframe.
irisDF := dataframe.ReadCSV(f)
// Form the matrix.
mat := irisDF.Select([]string{"sepal_length", "sepal_width", "petal_length", "petal_width"}).Matrix()
// Calculate the principal component direction vectors
// and variances.
var pc stat.PC
ok := pc.PrincipalComponents(mat, nil)
if !ok {
log.Fatal("Could not calculate principal components")
}
// Get the prinipal components and the corresponding vectors.
var vars []float64
var vecs *mat64.Dense
vars = pc.Vars(vars)
vecs = pc.Vectors(vecs)
// Sum the eigenvalues (variances).
total := floats.Sum(vars)
// Calculate cumulative variance percentages for each sorted value.
cumVar := make(plotter.Values, 4)
var cumSum float64
for idx, variance := range vars {
cumSum += (variance / total) * 100.0
cumVar[idx] = cumSum
}
// Create a bar plot to visualize the variance percentages.
p, err := plot.New()
if err != nil {
log.Fatal(err)
}
p.X.Label.Text = "Principal components"
p.Y.Label.Text = "Percent of variance captured"
p.Y.Max = 110.0
p.X.Max = 3.1
p.X.Min = -0.1
w := vg.Points(20)
// Create the bars for the percent values.
bars, err := plotter.NewBarChart(cumVar, w)
if err != nil {
log.Fatal(err)
}
bars.LineStyle.Width = vg.Length(0)
bars.Color = plotutil.Color(0)
// Format the bars.
p.Add(bars)
p.NominalX("One", "Two", "Three", "Four")
// Plot a line at 100% for easy inspection.
hundred := plotter.NewFunction(func(x float64) float64 { return 100.0 })
hundred.Color = color.RGBA{B: 255, A: 255}
hundred.Dashes = []vg.Length{vg.Points(2), vg.Points(2)}
hundred.Width = vg.Points(2)
p.Add(hundred)
// Save the graph.
if err := p.Save(4*vg.Inch, 5*vg.Inch, "barchart.png"); err != nil {
log.Fatal(err)
}
}