99 [tablecloth.api :as tc]
1010 [tech.v3.dataset :as ds]
1111 [tech.v3.dataset.categorical :as dscat]
12- [tech.v3.dataset.column-filters :as ds-cf]
13- [tech.v3.dataset.modelling :as ds-mod]))
12+ [tech.v3.dataset.modelling :as ds-mod]
13+ [tech.v3.dataset.column-filters :as cf]))
14+
1415
1516(def iris-target-raw
1617 (->>
4647 (ds-mod/set-inference-target :species )))
4748
4849(defn- validate [ds expected-target-val expected-accuracy]
49- (let [split (ds-mod/train-test-split ds {:seed 123 })
50+ (let [options {:model-type :scicloj.ml.tribuo/classification
51+ :tribuo-components [{:name " trainer"
52+ :type " org.tribuo.classification.dtree.CARTClassificationTrainer" }]
53+ :tribuo-trainer-name " trainer" }
5054
51- model (ml/train (:train-ds split)
52- {:model-type :scicloj.ml.tribuo/classification
53- :tribuo-components [{:name " trainer"
54- :type " org.tribuo.classification.dtree.CARTClassificationTrainer" }]
55- :tribuo-trainer-name " trainer" })
55+ split (ds-mod/train-test-split ds {:seed 123 })
56+
57+ model (ml/train (:train-ds split) options)
5658 predictions (-> (ml/predict (:test-ds split) model))
5759
5860 accuracy (loss/classification-accuracy (-> split :test-ds
5961 dscat/reverse-map-categorical-xforms
6062 :species )
6163 (-> predictions
6264 dscat/reverse-map-categorical-xforms
63- :species ))]
65+ :species ))
66+ score-fn (:score-fn (ml/options->model-def options))
67+ score (score-fn model (:test-ds split))]
6468
6569
70+ (t/is (< expected-accuracy score))
6671 (t/is (< expected-accuracy accuracy))
6772 (t/is (= expected-target-val
6873 (->
6974 predictions
70- (ds- cf/prediction )
75+ (cf/prediction )
7176 ds/columns
7277 first
7378 first)))))
8590 nil )
8691 2
8792 0.94 )
88-
93+
8994 (validate
9095 (make-iris-ds
9196 (make-species-column :float32
98103 2.0
99104 0.94 )
100105
106+ (validate
107+ (make-iris-ds
108+ (make-species-column :float32
109+ true ; categorical?
110+ true ; inference-target?
111+ {:setosa 0.0
112+ :versicolor 1.0
113+ :virginica 2.0 })
114+ nil )
115+ 2.0
116+ 0.94 )
117+
118+
119+
101120 (validate
102121
103122 (make-iris-ds
225244 " 12"
226245 0.94 ))))
227246
228- (ds/new-column :x [:a :b :c ])
229247
230248(defn- verify-evaluate [ds]
231- (let [make-pipefn (fn [opts]
232- (mm/pipeline
233- {:metamorph/id :model }
234- (ml/model {:model-type :scicloj.ml.tribuo/classification
235- :tribuo-components [{:name " trainer"
236- :type " org.tribuo.classification.dtree.CARTClassificationTrainer"
237- :properties {:maxDepth " 8" }}]
249+ (let [options
250+ {:model-type :scicloj.ml.tribuo/classification
251+ :tribuo-components [{:name " trainer"
252+ :type " org.tribuo.classification.dtree.CARTClassificationTrainer"
253+ :properties {:maxDepth " 8" }}]
238254
239255
240256
241- :tribuo-trainer-name " trainer" })))
257+ :tribuo-trainer-name " trainer" }
258+ make-pipefn (fn []
259+ (mm/pipeline
260+ {:metamorph/id :model }
261+ (ml/model options)))
242262
243263 splits
244264 (tc/split->seq ds :kfold {:seed 1234 })
245265
246- pipefns [(make-pipefn {} )]
266+ pipefns [(make-pipefn )]
247267
248268 evaluations
249269 (ml/evaluate-pipelines pipefns splits
254274 :return-best-pipeline-only true
255275 :evaluation-handler-fn
256276 (fn [eval-result]
257- eval-result)})]
277+ eval-result)})
278+ ]
258279
259280 (t/is (= " org.tribuo.common.tree.TreeModel"
260281 (->
286307
287308(defn- validate-target-symetry [datatype]
288309 (t/is (= datatype
289- (->>
290- (ml/train
291- (-> (ds/->dataset {:x [1 2 3 4 ]
292- :y [:a :b :c :d ]})
293- (ds/categorical->number [:y ] [] datatype)
294- (ds-mod/set-inference-target [:y ]))
295- {:model-type :scicloj.ml.tribuo/classification
296- :tribuo-components [{:name " trainer"
297- :type " org.tribuo.classification.dtree.CARTClassificationTrainer"
298- :properties {:maxDepth " 8" }}]
299- :tribuo-trainer-name " trainer" })
300- (ml/predict
301- (-> (ds/->dataset {:x [1 2 3 4 ]})))
302- :y
303- meta
304- :datatype ))))
305-
306-
307- (t/deftest validate-target-sym
310+ (->>
311+ (ml/train
312+ (-> (ds/->dataset {:x [1 2 3 4 ]
313+ :y [:a :b :c :d ]})
314+ (ds/categorical->number [:y ] [] datatype)
315+ (ds-mod/set-inference-target [:y ]))
316+ {:model-type :scicloj.ml.tribuo/classification
317+ :tribuo-components [{:name " trainer"
318+ :type " org.tribuo.classification.dtree.CARTClassificationTrainer"
319+ :properties {:maxDepth " 8" }}]
320+ :tribuo-trainer-name " trainer" })
321+ (ml/predict
322+ (-> (ds/->dataset {:x [1 2 3 4 ]})))
323+ :y
324+ meta
325+ :datatype ))))
326+
327+
328+ (t/deftest validate-target-sym
308329 (validate-target-symetry :int8 )
309330 (validate-target-symetry :int16 )
310331 (validate-target-symetry :int32 )
313334 (validate-target-symetry :float64 ))
314335
315336
337+ (t/deftest xxx
338+ (let [iris
339+ (make-iris-ds
340+ (make-species-column :int32
341+ true ; categorical?
342+ true ; inference-target?
343+ {:setosa :setosa
344+ :versicolor :versicolor
345+ :virginica :virginica })
346+ :int )
347+ split (ds-mod/train-test-split iris {:seed 123 })
348+ options {:model-type :scicloj.ml.tribuo/classification
349+ :tribuo-components [{:name " trainer"
350+ :type " org.tribuo.classification.dtree.CARTClassificationTrainer" }]
351+ :tribuo-trainer-name " trainer" }
352+ model (ml/train (:train-ds split)
353+ options)
354+
355+ p (ml/predict (:test-ds split) model)]
356+ (t/is (= (-> p cf/prediction ds/column-names) [:species ]))
357+ (t/is (= (-> p cf/probability-distribution ds/column-names) [" virginica" " setosa" " versicolor" ]))
358+
359+ (t/is (.equals
360+ {:name :species ,
361+ :datatype :int32 ,
362+ :n-elems 45 ,
363+ :column-type :prediction ,
364+ :categorical-map {:lookup-table {:versicolor 0 , :setosa 1 , :virginica 2 }, :src-column :species , :result-datatype :int }}
365+ (-> p (cf/prediction ) :species meta)))))
316366
317367
318368
319-
0 commit comments