Created
July 9, 2013 20:06
-
-
Save mydogisbox/5960782 to your computer and use it in GitHub Desktop.
F Sharp implementation of the Kaggle digit recognizer exercise.
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.IO | |
open System | |
open System.Text | |
type DataWithDigit = { digit:int | |
data:int array } | |
type Data = {data:int array} | |
let convertToTrainingData (x:string) = | |
let split = x.Split(',') | |
{ digit = (int)split.[0] | |
data = split.[1..] |> Array.map(int) } | |
let convertToTestData (x:string) = | |
{ data = x.Split(',') |> Array.map(int) } | |
let readData file converter= | |
(File.ReadAllLines file).[1..] |> Array.Parallel.map converter | |
let GetData trainingFile testFile = | |
readData trainingFile convertToTrainingData, (readData testFile convertToTestData) | |
let PutData data = | |
File.WriteAllLines("results.csv", data |> Array.map(string)) | |
let euclideanDistance x y = | |
(x,y)||> Array.fold2(fun a b c -> a + pown (b-c) 2) 0 | |
let classifyEuclideanDistance (x:DataWithDigit array) (y:Data) = | |
x |> Array.minBy(fun a -> euclideanDistance (y.data) (a.data)) | |
//do | |
let data = GetData (__SOURCE_DIRECTORY__ + @"\..\" + "train.csv") (__SOURCE_DIRECTORY__ + @"\..\" + "test.csv") | |
let classifyFunction = classifyEuclideanDistance (fst data) | |
let results = (snd data) |> Array.Parallel.map(classifyFunction) | |
PutData (results |> Array.map(fun x-> x.digit.ToString())) | |
//let comparisonData = (fst data).[1000..1500], results | |
//let compareResults = comparisonData ||> Array.map2(fun x y->x, y, x.digit = y.digit) | |
//printfn "%d" (compareResults |> Array.filter(fun (x,y,z)->not z)|>Array.length) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment