Skip to content

Instantly share code, notes, and snippets.

@davidandrzej
Created May 13, 2011 07:30
Show Gist options
  • Save davidandrzej/970144 to your computer and use it in GitHub Desktop.
Save davidandrzej/970144 to your computer and use it in GitHub Desktop.
Create 1-of-N binary encoder from a discrete feature
;; Assumes examples are Clojure maps,
;; for example from clojure.contrib.sql
;;
;; {:eyecolor "brown" :heightmeters 1.7 ...}
;; {:eyecolor "blue" :heightmeters 1.5 ...}
;;
;; (def eye-encoder (discrete-feature-encoder :eyecolor myexamples))
;;
(defn discrete-feature
"Create binary 1-of-N encoding of a discrete feature"
[featurekey allexamples]
(let [uniqvals (->> allexamples (map featurekey) set vec)]
{:values (map #(format "%s:%s" (str featurekey) %1) uniqvals)
:encoder (fn [example]
(map #(if (= %1 (featurekey example)) 1 0) uniqvals))}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment