-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogistic_regression.go
48 lines (41 loc) · 1.05 KB
/
logistic_regression.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
package main
import (
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
"image/color"
"log"
"math"
)
func main() {
// Create a new plot.
p, err := plot.New()
if err != nil {
log.Fatal(err)
}
p.Title.Text = "Logistic Function"
p.X.Label.Text = "x"
p.Y.Label.Text = "f(x)"
// Create the plotter function.
logisticPlotter := plotter.NewFunction(func(x float64) float64 { return logistic(x) })
logisticPlotter.Color = color.RGBA{B: 255, A: 255}
// Add the plotter function to the plot.
p.Add(logisticPlotter)
// Set the axis ranges. Unlike other data sets,
// functions don't set the axis ranges automatically
// since functions don't necessarily have a
// finite range of x and y values.
p.X.Min = -10
p.X.Max = 10
p.Y.Min = -0.1
p.Y.Max = 1.1
// Save the plot to a PNG file.
if err := p.Save(4*vg.Inch, 4*vg.Inch, "logistic.png"); err != nil {
log.Fatal(err)
}
}
// logistic implements the logistic function, which
// is used in logistic regression.
func logistic(x float64) float64 {
return 1 / (1 + math.Exp(-x))
}