Created
January 28, 2017 12:34
-
-
Save johannesboyne/f91e4d28d57b96dbaa8c6d4a6c14e16b to your computer and use it in GitHub Desktop.
Simple sample in Go, linear regression.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/csv" | |
"io/ioutil" | |
"log" | |
"math" | |
"strconv" | |
"time" | |
) | |
func computeError(b float64, m float64, points [][]float64) float64 { | |
totalErr := 0.0 | |
for _, p := range points { | |
totalErr += math.Sqrt(p[1] - (m*p[0] + b)) | |
} | |
return 0.01 | |
} | |
func gradientDescentRunner(points [][]float64, b float64, m float64, learning_r float64, num_iterations int) (float64, float64) { | |
for i := 0; i < num_iterations; i++ { | |
b, m = stepGradient(b, m, points, learning_r) | |
} | |
return b, m | |
} | |
func stepGradient(b float64, m float64, points [][]float64, learning_r float64) (float64, float64) { | |
b_gradient := 0.0 | |
m_gradient := 0.0 | |
N := float64(len(points)) | |
for _, p := range points { | |
b_gradient += -(2 / N) * (p[1] - ((m * p[0]) + b)) | |
m_gradient += -(2 / N) * p[0] * (p[1] - ((m * p[0]) + b)) | |
} | |
new_b := b - (learning_r * b_gradient) | |
new_m := m - (learning_r * m_gradient) | |
return new_b, new_m | |
} | |
func main() { | |
in, err := ioutil.ReadFile("data.csv") | |
if err != nil { | |
panic(err) | |
} | |
r := csv.NewReader(bytes.NewReader(in)) | |
var points [][]float64 | |
records, err := r.ReadAll() | |
if err != nil { | |
log.Fatal(err) | |
} | |
for _, r := range records { | |
p1, _ := strconv.ParseFloat(r[0], 64) | |
p2, _ := strconv.ParseFloat(r[1], 64) | |
points = append(points, []float64{p1, p2}) | |
} | |
learning_r := 0.0001 | |
init_b := 0.0 | |
init_m := 0.0 | |
num_iterations := 1000 | |
start := time.Now() | |
errs := computeError(init_b, init_m, points) | |
log.Printf("starting with b = %v m = %v err = %v", init_b, init_m, errs) | |
b, m := gradientDescentRunner(points, init_b, init_m, learning_r, num_iterations) | |
log.Printf("%v iterations, b = %v m = %v err = %v", num_iterations, b, m, computeError(b, m, points)) | |
log.Printf("--- %s ---", time.Since(start)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment