(use 'incanter.core)
(import (java.io BufferedReader BufferedWriter InputStreamReader FileReader FileWriter PrintWriter))
(defn linear-regression [X t]
(let [X_T (trans X)]
(mmult (solve (mmult X_T X)) X_T t)))
(defn remove-col [coll-of-colls n]
(map #(concat (take n %) (drop (inc n) %)) coll-of-colls))
(defn odd-items [coll] (map first (partition 1 2 coll)))
(defn even-items [coll] (odd-items (rest coll)))
(defn read-data [file-name feature-size]
(with-open [rdr (BufferedReader. (FileReader. file-name))]
(reduce
(fn [acc line]
(if (re-matches #".*\?.*" line)
acc
(if-let [data-seq (re-seq #"\S+" (re-find #"^[^\"]+" line))]
(let [[features target] (split-at feature-size (map #(Double/parseDouble %) data-seq))]
(assoc (assoc acc 0 (conj (get acc 0) (vec features))) 1 (conj (get acc 1) (vec (conj target -1)))))
acc)))
[[] []] (line-seq rdr))))
(def auto-data (read-data "../RL/Marsland Machine Learning/auto-mpg.data" 1))
(def inputs (second auto-data))
(def targets (first auto-data))
; SUM-OF-SQUARES ERROR
;(let [inputs (remove-col (remove-col inputs 1) 4) ; data cleanup
(let [inputs (remove-col inputs 1) ; data cleanup
beta (trans (linear-regression (odd-items inputs) (odd-items targets)))]
(reduce + (map #(Math/pow (- %1 (first %2)) 2) (map #(mmult beta %) (even-items inputs)) (even-items targets))))
2010年5月30日日曜日
Linear Regression auto-mpg - Clojure / Incanter
登録:
コメントの投稿 (Atom)
0 件のコメント:
コメントを投稿