Created
July 9, 2013 20:24
-
-
Save tjennings/5960953 to your computer and use it in GitHub Desktop.
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
(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