Skip to content

Instantly share code, notes, and snippets.

@sudipto80
Created January 21, 2016 02:17
Show Gist options
  • Save sudipto80/84ea679cd85ca7f6f8c4 to your computer and use it in GitHub Desktop.
Save sudipto80/84ea679cd85ca7f6f8c4 to your computer and use it in GitHub Desktop.
Cosine Similarity
let SimuDot (ratings :(float list)list) (a:int)(u:int)=
let num = List.zip ratings.[a] ratings.[u]
|> List.sumBy (fun item -> fst item * snd item)
let d1 = ratings.[a] |> List.sumBy (fun item -> item * item )
let d2 = ratings.[u] |> List.sumBy (fun item -> item * item )
if d1 = 0.0 || d2 = 0.0 then 0.0 else num / (sqrt d1 * sqrt d2)
let ratings = [[4.;0.;5.;5.];[4.;2.;1.;0.];[3.;0.;2.;4.];[4.;4.;0.;0.];[2.;1.;3.;5.]]
SimuDot ratings 0 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment