Skip to content

Instantly share code, notes, and snippets.

@tjennings
Created July 9, 2013 20:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjennings/5960953 to your computer and use it in GitHub Desktop.
Save tjennings/5960953 to your computer and use it in GitHub Desktop.
(ns ml-jam.core
(:use clojure-csv.core))
(def training-data
(for [row (rest (parse-csv (slurp "./digitssample.csv")))]
(for [x row] ( Integer/parseInt x))))
(def check-data
(for [row (rest (parse-csv (slurp "./digitssample.csv")))]
(for [x row] ( Integer/parseInt x))))
(defn distance [r1 r2]
(reduce + (map (fn [x y] (Math/pow (- x y) 2)) (rest r1) (rest r2))))
(defn class-of [r]
(first r))
(defn classify [row tset]
(let [groups (group-by first tset)
averages (pmap (fn [[k v]] [k (/ (reduce + (mapv (partial distance row) v)) (count v))]) groups)
;;averages (for [[k v] groups]
;; [k (/ (reduce + (mapv (partial distance row) v)) (count v))])
]
(first (sort-by second averages))))
(defn check [data]
(for [r data]
[(first r) (classify r training-data)]))
(defn results [input]
(let [result (check input)]
(println (float (/ (count (filter #(= (first %) (first (second %))) result)) (count result))))))
;; (map (fn [x y] (Math/pow (- x y) 2)) (nth training-data 1) (nth training-data 2))
;; (map (fn [x y] (println x)) (nth training-data 0) (nth training-data 1))
;; (distance (nth training-data 0) (nth training-data 1))
;; (classify (first training-data) training-data)
;; (time (results (take 10 check-data)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment