|
| 1 | +(ns lambda-ml.decision-tree |
| 2 | + (:require [lambda-ml.data.binary-tree :as bt])) |
| 3 | + |
| 4 | +(defn gini-impurity |
| 5 | + [y] |
| 6 | + (let [total (count y)] |
| 7 | + (->> (vals (frequencies y)) |
| 8 | + (map #(/ % total)) |
| 9 | + (map #(* % (- 1 %))) |
| 10 | + (reduce +)))) |
| 11 | + |
| 12 | +(defn weighted-cost |
| 13 | + [y1 y2 f] |
| 14 | + (let [n1 (count y1) |
| 15 | + n2 (count y2) |
| 16 | + c1 (f y1) |
| 17 | + c2 (f y2)] |
| 18 | + (float (+ (* (/ n1 (+ n1 n2)) c1) |
| 19 | + (* (/ n2 (+ n1 n2)) c2))))) |
| 20 | + |
| 21 | +(defn categorical-partitions |
| 22 | + "Given a seq of k distinct values, returns the 2^{k-1}-1 possible binary |
| 23 | + partitions of the values into sets. Returns a trivial partition when k = 1." |
| 24 | + [vals] |
| 25 | + (let [partition [(hash-set (first vals)) |
| 26 | + (set (rest vals))]] |
| 27 | + (if (<= (count vals) 2) |
| 28 | + [partition] |
| 29 | + (reduce (fn [p [s1 s2]] |
| 30 | + (conj p |
| 31 | + [(conj s1 (first vals)) s2] |
| 32 | + [(conj s2 (first vals)) s1])) |
| 33 | + [partition] |
| 34 | + (categorical-partitions (rest vals)))))) |
| 35 | + |
| 36 | +(defn numeric-partitions |
| 37 | + "Given a seq of k distinct numeric values, returns k-1 possible binary |
| 38 | + partitions of the values by taking the average of consecutive elements in the |
| 39 | + sorted seq of values. Returns the same seq when k = 1." |
| 40 | + [vals] |
| 41 | + (if (= (count vals) 1) |
| 42 | + vals |
| 43 | + (loop [partitions [] |
| 44 | + v (sort vals)] |
| 45 | + (if (= (count v) 1) |
| 46 | + partitions |
| 47 | + (recur (conj partitions (/ (+ (first v) (second v)) 2)) |
| 48 | + (rest v)))))) |
| 49 | + |
| 50 | +(defn splitters |
| 51 | + "Returns a seq of all possible splitters for feature i. A splitter is a |
| 52 | + predicate function that evaluates to true if an example belongs in the left |
| 53 | + subtree, or false if an example belongs in the right subtree, based on the |
| 54 | + splitting criterion." |
| 55 | + [x i] |
| 56 | + (let [domain (distinct (map #(nth % i) x))] |
| 57 | + (cond (number? (first domain)) (->> (numeric-partitions domain) |
| 58 | + (map (fn [s] |
| 59 | + (with-meta |
| 60 | + (fn [x] (<= (nth x i) s)) |
| 61 | + {:decision (float s)})))) |
| 62 | + (string? (first domain)) (->> (categorical-partitions domain) |
| 63 | + (map (fn [[s1 s2]] |
| 64 | + (with-meta |
| 65 | + (fn [x] (contains? s1 (nth x i))) |
| 66 | + {:decision [s1 s2]})))) |
| 67 | + :else (throw (IllegalStateException. "Invalid feature type"))))) |
| 68 | + |
| 69 | +(defn best-splitter |
| 70 | + "Returns the splitter for the given data that minimizes cost function f." |
| 71 | + [f x y] |
| 72 | + (->> (for [i (range (count (first x)))] |
| 73 | + ;; Find best splitter for feature i |
| 74 | + (->> (splitters x i) |
| 75 | + (map (fn [splitter] |
| 76 | + (let [data (map #(conj (vec %1) %2) x y) |
| 77 | + [left right] (vals (group-by splitter data)) |
| 78 | + cost (weighted-cost (map last left) (map last right) f)] |
| 79 | + ;; Add metadata to splitter |
| 80 | + [(vary-meta splitter merge {:cost cost :feature i}) cost i]))) |
| 81 | + (apply min-key second))) |
| 82 | + ;; Find best splitter amongst all features |
| 83 | + (reduce (fn [a b] |
| 84 | + (let [[_ c1 i1] a [_ c2 i2] b] |
| 85 | + (cond (< c1 c2) a |
| 86 | + ;; To match the CART algorithm, break ties in cost by |
| 87 | + ;; choosing splitter for feature with lower index |
| 88 | + (= c1 c2) (if (< i1 i2) a b) |
| 89 | + :else b)))) |
| 90 | + (first))) |
| 91 | + |
| 92 | +(defn decision-tree-fit |
| 93 | + "Fits a decision tree to the given training data." |
| 94 | + ([model data] |
| 95 | + (decision-tree-fit model (map butlast data) (map last data))) |
| 96 | + ([model x y] |
| 97 | + (->> (if (apply = y) |
| 98 | + (bt/make-tree (first y)) |
| 99 | + (let [{cost :cost} model |
| 100 | + splitter (best-splitter cost x y) |
| 101 | + data (map #(conj (vec %1) %2) x y) |
| 102 | + split (group-by splitter data) |
| 103 | + left (get split true) |
| 104 | + right (get split false)] |
| 105 | + (bt/make-tree splitter |
| 106 | + (:parameters (decision-tree-fit model left)) |
| 107 | + (:parameters (decision-tree-fit model right))))) |
| 108 | + (assoc model :parameters)))) |
| 109 | + |
| 110 | +(defn decision-tree-predict |
| 111 | + "Predicts the values of example data using a decision tree." |
| 112 | + [model x] |
| 113 | + (let [{tree :parameters} model] |
| 114 | + (when (not (nil? tree)) |
| 115 | + (letfn [(predict [t xi] |
| 116 | + (let [val (bt/get-value t)] |
| 117 | + (cond (bt/leaf? t) val |
| 118 | + (val xi) (predict (bt/get-left t) xi) |
| 119 | + :else (predict (bt/get-right t) xi))))] |
| 120 | + (map #(predict tree %) x))))) |
| 121 | + |
| 122 | +(defn print-decision-tree |
| 123 | + "Prints information about a given decision tree." |
| 124 | + [model] |
| 125 | + (println (dissoc model :parameters)) |
| 126 | + (when (contains? model :parameters) |
| 127 | + (bt/print-tree (:parameters model)))) |
| 128 | + |
| 129 | +(defn make-decision-tree |
| 130 | + "Returns a decision tree model using the given cost function." |
| 131 | + [cost] |
| 132 | + {:cost cost}) |
0 commit comments