Skip to content

Commit de6f360

Browse files
committed
added pre-metric-standardise fn
1 parent 8032032 commit de6f360

3 files changed

Lines changed: 77 additions & 14 deletions

File tree

deps.edn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
:deps {org.clojure/clojure {:mvn/version "1.12.4"}
33
org.scicloj/metamorph.ml
44
{:git/url "https://github.com/scicloj/metamorph.ml"
5-
:git/sha "bbd4d390981fbb77d0f93d489f5ed299d2c80080"}
5+
:git/sha "2cac3eff15792462aaf07ebf34f758e694214d45"}
66
;{:mvn/version "1.3.2"}
77

88
cheshire/cheshire {:mvn/version "6.1.0"}

src/scicloj/ml/tribuo.clj

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[scicloj.metamorph.ml.loss :as loss]
55
[scicloj.metamorph.ml :as ml]
66
[tech.v3.dataset :as ds]
7+
[tech.v3.dataset.column :as ds-col]
78
[tech.v3.dataset.modelling :as ds-mod]
89
[tech.v3.datatype :as dt]
910
[tech.v3.datatype.errors :as errors]
@@ -196,20 +197,72 @@
196197
different-types (frequencies values-1) (frequencies values-2)))))
197198

198199

200+
201+
(defn- do-harmonize [ds variable-type filter-fn]
202+
(let [column (-> ds
203+
filter-fn
204+
(dscat/reverse-map-categorical-xforms)
205+
(ds/columns)
206+
first)]
207+
(assert (some? column)
208+
(format "No column found matching filter: %s\nmeta of ds columns: %s"
209+
filter-fn
210+
(mapv meta (ds/columns ds)))
211+
)
212+
(case variable-type
213+
:discrete
214+
(case (-> column meta :datatype)
215+
:keyword (vec column)
216+
:int64 (vec column)
217+
:string (vec column)
218+
:boolean (vec column)
219+
:float64 (int-array column))
220+
221+
:continous (ds-col/to-double-array column)))
222+
)
223+
224+
225+
226+
(defn- do-harmonize-trueth [ds discrete-or-continous]
227+
(do-harmonize ds discrete-or-continous cf/target))
228+
229+
(defn- do-harmonize-prediction [ds discrete-or-continous]
230+
(do-harmonize ds discrete-or-continous cf/prediction))
231+
232+
233+
(defn pre-metric-standardise
234+
"converts prediction result and the trueth into either
235+
seq of
236+
:discrete keyword,string,intXX,...
237+
:continous double, float
238+
or fails.
239+
240+
`prediction-ds` and `thrueth-ds` are tabular data,
241+
usualy of type tech.v3.dataset
242+
243+
returns map of
244+
:prediction (seq)
245+
:trueth (seq)
246+
247+
I case of :discrete the discrete values in :predicion and :trueth
248+
should have semantically identical meaning, as they might get
249+
compared via '=' later
250+
"
251+
[prediction-ds trueth-ds discrete-or-continous]
252+
253+
{:prediction (do-harmonize-prediction prediction-ds discrete-or-continous)
254+
:trueth (do-harmonize-trueth trueth-ds discrete-or-continous)}
255+
)
256+
257+
199258
(defn- score
200259
([model scoring-ds options]
260+
;; classificatioon only
201261
(let [prediction (ml/predict (cf/feature scoring-ds) model)
202262
trueth (cf/target scoring-ds)
203-
prediction-values
204-
(->
205-
(cf/prediction prediction)
206-
(dscat/reverse-map-categorical-xforms)
207-
(get (-> model :target-columns first)))
208-
trueth-values
209-
(->
210-
trueth
211-
(dscat/reverse-map-categorical-xforms)
212-
(get (-> model :target-columns first)))
263+
standardised (pre-metric-standardise prediction trueth :discrete)
264+
prediction-values (:prediction standardised)
265+
trueth-values (:trueth standardised)
213266
_ (safety-first! prediction-values trueth-values)]
214267
(loss/classification-accuracy prediction-values trueth-values)))
215268
([model scoring-ds](score model scoring-ds nil))
@@ -232,6 +285,7 @@
232285
predict-classification
233286
{:thaw-fn thaw
234287
:score-fn score
288+
:pre-metric-standarisation-fn pre-metric-standardise
235289
})
236290

237291

test/scicloj/ml/tribuo_test.clj

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
[tech.v3.dataset :as ds]
1111
[tech.v3.dataset.categorical :as dscat]
1212
[tech.v3.dataset.modelling :as ds-mod]
13-
[tech.v3.dataset.column-filters :as cf]))
13+
[tech.v3.dataset.column-filters :as cf]
14+
[tech.v3.libs.tribuo :as tribuo]))
1415

1516

1617
(def iris-target-raw
@@ -57,16 +58,24 @@
5758
model (ml/train (:train-ds split) options)
5859
predictions (-> (ml/predict (:test-ds split) model))
5960

61+
standardise-fn (:pre-metric-standarisation-fn (ml/options->model-def options))
62+
63+
64+
6065
accuracy (loss/classification-accuracy (-> split :test-ds
6166
dscat/reverse-map-categorical-xforms
6267
:species)
6368
(-> predictions
6469
dscat/reverse-map-categorical-xforms
6570
:species))
6671
score-fn (:score-fn (ml/options->model-def options))
67-
score (score-fn model (:test-ds split))]
68-
72+
score (score-fn model (:test-ds split))
73+
standardised (standardise-fn predictions (:test-ds split) :discrete)
74+
accuracy-from-standardised (loss/classification-accuracy
75+
(-> standardised :prediction)
76+
(-> standardised :trueth))]
6977

78+
(t/is (< expected-accuracy accuracy-from-standardised))
7079
(t/is (< expected-accuracy score))
7180
(t/is (< expected-accuracy accuracy))
7281
(t/is (= expected-target-val

0 commit comments

Comments
 (0)