It is also useful to have a simple way to display nicely formatted code. Our example snippet is a Clojure function for constructing a machine learning feature encoder, and we display it by

GitHub gist embedding

Google Prettify

;; 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))}))

Follow me on the twitters: @davidandrzej

Published

2012-08-21

Categories


Tags