Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linear_regression_intro WIP #88

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions notebooks/noj_book/linear_regression_intro.clj
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,41 @@ totals-with-day-of-week
(tcc/* 1)))))
totals-with-day-of-week
days-of-week)
(tc/drop-columns [:day-of-week])))
(tc/drop-columns [:day-of-week])
(dsmod/set-inference-target :total)))

totals-with-one-hot-days-of-week
(-> totals-with-one-hot-days-of-week
(tc/select-columns dsmod/inference-column?))

;; Let us compute the linear regression model using Fastmath.
;; We will use this wrapper function that handles a dataset
;; (a concept which is unknown to Fastmath):

(defn lm [dataset options]
(let [inference-column-name (-> dataset
dsmod/inference-target-column-names
first)
ds-without-target (-> dataset
(tc/drop-columns [inference-column-name]))]
(reg/lm
;; ys
(get dataset inference-column-name)
;; xss
(tc/rows ds-without-target)
;; options
(merge {:names (-> ds-without-target
tc/column-names
vec)}
options))))

;; The binary columns are collinear (sum up to 1),
;; but we will avoide the intercept.
;; This way, the interpretation of each coefficient is the expected
;; bike count for the corresponding day of week.

(def fit
(let [data-without-target (-> totals-with-one-hot-days-of-week
(tc/drop-columns [:total]))]
data-without-target
(reg/lm (:total totals-with-one-hot-days-of-week)
(tc/rows data-without-target)
{:intercept? false
:names (vec (tc/column-names data-without-target))})))
(def days-of-week-model
(lm totals-with-one-hot-days-of-week
{:intercept? false}))

;; Here are the regression results:

Expand Down
Loading