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

Get Clojure for Data Science now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.