-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLinearRegressionWithOneVariable.fs
50 lines (37 loc) · 1.78 KB
/
LinearRegressionWithOneVariable.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module LinearRegressionWithOneVariable
open System.IO
open FSharp.Charting
let h (θ0, θ1) x = θ0 + θ1 * x
let J data θ =
let m = List.length data |> float
1. / (2. * m) * List.sumBy (fun (x, y) -> (h θ x - y) ** 2.) data
let private innerGradientDescent iterationFunction α maxIterations data =
let m = List.length data |> float
let iteration θ =
(fst θ - (α / m) * List.sumBy (fun (x, y) -> h θ x - y) data,
snd θ - (α / m) * List.sumBy (fun (x, y) -> (h θ x - y) * x) data)
(0.0, 0.0) |> iterationFunction iteration maxIterations
let gradientDescent = innerGradientDescent Iteration.iterateUntilConvergence
let gradientDescentWithIntermediateResults = innerGradientDescent Iteration.iterateUntilConvergenceWithIntermediateResults
let plotGradientDescentIterations α maxIterations data =
let θiterations =
gradientDescentWithIntermediateResults α maxIterations data
|> Array.ofSeq
θiterations |> Seq.map (J data)
|> Charting.plotIterations "Gradient Descent Iterations" "Cost J"
|> ignore
θiterations.[θiterations.Length - 1]
let plot θ (data : (float*float) list) =
Chart.Combine
[ Chart.Point(data, Name = "Training Data") |> Charting.withRedCrossMarkerStyle
Chart.Line([ for (x, _) in data -> (x, h θ x) ], Name = "Linear Regression") ]
|> Chart.WithLegend()
|> ChartWindow.showWithTitle "Linear Regression"
let loadData file =
let loadLine (line: string) =
let parts = line.Split(',')
assert (parts.Length = 2)
(float parts.[0], float parts.[1])
File.ReadLines(__SOURCE_DIRECTORY__ + "/data/" + file)
|> Seq.map loadLine
|> List.ofSeq