-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathml_ex1.fsx
56 lines (38 loc) · 2 KB
/
ml_ex1.fsx
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
51
52
53
54
55
56
#load "Setup.fsx"
open MathNet.Numerics.LinearAlgebra
open FSharp.Charting
// ==================== Part 1: Basic Function ====================
printfn "Running warmUpExercise ..."
printfn "5x5 Identity Matrix:"
DenseMatrix.identity<float> 5 |> printfn "%O"
// ======================= Part 2: Plotting =======================
printfn "Plotting Data ..."
let data = LinearRegressionWithOneVariable.loadData "ex1data1.txt"
let chartWindow =
Chart.Point(data, Name = "Training Data")
|> Charting.withRedCrossMarkerStyle
|> Chart.WithXAxis(Title = "Population of City in 10,000s", Min = 4.0)
|> Chart.WithYAxis(Title = "Profit in $10,000s")
|> ChartWindow.showWithTitle "Population of City vs. Profit"
// =================== Part 3: Gradient descent ===================
printfn "Running Gradient Descent ..."
let iterations = 1500
let α = 0.01
let J = (0.0, 0.0) |> LinearRegressionWithOneVariable.J data
printfn "J = %O" J
let θ = data |> LinearRegressionWithOneVariable.gradientDescent α iterations
printfn "θ found by gradient descent: %A" θ
let predictions = [ for (x, _) in data -> (x, LinearRegressionWithOneVariable.h θ x) ]
Chart.Line(predictions, Name = "Linear Regression")
|> Chart.WithLegend()
|> chartWindow.Combine
// Predict values for population sizes of 35,000 and 70,000
let predict1 = LinearRegressionWithOneVariable.h θ 3.5
printfn "For population = 35,000, we predict a profit of %A" (predict1 * 10000.0)
let predict2 = LinearRegressionWithOneVariable.h θ 7.0
printfn "For population = 70,000, we predict a profit of %A" (predict2 * 10000.0)
// ============= Part 4: Visualizing J(θ0, θ1) =============
printfn "Visualizing J(θ0, θ1) ..."
Charting.plotSurface "J surface" (-10.0, -1.0) (10.0, 4.0) (LinearRegressionWithOneVariable.J data)
Charting.plotSurface2 "J surface" (-10.0, -1.0) (10.0, 4.0) (LinearRegressionWithOneVariable.J data)
Charting.plotContour "J contour" ("θ0", "θ1") (-10.0, -1.0) (10.0, 4.0) (LinearRegressionWithOneVariable.J data)