Book Image

Clojure for Data Science

By : Henry Garner
Book Image

Clojure for Data Science

By: Henry Garner

Overview of this book

Table of Contents (18 chapters)
Clojure for Data Science
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Preface
Index

Implementing logistic regression with Incanter


We can define a logistic regression function with Incanter's minimize function as follows:

(defn logistic-regression [ys xs]
  (let [cost-fn (fn [coefs]
                  (let [classify (sigmoid-function coefs)
                        y-hats   (map (comp classify i/trans) xs)]
                    (logistic-cost ys y-hats)))
        init-coefs (repeat (i/ncol xs) 0.0)]
    (o/minimize cost-fn init-coefs)))

The cost-fn accepts a matrix of coefficients. We create a classifier from the coefficients using the sigmoid-function previously defined, and a sequence of predictions, y-hats, based on the input data. Finally, we can calculate and return the logistic-cost value based on the provided coefficients.

To perform logistic regression, we minimize the logistic cost-fn by selecting the optimal parameters to the sigmoid-function. Since we have to start somewhere, our initial coefficients are simply 0.0 for each parameter.

The minimize function expects to...