Skip to content

Instantly share code, notes, and snippets.

@dmitry-a-morozov
Created August 20, 2013 13:48
Show Gist options
  • Save dmitry-a-morozov/6281692 to your computer and use it in GitHub Desktop.
Save dmitry-a-morozov/6281692 to your computer and use it in GitHub Desktop.
open System.IO
open System.Net
let wc = new WebClient()
let trainingSetFile = Path.Combine( __SOURCE_DIRECTORY__, "trainingSet.csv")
File.WriteAllText(trainingSetFile,
contents = wc.DownloadString("http://brandewinder.blob.core.windows.net/public/trainingsample.csv")
)
let validationSetFile = Path.Combine( __SOURCE_DIRECTORY__, "validationSet.csv")
File.WriteAllText(validationSetFile,
contents = wc.DownloadString("http://brandewinder.blob.core.windows.net/public/validationsample.csv")
)
type Example = { Label:int; Pixels:int[] }
let parseDataset fileName =
query {
for line in File.ReadAllLines fileName do
skip 1
let oneImage = query { for x in line.Split(',') do select(int x) } |> Seq.toArray
select { Label = oneImage.[0]; Pixels = oneImage.[1..] }
} |> Seq.toArray
let trainingSet = parseDataset trainingSetFile
let validationSet = parseDataset validationSetFile
let distance p1 p2 = (p1, p2) ||> Array.map2 (fun x1 x2 -> (x1-x2)*(x1-x2)) |> Array.sum
let classify (trainingSet : Example[]) (input : Example) =
trainingSet
|> Array.minBy (fun example -> distance input.Pixels example.Pixels)
|> (fun x -> x.Label)
let checkValidationSet =
let success, failure =
validationSet
|> Array.map (fun x -> classify trainingSet x = x.Label)
|> Array.partition id
float success.Length/(float success.Length + float failure.Length)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment