Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Digit Recognizer

By: ptrelford on Jun 18th, 2013  |  syntax: F#  |  size: 0.93 KB  |  hits: 63  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. open System
  2. open System.IO
  3.  
  4. type Digit = { Number:int; Pixels:int[] }
  5.  
  6. let toDigits path =
  7.    let lines = File.ReadAllLines(path)
  8.    let strings = lines |> Array.map (fun s -> s.Split(','))
  9.    let data = strings.[1..]
  10.    let values = data |> Array.map (Array.map int)
  11.    values |> Array.map (fun xs -> { Number = xs.[0]; Pixels = xs.[1..] })
  12.  
  13. let samples = let path = @"c:\digits\digitssample.csv" in toDigits path
  14.  
  15. let distance xs ys =
  16.    Array.map2 (fun x y -> (x - y) * (x - y)) xs ys |> Array.sum
  17.  
  18. let classify (unknown:int[]) =
  19.    samples |> Array.minBy (fun sample -> distance unknown sample.Pixels)
  20.    |> fun digit -> digit.Number
  21.  
  22. let checks = let path = @"c:\digits\digitscheck.csv" in toDigits path
  23. let matches =
  24.    checks |> Array.filter (fun check ->
  25.       let x = classify check.Pixels
  26.       printfn "%d %d" x check.Number
  27.       x = check.Number
  28.    )
  29.  
  30. let result = float matches.Length / float checks.Length