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