|
4 | 4 | [scicloj.metamorph.ml.loss :as loss] |
5 | 5 | [scicloj.metamorph.ml :as ml] |
6 | 6 | [tech.v3.dataset :as ds] |
| 7 | + [tech.v3.dataset.column :as ds-col] |
7 | 8 | [tech.v3.dataset.modelling :as ds-mod] |
8 | 9 | [tech.v3.datatype :as dt] |
9 | 10 | [tech.v3.datatype.errors :as errors] |
|
196 | 197 | different-types (frequencies values-1) (frequencies values-2))))) |
197 | 198 |
|
198 | 199 |
|
| 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 | + |
199 | 258 | (defn- score |
200 | 259 | ([model scoring-ds options] |
| 260 | + ;; classificatioon only |
201 | 261 | (let [prediction (ml/predict (cf/feature scoring-ds) model) |
202 | 262 | 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) |
213 | 266 | _ (safety-first! prediction-values trueth-values)] |
214 | 267 | (loss/classification-accuracy prediction-values trueth-values))) |
215 | 268 | ([model scoring-ds](score model scoring-ds nil)) |
|
232 | 285 | predict-classification |
233 | 286 | {:thaw-fn thaw |
234 | 287 | :score-fn score |
| 288 | + :pre-metric-standarisation-fn pre-metric-standardise |
235 | 289 | }) |
236 | 290 |
|
237 | 291 |
|
|
0 commit comments