|
| 1 | +# This file defines `ConstantClassifier()` |
| 2 | + |
| 3 | +using LearnAPI |
| 4 | +import LearnDataFrontEnds as FrontEnds |
| 5 | +import MLCore |
| 6 | +import CategoricalArrays |
| 7 | +import CategoricalDistributions |
| 8 | +import CategoricalDistributions.OrderedCollections.OrderedDict |
| 9 | +import CategoricalDistributions.Distributions.StatsBase.proportionmap |
| 10 | + |
| 11 | +# The implementation of a constant classifier below is not the simplest, but it |
| 12 | +# demonstrates some patterns that apply more generally in classification. |
| 13 | + |
| 14 | +""" |
| 15 | + ConstantClassifier() |
| 16 | +
|
| 17 | +Instantiate a constant (dummy) classifier. Can predict `Point` or `Distribution` targets. |
| 18 | +
|
| 19 | +""" |
| 20 | +struct ConstantClassifier end |
| 21 | + |
| 22 | +struct ConstantClassifierFitted |
| 23 | + learner::ConstantClassifier |
| 24 | + probabilities |
| 25 | + names::Vector{Symbol} |
| 26 | + classes_seen |
| 27 | + codes_seen |
| 28 | + decoder |
| 29 | +end |
| 30 | + |
| 31 | +LearnAPI.learner(model::ConstantClassifierFitted) = model.learner |
| 32 | + |
| 33 | +# add a data front end; `obs` will return objects with type `FrontEnds.Obs`: |
| 34 | +const front_end = FrontEnds.Sage(code_type=:small) |
| 35 | +LearnAPI.obs(learner::ConstantClassifier, data) = |
| 36 | + FrontEnds.fitobs(learner, data, front_end) |
| 37 | +LearnAPI.obs(model::ConstantClassifierFitted, data) = |
| 38 | + obs(model, data, front_end) |
| 39 | + |
| 40 | +# data deconstructors: |
| 41 | +LearnAPI.features(learner::ConstantClassifier, data) = |
| 42 | + LearnAPI.features(learner, data, front_end) |
| 43 | +LearnAPI.target(learner::ConstantClassifier, data) = |
| 44 | + LearnAPI.target(learner, data, front_end) |
| 45 | + |
| 46 | +function LearnAPI.fit(learner::ConstantClassifier, observations::FrontEnds.Obs; verbosity=1) |
| 47 | + y = observations.target # integer "codes" |
| 48 | + names = observations.names |
| 49 | + classes_seen = observations.classes_seen |
| 50 | + codes_seen = sort(unique(y)) |
| 51 | + decoder = observations.decoder |
| 52 | + |
| 53 | + d = proportionmap(y) |
| 54 | + # proportions ordered by key, i.e., by codes seen: |
| 55 | + probabilities = values(sort!(OrderedDict(d))) |> collect |
| 56 | + |
| 57 | + return ConstantClassifierFitted( |
| 58 | + learner, |
| 59 | + probabilities, |
| 60 | + names, |
| 61 | + classes_seen, |
| 62 | + codes_seen, |
| 63 | + decoder, |
| 64 | + ) |
| 65 | +end |
| 66 | +LearnAPI.fit(learner::ConstantClassifier, data; kwargs...) = |
| 67 | + fit(learner, obs(learner, data); kwargs...) |
| 68 | + |
| 69 | +function LearnAPI.predict( |
| 70 | + model::ConstantClassifierFitted, |
| 71 | + ::Point, |
| 72 | + observations::FrontEnds.Obs, |
| 73 | + ) |
| 74 | + n = MLCore.numobs(observations) |
| 75 | + idx = argmax(model.probabilities) |
| 76 | + code_of_mode = model.codes_seen[idx] |
| 77 | + return model.decoder.(fill(code_of_mode, n)) |
| 78 | +end |
| 79 | +LearnAPI.predict(model::ConstantClassifierFitted, ::Point, data) = |
| 80 | + predict(model, Point(), obs(model, data)) |
| 81 | + |
| 82 | +function LearnAPI.predict( |
| 83 | + model::ConstantClassifierFitted, |
| 84 | + ::Distribution, |
| 85 | + observations::FrontEnds.Obs, |
| 86 | + ) |
| 87 | + n = MLCore.numobs(observations) |
| 88 | + probs = model.probabilities |
| 89 | + # repeat vertically to get rows of a matrix: |
| 90 | + probs_matrix = reshape(repeat(probs, n), (length(probs), n))' |
| 91 | + return CategoricalDistributions.UnivariateFinite(model.classes_seen, probs_matrix) |
| 92 | +end |
| 93 | +LearnAPI.predict(model::ConstantClassifierFitted, ::Distribution, data) = |
| 94 | + predict(model, Distribution(), obs(model, data)) |
| 95 | + |
| 96 | +# accessor function: |
| 97 | +LearnAPI.feature_names(model::ConstantClassifierFitted) = model.names |
| 98 | + |
| 99 | +@trait( |
| 100 | + ConstantClassifier, |
| 101 | + constructor = ConstantClassifier, |
| 102 | + kinds_of_proxy = (Point(),Distribution()), |
| 103 | + tags = ("classification",), |
| 104 | + functions = ( |
| 105 | + :(LearnAPI.fit), |
| 106 | + :(LearnAPI.learner), |
| 107 | + :(LearnAPI.clone), |
| 108 | + :(LearnAPI.strip), |
| 109 | + :(LearnAPI.obs), |
| 110 | + :(LearnAPI.features), |
| 111 | + :(LearnAPI.target), |
| 112 | + :(LearnAPI.predict), |
| 113 | + :(LearnAPI.feature_names), |
| 114 | + ) |
| 115 | +) |
| 116 | + |
| 117 | +true |
0 commit comments