-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
53 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Package linreg provides a basic implementation of linear regression | ||
// with gradient descent on two dimensional data. | ||
package linreg | ||
|
||
import "fmt" | ||
|
||
// LinearRegression runs the requested number of iterations of gradient | ||
// descent and returns the latest approximated coefficients. | ||
func LinearRegression(xs, ys []float64, iterations int, alpha float64) (m, c float64) { | ||
for i := 0; i < iterations; i++ { | ||
cost, dm, dc := Gradient(xs, ys, m, c) | ||
m += -dm * alpha | ||
c += -dc * alpha | ||
if (10 * i % iterations) == 0 { | ||
fmt.Printf("cost(%.2f, %.2f) = %.2f\n", m, c, cost) | ||
} | ||
} | ||
|
||
return m, c | ||
} | ||
|
||
// Gradient computes the cost function and its gradients. | ||
func Gradient(xs, ys []float64, m, c float64) (cost, dm, dc float64) { | ||
for i := range xs { | ||
d := ys[i] - (xs[i]*m + c) | ||
cost += d * d | ||
dm += -xs[i] * d | ||
dc += -d | ||
} | ||
n := float64(len(xs)) | ||
return cost / n, 2 / n * dm, 2 / n * dc | ||
} |
This file contains 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