forked from pa-m/sklearn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline_test.go
61 lines (47 loc) · 1.44 KB
/
pipeline_test.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
package pipeline
import (
"fmt"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/mat"
"github.com/etrace-io/sklearn/base"
"github.com/etrace-io/sklearn/datasets"
nn "github.com/etrace-io/sklearn/neural_network"
"github.com/etrace-io/sklearn/preprocessing"
)
func ExamplePipeline() {
randomState := rand.New(base.NewLockedSource(7))
ds := datasets.LoadBreastCancer()
scaler := preprocessing.NewStandardScaler()
pca := preprocessing.NewPCA()
pca.MinVarianceRatio = 0.995
poly := preprocessing.NewPolynomialFeatures(2)
poly.IncludeBias = false
m := nn.NewMLPClassifier([]int{}, "relu", "adam", 0)
m.RandomState = randomState
m.MaxIter = 300
m.LearningRateInit = .02
m.WeightDecay = .001
pl := MakePipeline(scaler, pca, poly, m)
// or equivalent:
pl = NewPipeline(NamedStep{"scaler", scaler}, NamedStep{"pca", pca}, NamedStep{"poly", poly}, NamedStep{"mlp", m})
// pipeline is clonable
pl = pl.PredicterClone().(*Pipeline)
// pipeline is classifier if last step is a classifier
if !pl.IsClassifier() {
fmt.Println("shouldn't happen")
}
pl.Fit(ds.X, ds.Y)
nSamples, _ := ds.X.Dims()
_, nOutputs := ds.Y.Dims()
Ypred := mat.NewDense(nSamples, nOutputs, nil)
pl.Predict(ds.X, Ypred)
accuracy := pl.Score(ds.X, ds.Y)
fmt.Println("accuracy>0.999 ?", accuracy > 0.999)
if accuracy <= .999 {
fmt.Println("accuracy:", accuracy)
}
// pipeline is a Transformer too
_, _ = pl.FitTransform(ds.X, ds.Y)
// Output:
// accuracy>0.999 ? true
}