Skip to content

Instantly share code, notes, and snippets.

@owulveryck
Last active August 1, 2021 17:06
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save owulveryck/5c4044bb7ef1896142134c1c640570e5 to your computer and use it in GitHub Desktop.
Save owulveryck/5c4044bb7ef1896142134c1c640570e5 to your computer and use it in GitHub Desktop.
Simple linear regression with plot in go with gonum

Very simple linear regression

curl -s //www.lauradhamilton.com/data_files/cricket_chirps_versus_temperature.txt | go run main.go > output.svg
2018/06/13 19:57:59 3.3057614388773593*x+24.966014428303183
package main
import (
"encoding/csv"
"log"
"os"
"strconv"
"gonum.org/v1/gonum/stat"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
)
type xy struct {
x []float64
y []float64
}
func (d xy) Len() int {
return len(d.x)
}
func (d xy) XY(i int) (x, y float64) {
x = d.x[i]
y = d.y[i]
return
}
func main() {
r := csv.NewReader(os.Stdin)
records, err := r.ReadAll()
if err != nil {
log.Fatal(err)
}
size := len(records)
data := xy{
x: make([]float64, size),
y: make([]float64, size),
}
for i, v := range records {
if len(v) != 2 {
log.Fatal("Expected two elements")
}
if s, err := strconv.ParseFloat(v[0], 64); err == nil {
data.y[i] = s
}
if s, err := strconv.ParseFloat(v[1], 64); err == nil {
data.x[i] = s
}
}
b, a := stat.LinearRegression(data.x, data.y, nil, false)
log.Printf("%v*x+%v", a, b)
line := plotter.NewFunction(func(x float64) float64 { return a*x + b })
p, err := plot.New()
if err != nil {
log.Panic(err)
}
plotter.DefaultLineStyle.Width = vg.Points(1)
plotter.DefaultGlyphStyle.Radius = vg.Points(2)
scatter, err := plotter.NewScatter(data)
if err != nil {
log.Panic(err)
}
p.Add(scatter, line)
w, err := p.WriterTo(300, 300, "svg")
if err != nil {
log.Panic(err)
}
_, err = w.WriteTo(os.Stdout)
if err != nil {
log.Panic(err)
}
}
Display the source blob
Display the rendered blob
Raw
<?xml version="1.0"?>
<!-- Generated by SVGo and Plotinum VG -->
<svg width="4.1667in" height="4.1667in"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<g transform="scale(1, -1) translate(0, -375)">
<path d="M0,0L375,0L375,375L0,375Z" style="fill:#FFFFFF" />
<text x="56.138" y="-0.23193" transform="scale(1, -1)"
style="font-family:Times;font-weight:normal;font-style:normal;font-size:10pt">15.00</text>
<text x="201.51" y="-0.23193" transform="scale(1, -1)"
style="font-family:Times;font-weight:normal;font-style:normal;font-size:10pt">17.50</text>
<text x="346.88" y="-0.23193" transform="scale(1, -1)"
style="font-family:Times;font-weight:normal;font-style:normal;font-size:10pt">20.00</text>
<path d="M70.201,12.268L70.201,22.268" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M215.57,12.268L215.57,22.268" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M360.94,12.268L360.94,22.268" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M41.127,17.268L41.127,22.268" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M99.275,17.268L99.275,22.268" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M128.35,17.268L128.35,22.268" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M157.42,17.268L157.42,22.268" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M186.5,17.268L186.5,22.268" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M244.64,17.268L244.64,22.268" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M273.72,17.268L273.72,22.268" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M302.79,17.268L302.79,22.268" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M331.86,17.268L331.86,22.268" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M35.312,22.268L360.94,22.268" style="fill:none;stroke:#000000;stroke-width:0.625" />
<text x="0" y="-33.993" transform="scale(1, -1)"
style="font-family:Times;font-weight:normal;font-style:normal;font-size:10pt">70</text>
<text x="0" y="-176.74" transform="scale(1, -1)"
style="font-family:Times;font-weight:normal;font-style:normal;font-size:10pt">80</text>
<text x="0" y="-319.49" transform="scale(1, -1)"
style="font-family:Times;font-weight:normal;font-style:normal;font-size:10pt">90</text>
<path d="M15.625,39.895L25.625,39.895" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M15.625,182.64L25.625,182.64" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M15.625,325.39L25.625,325.39" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M20.625,68.445L25.625,68.445" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M20.625,96.995L25.625,96.995" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M20.625,125.54L25.625,125.54" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M20.625,154.09L25.625,154.09" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M20.625,211.19L25.625,211.19" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M20.625,239.74L25.625,239.74" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M20.625,268.29L25.625,268.29" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M20.625,296.84L25.625,296.84" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M20.625,353.94L25.625,353.94" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M25.625,31.331L25.625,372.5" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M363.44,305.41A2.5,2.5 0 1 1 358.44,305.41A2.5,2.5 0 1 1 363.44,305.41Z" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M130.85,62.735A2.5,2.5 0 1 1 125.85,62.735A2.5,2.5 0 1 1 130.85,62.735Z" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M351.81,372.5L351.81,372.5A2.5,2.5 0 1 1 346.81,372.5A2.5,2.5 0 1 1 351.81,372.5Z" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M270.4,244.03A2.5,2.5 0 1 1 265.4,244.03A2.5,2.5 0 1 1 270.4,244.03Z" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M194.81,191.21L194.81,191.21A2.5,2.5 0 1 1 189.81,191.21A2.5,2.5 0 1 1 194.81,191.21Z" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M101.77,114.12L101.77,114.12A2.5,2.5 0 1 1 96.775,114.12A2.5,2.5 0 1 1 101.77,114.12Z" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M55.257,35.613A2.5,2.5 0 1 1 50.257,35.613A2.5,2.5 0 1 1 55.257,35.613Z" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M194.81,211.19L194.81,211.19A2.5,2.5 0 1 1 189.81,211.19A2.5,2.5 0 1 1 194.81,211.19Z" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M95.96,31.331L95.96,31.331A2.5,2.5 0 1 1 90.96,31.331A2.5,2.5 0 1 1 95.96,31.331Z" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M148.29,229.75L148.29,229.75A2.5,2.5 0 1 1 143.29,229.75A2.5,2.5 0 1 1 148.29,229.75Z" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M72.701,176.93L72.701,176.93A2.5,2.5 0 1 1 67.701,176.93A2.5,2.5 0 1 1 72.701,176.93Z" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M200.62,219.76L200.62,219.76A2.5,2.5 0 1 1 195.62,219.76A2.5,2.5 0 1 1 200.62,219.76Z" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M130.85,191.21A2.5,2.5 0 1 1 125.85,191.21A2.5,2.5 0 1 1 130.85,191.21Z" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M189,232.61L189,232.61A2.5,2.5 0 1 1 184,232.61A2.5,2.5 0 1 1 189,232.61Z" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M37.812,129.83A2.5,2.5 0 1 1 32.812,129.83A2.5,2.5 0 1 1 37.812,129.83Z" style="fill:none;stroke:#000000;stroke-width:0.625" />
<path d="M35.312,76.567L41.958,81.96L48.603,87.353L55.249,92.747L61.894,98.14L68.54,103.53L75.185,108.93L81.83,114.32L88.476,119.71L95.121,125.1L101.77,130.5L108.41,135.89L115.06,141.28L121.7,146.68L128.35,152.07L134.99,157.46L141.64,162.86L148.28,168.25L154.93,173.64L161.58,179.04L168.22,184.43L174.87,189.82L181.51,195.21L188.16,200.61L194.8,206L201.45,211.39L208.09,216.79L214.74,222.18L221.38,227.57L228.03,232.97L234.67,238.36L241.32,243.75L247.97,249.15L254.61,254.54L261.26,259.93L267.9,265.32L274.55,270.72L281.19,276.11L287.84,281.5L294.48,286.9L301.13,292.29L307.77,297.68L314.42,303.08L321.07,308.47L327.71,313.86L334.36,319.26L341,324.65L347.65,330.04L354.29,335.43L360.94,340.83" style="fill:none;stroke:#000000;stroke-width:1.25" />
</g>
</svg>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment