Created
          December 11, 2014 08:28 
        
      - 
      
 - 
        
Save edmellum/895a3515664c14d458c2 to your computer and use it in GitHub Desktop.  
    F# Dojo Digits Recognizer
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | type Image = int [] | |
| type Sample = { Label:int; Pixels:Image } | |
| let csvToRecords (lines:string[]) = | |
| lines.[1..] | |
| |> Array.Parallel.map (fun x -> x.Split(',')) | |
| |> Array.Parallel.map (Array.map int) | |
| |> Array.Parallel.map (fun x -> {Label = x.[0]; Pixels = x.[1..]}) | |
| let distance (sample1:Image) (sample2:Image) = | |
| Array.map2 (fun x y -> pown (x - y) 2) sample1 sample2 | |
| |> Array.sum | |
| |> float | |
| |> sqrt | |
| let classify (examples:Sample[]) (unknown:Sample) = | |
| examples | |
| |> Array.sortBy (fun x -> distance unknown.Pixels x.Pixels) | |
| |> fun x -> x.[..10] | |
| |> Seq.groupBy (fun x -> x.Label) | |
| |> Seq.maxBy (fun (x, _) -> x) | |
| |> fun (x, _) -> x | |
| let validations = File.ReadAllLines("validationsample.csv") |> csvToRecords | |
| let examples = File.ReadAllLines("trainingsample.csv") |> csvToRecords | |
| validations | |
| |> Array.averageBy (fun x -> if x.Label = classify examples x then 1.0 else 0.0) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment