|
| 1 | +package linearregression |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "image/color" |
| 6 | + "log" |
| 7 | + "math" |
| 8 | + |
| 9 | + "gonum.org/v1/plot" |
| 10 | + "gonum.org/v1/plot/plotter" |
| 11 | + "gonum.org/v1/plot/vg" |
| 12 | +) |
| 13 | + |
| 14 | +// RegressionData for collecting some data that needed in linear regression calculation |
| 15 | +type RegressionData struct { |
| 16 | + XValues []float64 |
| 17 | + YValues []float64 |
| 18 | + Epoch int |
| 19 | + LearningRate float64 |
| 20 | + slope float64 |
| 21 | + intercept float64 |
| 22 | +} |
| 23 | + |
| 24 | +// New ... |
| 25 | +func New() *RegressionData { |
| 26 | + rd := &RegressionData{} |
| 27 | + return rd |
| 28 | +} |
| 29 | + |
| 30 | +// PlotTheDataset will make the scatter plot based on x and y values |
| 31 | +func (d *RegressionData) PlotTheDataset() { |
| 32 | + d.plotMaker(false) |
| 33 | +} |
| 34 | + |
| 35 | +// Calculate to calculate the linear regression |
| 36 | +func (d *RegressionData) Calculate(isWriteResultToPlot bool) (float64, float64) { |
| 37 | + if len(d.XValues) == 0 || len(d.YValues) == 0 { |
| 38 | + panic("x values and y values cannot be empty") |
| 39 | + } |
| 40 | + for i := 0; i < d.Epoch; i++ { |
| 41 | + d.slope, d.intercept = d.gradientDescent(d.slope, d.intercept) |
| 42 | + } |
| 43 | + //round float to 2 decimal |
| 44 | + d.slope = math.Round(d.slope*100) / 100 |
| 45 | + d.intercept = math.Round(d.intercept*100) / 100 |
| 46 | + if isWriteResultToPlot { |
| 47 | + d.plotMaker(true) |
| 48 | + } |
| 49 | + return d.slope, d.intercept |
| 50 | +} |
| 51 | + |
| 52 | +func (d *RegressionData) gradientDescent(mCurrent float64, bCurrent float64) (finalM float64, finalB float64) { |
| 53 | + var mGradient float64 |
| 54 | + var bGradient float64 |
| 55 | + nData := len(d.XValues) //the x or y values length |
| 56 | + twoPerN := float64(2) / float64(nData) |
| 57 | + for i := 0; i < nData; i++ { |
| 58 | + mGradient += -twoPerN * d.XValues[i] * (d.YValues[i] - ((mCurrent * d.XValues[i]) + bCurrent)) |
| 59 | + bGradient += -twoPerN * (d.YValues[i] - ((mCurrent * d.XValues[i]) + bCurrent)) |
| 60 | + } |
| 61 | + finalM = mCurrent - (d.LearningRate * mGradient) |
| 62 | + finalB = bCurrent - (d.LearningRate * bGradient) |
| 63 | + return |
| 64 | +} |
| 65 | + |
| 66 | +func (d *RegressionData) plotMaker(isWriteResultToPlot bool) { |
| 67 | + file := "" |
| 68 | + p, err := plot.New() |
| 69 | + if err != nil { |
| 70 | + panic(err) |
| 71 | + } |
| 72 | + p.Title.Text = "Linear Regression using Go" |
| 73 | + p.X.Min = 0 |
| 74 | + p.X.Max = 6 |
| 75 | + p.X.Label.Text = "X" |
| 76 | + p.Y.Min = 0 |
| 77 | + p.Y.Max = 6 |
| 78 | + p.Y.Label.Text = "Y" |
| 79 | + p.Add(plotter.NewGrid()) |
| 80 | + |
| 81 | + dataset := make(plotter.XYs, len(d.XValues)) |
| 82 | + for i := range dataset { |
| 83 | + dataset[i].X = d.XValues[i] |
| 84 | + dataset[i].Y = d.YValues[i] |
| 85 | + } |
| 86 | + s, err := plotter.NewScatter(dataset) |
| 87 | + if err != nil { |
| 88 | + log.Panic(err) |
| 89 | + } |
| 90 | + s.GlyphStyle.Color = color.RGBA{R: 255, B: 128, A: 255} |
| 91 | + s.GlyphStyle.Radius = vg.Points(3) |
| 92 | + |
| 93 | + if isWriteResultToPlot { |
| 94 | + lineData := make(plotter.XYs, 7) |
| 95 | + for i := 0; i < 7; i++ { |
| 96 | + x := float64(i) |
| 97 | + lineData[i].X = x |
| 98 | + lineData[i].Y = d.slope*x + d.intercept |
| 99 | + } |
| 100 | + l, err := plotter.NewLine(lineData) |
| 101 | + if err != nil { |
| 102 | + log.Panic(err) |
| 103 | + } |
| 104 | + l.LineStyle.Width = vg.Points(1) |
| 105 | + l.LineStyle.Color = color.RGBA{B: 255, A: 255} |
| 106 | + |
| 107 | + p.Add(s, l) |
| 108 | + p.Legend.Add("dataset", s) |
| 109 | + |
| 110 | + p.Legend.Add(fmt.Sprintf("y(x) = %.2fx + %.2f", d.slope, d.intercept), l) |
| 111 | + file = "plot/result.png" |
| 112 | + } else { |
| 113 | + p.Add(s) |
| 114 | + p.Legend.Add("dataset", s) |
| 115 | + file = "plot/scatter.png" |
| 116 | + } |
| 117 | + |
| 118 | + err = p.Save(300, 300, file) |
| 119 | + if err != nil { |
| 120 | + log.Panic(err) |
| 121 | + } |
| 122 | +} |
0 commit comments