Skip to content

Instantly share code, notes, and snippets.

@ahmadyogi543
Created February 6, 2024 08:03
Show Gist options
  • Save ahmadyogi543/8b6482610da5b96ca0f1e37d31cde367 to your computer and use it in GitHub Desktop.
Save ahmadyogi543/8b6482610da5b96ca0f1e37d31cde367 to your computer and use it in GitHub Desktop.
Linear Regression Implementation
package main
import (
"fmt"
"math/rand"
)
func sum(slice []float64) float64 {
sum := 0.0
for _, v := range slice {
sum += v
}
return sum
}
func mean(slice []float64) float64 {
return sum(slice) / float64(len(slice))
}
func square_trick(slope, intercept, feature, label, rate float64) (float64, float64) {
predicted_label := slope*feature + intercept
slope += rate * feature * (label - predicted_label)
intercept += rate * (label - predicted_label)
return slope, intercept
}
func linear_regression(
features, labels []float64,
rate float64,
epochs int,
) func(feature float64) float64 {
slope := mean(features)
intercept := slope
epoch := 0
for epoch < epochs {
pos := rand.Intn(len(features))
slope, intercept = square_trick(
slope,
intercept,
features[pos],
labels[pos],
rate,
)
epoch++
}
return func(feature float64) float64 {
return slope*feature + intercept
}
}
func main() {
features := []float64{1, 2, 3, 5, 6, 7}
labels := []float64{150, 200, 250, 350, 400, 450}
rate := 0.01
epochs := 10000
model := linear_regression(features, labels, rate, epochs)
fmt.Println(model(4))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment