Created
October 14, 2011 05:01
-
-
Save 55v/1286293 to your computer and use it in GitHub Desktop.
ML linear regression cost
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
// Learn more about F# at http://fsharp.net | |
#light | |
let T0 = 0.0 | |
let T1 = 1.0 | |
let m = 4.0 | |
let X = [| 3.0; 1.0; 0.0; 4.0 |] | |
let Y = [| 2.0; 2.0; 1.0; 3.0 |] | |
//let XY = Array.zip(x y) | |
let h x = T0 + T1 * x | |
Array.iter(fun xi -> printfn "%f" (h xi)) X | |
printfn "" | |
let J arrX arrY = (1.0/(2.0 * m)) * Array.fold2(fun acc elemX elemY -> acc + (h elemX - elemY) ** 2.0) 0.0 arrX arrY | |
let result = J X Y | |
printf "%f" result | |
System.Console.ReadKey() |> ignore |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment