Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module DigitRecognizerModule
- open System
- open System.IO
- // type represents sample from dataset
- type Sample = {Label:int; Vector:int[] }
- // load CSV and return Sample array
- let loadCSVLines(fn:string) = File.ReadAllLines(fn)
- let lineToIntArr(line:string) = Array.map (fun s->s|>int) (line.Split(','))
- let getData(fn:string) =
- loadCSVLines(fn)
- |> (fun x->x.[1..])
- |> Array.map (fun x->let arr=lineToIntArr x in {Label=arr.[0]; Vector=arr.[1..]})
- // distance between two vectors
- let dist (v1:int[], v2:int[]) =
- Array.zip v1 v2
- |> Array.map (fun x->((fst x-snd x)|>float)**2.0)
- |> Array.sum
- // classify test sample by looking for most similar training sample
- let classify(train:Sample[], test:Sample)=
- Array.map (fun x->dist(x.Vector, test.Vector)) train
- |> Array.zip [|0..(train|>Array.length)-1|]
- |> Array.fold (fun acc x->if (snd acc) > (snd x) then x else acc) (0, System.Double.MaxValue)
- |> fst
- |> (fun i->train.[i].Label)
- // evaluate the digit classifier
- let eval()=
- // load data
- let dir="../../../../"
- let test=getData(dir+"test.csv")
- let train=getData(dir+"train.csv")
- let len=test |> Array.length
- // classify samples from test set, calc accuracy
- let accuracy=(Array.map (fun i->if classify(train, test.[i])=test.[i].Label then 1.0 else 0.0) [|0..len-1|] )|> Array.sum |> (fun x->x/(len|>float))
- printfn "Accuracy: %f" accuracy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement