Created
July 10, 2016 19:22
-
-
Save mathias-brandewinder/720e520a5a7d1a389398b3b6ffb27bc2 to your computer and use it in GitHub Desktop.
DotNetFringe
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
open System | |
open System.IO | |
let path = @"C:\Users\Mathias\Desktop\day-1-test\day-1\trainingsample.csv" | |
let data0 = File.ReadAllLines path | |
let data1 = | |
data0 |> Array.map (fun line -> line.Split ',') | |
let data2 = | |
data1.[1..] | |
let data3 = | |
data2 | |
|> Array.map (fun line -> | |
line | |
|> Array.map (fun x -> int x)) | |
type Pixels = int[] | |
type Example = { Label:int; Pixels:Pixels } | |
let data4 = | |
data3 | |
|> Array.map (fun line -> { Example.Label = line.[0]; Pixels = line.[1..] } ) | |
let readFile path = | |
path | |
|> File.ReadAllLines | |
|> Array.map (fun line -> line.Split ',') | |
|> fun lines -> lines.[1..] | |
|> Array.map (fun line -> | |
line | |
|> Array.map (fun x -> int x)) | |
|> Array.map (fun line -> | |
{ Example.Label = line.[0]; Pixels = line.[1..] } ) | |
let trainpath = @"C:\Users\Mathias\Desktop\day-1-test\day-1\trainingsample.csv" | |
let testpath = @"C:\Users\Mathias\Desktop\day-1-test\day-1\validationsample.csv" | |
let train = readFile trainpath | |
let test = readFile testpath | |
let distance (img1:Pixels) (img2:Pixels) = | |
Array.map2 (fun x y -> (x-y) * (x-y)) img1 img2 | |
|> Array.sum | |
|> float | |
|> sqrt | |
let classify (img:Pixels) = | |
train | |
|> Array.minBy (fun example -> distance example.Pixels img) | |
|> fun example -> example.Label | |
let classify2 (img:Pixels) = 5 | |
let quality = | |
test | |
|> Array.averageBy (fun example -> | |
if example.Label = classify2 (example.Pixels) | |
then 1.0 | |
else 0.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment