Skip to content

Instantly share code, notes, and snippets.

@jxa
Created July 9, 2013 19:40
Show Gist options
  • Save jxa/5960584 to your computer and use it in GitHub Desktop.
Save jxa/5960584 to your computer and use it in GitHub Desktop.
distance algorithm for Lambda Jam
(ns digits.core
(:require
[clojure.string :as str]
[clojure.java.io :refer [resource reader]]
[clojure.core.reducers :as r]))
(defn train [reader]
(map (fn [line]
(map #(Integer/parseInt %) (str/split line #",")))
(rest (line-seq reader)))
)
;; X = [ x1; x2; .. xn ]
;; Y = [ y1; y2; .. yn ]
;; Dist(X, Y) = (x1-y1)^2 + (x2-y2)^2 .. + (xn-yn)^2
(defn square [^Integer x]
(* x x))
(defn dist
[[_ & x] [_ & y]]
(reduce + (r/map (fn [^Integer x' ^Integer y']
(square (- x' y')))
x y)))
(defn closest
([knowns unknown]
(closest (rest knowns)
unknown
(dist (first knowns) unknown)
(first knowns)))
([knowns unknown best-dist best-match]
(if (seq knowns)
(let [score (dist (first knowns) unknown)]
(if (< score best-dist)
(recur (rest knowns) unknown score (first knowns))
(recur (rest knowns) unknown best-dist best-match)))
[best-dist best-match])))
(defn test [filename]
(let [knowns (train (reader (resource "smalltraining.csv")))]
(map (partial closest knowns)
(take 10 (train (reader (resource filename)))) )))
(comment
(apply dist (take 2 (train (reader (resource "smalltraining.csv")))))
(map (comp first second) (test "digitscheck.csv"))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment