O'Reilly logo

Clojure for Data Science by Henry Garner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required