Created
May 13, 2011 07:30
-
-
Save davidandrzej/970144 to your computer and use it in GitHub Desktop.
Create 1-of-N binary encoder from a discrete feature
This file contains 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
;; 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