Skip to content

Commit

Permalink
added suport for tidy,glance,augment
Browse files Browse the repository at this point in the history
  • Loading branch information
behrica committed Apr 27, 2024
1 parent 3d6ddfe commit e0cc9cd
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
unreleased
- upgraded deps

- added suport for glance,augment,tidy

0.7.10
- added missing deps
Expand Down
77 changes: 77 additions & 0 deletions src/scicloj/metamorph/ml.clj
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,9 @@
[:thaw-fn {:optional true} fn?]
[:explain-fn {:optional true} fn?]
[:loglik-fn {:optional true} fn?]
[:tidy-fn {:optional true} fn?]
[:augment-fn {:optional true} fn?]
[:glance-fn {:optional true} fn?]
[:options {:optional true} sequential?]
[:documentation {:optional true} [:map
[:javadoc {:optional true} [:maybe string?]]
Expand All @@ -527,6 +530,9 @@
thaw-fn
explain-fn
loglik-fn
tidy-fn
glance-fn
augment-fn
options
documentation
unsupervised?]
Expand All @@ -539,6 +545,9 @@
:thaw-fn thaw-fn
:explain-fn explain-fn
:loglik-fn loglik-fn
:glance-fn glance-fn
:tidyfn tidy-fn
:augment-fn augment-fn
:options options
:unsupervised? unsupervised?
:documentation documentation})
Expand Down Expand Up @@ -689,6 +698,74 @@
:loglik-fn)]
(loglik-fn y yhat)))

(defn tidy
"summarizes information about model components.
Returns a dataset with rows from this list:
https://raw.githubusercontent.com/scicloj/metamorph.ml/main/resources/columms-tidy.edn
No other row names should be used.
Each model will only return a small subset of possible rows.
The list of allowed row names might change over time.
A model might not implement this function, and then an empty dataset will be returned.
"
[model]
(let [tidy-fn
(get
(options->model-def (:options model))
:tidy-fn)]
(if (tidy-fn)
(tidy-fn model)
(ds/->dataset {}))))

(defn glance
"Gives a glance on the model, returning a dataset with model information
about the entire model.
Potential row names are these:
https://raw.githubusercontent.com/scicloj/metamorph.ml/main/resources/columms-glance.edn
No other row names should be used.
Each model will only return a small subset of possible rows.
The list of allowed row names might change over time.
A model might not implement this function, and then an empty dataset will be returned.
"
[model]
(let [glance-fn
(get
(options->model-def (:options model))
:glance-fn)]
(if glance-fn
(glance-fn model)
(ds/->dataset {}))))


(defn augment
"
Adds informations about observations to a dataset
Potential row names are these:
https://raw.githubusercontent.com/scicloj/metamorph.ml/main/resources/columms-augment.edn
No other row names should be used.
Each model will only return a small subset of possible rows.
A model might not implement this function, and then the dataset is
returned unchanged.
"
[model data]

(let [augment-fn
(get
(options->model-def (:options model))
:augment-fn)]
(if augment-fn
(augment-fn model data)
data)))



(defn explain
Expand Down
2 changes: 1 addition & 1 deletion src/scicloj/metamorph/ml/classification.clj
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@
(ds/new-dataset [(ds/new-column target-column-name dummy-labels {:column-type :prediction
:categorical-map (get target-categorical-maps target-column-name)})])))

{})
{:glance-fn (fn [_] (ds/->dataset {:npar 0}))})
25 changes: 10 additions & 15 deletions src/scicloj/metamorph/ml/metrics.clj
Original file line number Diff line number Diff line change
Expand Up @@ -265,30 +265,25 @@



(defn AIC [model ds prediction-ds]
(let [inference-target (first (ds-mod/inference-target-column-names ds))
(defn AIC [model y yhat]
(let [
l (ml/loglik model
(get ds inference-target)
(get prediction-ds inference-target))
y
yhat)

p (dec ( ds/column-count ds))

p (dec ( count y))
k (+ 2 p)]

(-
(* 2 k)
(* 2 l))))


(defn BIC [model ds prediction-ds]
(let [inference-target (first (ds-mod/inference-target-column-names ds))
l
(ml/loglik model
(get ds inference-target)
(get prediction-ds inference-target))


n (ds/row-count ds)
p (dec ( ds/column-count ds))
(defn BIC [model y yhat sample-size]
(let [l (ml/loglik model y yhat)
n sample-size
p (dec ( count y))
k (+ 2 p)]

(+ (* -2 l)
Expand Down
4 changes: 4 additions & 0 deletions test/scicloj/metamorph/classification_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,15 @@
:dummy-strategy :fixed-class
:fixed-class 0})



prediction (ml/predict ds model)]


(is (= (:species prediction) (repeat 150 0)))))



(deftest dummy-classification-majority []
(let [ds (toydata/breast-cancer-ds)
model (ml/train ds {:model-type :metamorph.ml/dummy-classifier
Expand Down

0 comments on commit e0cc9cd

Please sign in to comment.