Skip to content

Commit 2c6926d

Browse files
committed
removed :pre-metric-standarisation-fn
1 parent f29cc39 commit 2c6926d

4 files changed

Lines changed: 38 additions & 92 deletions

File tree

deps.edn

Lines changed: 2 additions & 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 "2cac3eff15792462aaf07ebf34f758e694214d45"}
5+
:git/sha "86f5c18b4c9eb12ca187eae275357db53155d88b"}
66
;{:mvn/version "1.3.2"}
77

88
cheshire/cheshire {:mvn/version "6.1.0"}
@@ -20,6 +20,7 @@
2020
same/ish {:mvn/version "0.1.7"}
2121
;; the unit tests needs this
2222
org.tribuo/tribuo-regression-sgd {:mvn/version "4.3.2"}
23+
org.tribuo/tribuo-classification-libsvm {:mvn/version "4.3.2"}
2324
org.tribuo/tribuo-classification-tree {:mvn/version "4.3.2"}}}
2425

2526

src/scicloj/ml/tribuo.clj

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -231,40 +231,21 @@
231231

232232

233233
(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-
"
251234
[prediction-ds trueth-ds discrete-or-continous]
252-
253235
{:prediction (do-harmonize-prediction prediction-ds discrete-or-continous)
254236
:trueth (do-harmonize-trueth trueth-ds discrete-or-continous)}
255237
)
256238

257239

258240
(defn- score
259-
([model scoring-ds options]
241+
([prediction-ds trueth-ds metric-fn]
260242
;; classificatioon only
261-
(let [prediction (ml/predict (cf/feature scoring-ds) model)
262-
trueth (cf/target scoring-ds)
263-
standardised (pre-metric-standardise prediction trueth :discrete)
243+
(let [
244+
standardised (pre-metric-standardise prediction-ds trueth-ds :discrete)
264245
prediction-values (:prediction standardised)
265246
trueth-values (:trueth standardised)
266247
_ (safety-first! prediction-values trueth-values)]
267-
(loss/classification-accuracy prediction-values trueth-values)))
248+
(metric-fn prediction-values trueth-values)))
268249
([model scoring-ds](score model scoring-ds nil))
269250
)
270251

@@ -275,7 +256,6 @@
275256
{:glance-fn glance-fn-regression
276257
:augment-fn augment-fn-regression
277258
:thaw-fn thaw
278-
:pre-metric-standarisation-fn pre-metric-standardise
279259
})
280260

281261

@@ -285,7 +265,6 @@
285265
predict-classification
286266
{:thaw-fn thaw
287267
:score-fn score
288-
:pre-metric-standarisation-fn pre-metric-standardise
289268
})
290269

291270

test/scicloj/ml/linear_regression_test.clj

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -96,44 +96,3 @@
9696
)
9797

9898

99-
(comment
100-
101-
(def tribuo-linear-sdg
102-
(ml/train
103-
diabetes
104-
{:model-type :scicloj.ml.tribuo/regression
105-
:tribuo-components [{:name "squared"
106-
:type "org.tribuo.regression.sgd.objectives.SquaredLoss"}
107-
{:name "trainer"
108-
:type "org.tribuo.regression.sgd.linear.LinearSGDTrainer"
109-
:properties {:epochs "100"
110-
:minibatchSize "1"
111-
:objective "squared"}}]
112-
:tribuo-trainer-name "trainer"}))
113-
114-
115-
116-
(def prediction (ml/predict diabetes tribuo-linear-sdg))
117-
118-
119-
120-
;1. classical
121-
(loss/mae (:disease-progression prediction)
122-
(:disease-progression (cf/target diabetes)))
123-
;;=> 54.83060340549487
124-
125-
126-
127-
;2. use standardized
128-
(def standardised
129-
((:pre-metric-standarisation-fn
130-
(ml/options->model-def {:model-type :scicloj.ml.tribuo/regression}))
131-
prediction
132-
diabetes :continous))
133-
(loss/mae (:prediction standardised) (:trueth standardised))
134-
;;=> 54.83060340549487
135-
136-
137-
)
138-
139-

test/scicloj/ml/tribuo_test.clj

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
[scicloj.ml.tribuo]
99
[tablecloth.api :as tc]
1010
[tech.v3.dataset :as ds]
11-
[tech.v3.dataset.categorical :as dscat]
12-
[tech.v3.dataset.modelling :as ds-mod]
11+
[tech.v3.dataset.categorical :as ds-cat]
1312
[tech.v3.dataset.column-filters :as cf]
14-
[tech.v3.libs.tribuo :as tribuo]))
15-
13+
[tech.v3.dataset.modelling :as ds-mod]))
1614

1715
(def iris-target-raw
1816
(->>
@@ -58,24 +56,19 @@
5856
model (ml/train (:train-ds split) options)
5957
predictions (-> (ml/predict (:test-ds split) model))
6058

61-
standardise-fn (:pre-metric-standarisation-fn (ml/options->model-def options))
62-
63-
6459

6560
accuracy (loss/classification-accuracy (-> split :test-ds
66-
dscat/reverse-map-categorical-xforms
61+
ds-cat/reverse-map-categorical-xforms
6762
:species)
6863
(-> predictions
69-
dscat/reverse-map-categorical-xforms
64+
ds-cat/reverse-map-categorical-xforms
7065
:species))
71-
score-fn (:score-fn (ml/options->model-def options))
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))]
77-
78-
(t/is (< expected-accuracy accuracy-from-standardised))
66+
score (ml/score model
67+
(cf/prediction predictions)
68+
(cf/target (:test-ds split))
69+
loss/classification-accuracy
70+
)]
71+
7972
(t/is (< expected-accuracy score))
8073
(t/is (< expected-accuracy accuracy))
8174
(t/is (= expected-target-val
@@ -343,6 +336,7 @@
343336
(validate-target-symetry :float64))
344337

345338

339+
346340
(t/deftest xxx
347341
(let [iris
348342
(make-iris-ds
@@ -355,24 +349,37 @@
355349
:int)
356350
split (ds-mod/train-test-split iris {:seed 123})
357351
options {:model-type :scicloj.ml.tribuo/classification
358-
:tribuo-components [{:name "trainer"
359-
:type "org.tribuo.classification.dtree.CARTClassificationTrainer"}]
352+
:tribuo-components [{:name "nu"
353+
:type "org.tribuo.classification.libsvm.SVMClassificationType"
354+
:properties {:type "NU_SVC"}}
355+
{:name "trainer"
356+
:type "org.tribuo.classification.libsvm.LibSVMClassificationTrainer"
357+
:properties {:svmType "nu"
358+
:probability "true"
359+
}}]
360360
:tribuo-trainer-name "trainer"}
361361
model (ml/train (:train-ds split)
362362
options)
363363

364-
p (ml/predict (:test-ds split) model)]
365-
(t/is (= (-> p cf/prediction ds/column-names) [:species]))
366-
(t/is (= (-> p cf/probability-distribution ds/column-names) ["virginica" "setosa" "versicolor"]))
364+
prediction (ml/predict (:test-ds split) model)]
365+
366+
367+
(t/is (= 1.0
368+
(ml/score model
369+
prediction
370+
(:test-ds split)
371+
loss/classification-accuracy)))
372+
373+
374+
(t/is (= (-> prediction cf/prediction ds/column-names) [:species]))
375+
(t/is (= (->> prediction cf/probability-distribution ds/column-names (into #{})) #{"virginica" "setosa" "versicolor"}))
367376

368377
(t/is (.equals
369378
{:name :species,
370379
:datatype :int32,
371380
:n-elems 45,
372381
:column-type :prediction,
373382
:categorical-map {:lookup-table {:versicolor 0, :setosa 1, :virginica 2}, :src-column :species, :result-datatype :int}}
374-
(-> p (cf/prediction) :species meta)))))
375-
376-
383+
(-> prediction (cf/prediction) :species meta)))))
377384

378385

0 commit comments

Comments
 (0)