|
| 1 | +# [Code for ridge example](@id code) |
| 2 | + |
| 3 | +Below is the complete source code for the ridge implementations described in the tutorial, |
| 4 | +[Anatomy of an Implementation](@ref). |
| 5 | + |
| 6 | +- [Basic implementation](@ref) |
| 7 | +- [Implementation with data front end](@ref) |
| 8 | + |
| 9 | + |
| 10 | +## Basic implementation |
| 11 | + |
| 12 | +```julia |
| 13 | +using LearnAPI |
| 14 | +using LinearAlgebra, Tables |
| 15 | + |
| 16 | +struct Ridge{T<:Real} |
| 17 | + lambda::T |
| 18 | +end |
| 19 | + |
| 20 | +""" |
| 21 | + Ridge(; lambda=0.1) |
| 22 | +
|
| 23 | +Instantiate a ridge regression learner, with regularization of `lambda`. |
| 24 | +""" |
| 25 | +Ridge(; lambda=0.1) = Ridge(lambda) |
| 26 | +LearnAPI.constructor(::Ridge) = Ridge |
| 27 | + |
| 28 | +# struct for output of `fit` |
| 29 | +struct RidgeFitted{T,F} |
| 30 | + learner::Ridge |
| 31 | + coefficients::Vector{T} |
| 32 | + named_coefficients::F |
| 33 | +end |
| 34 | + |
| 35 | +function LearnAPI.fit(learner::Ridge, data; verbosity=1) |
| 36 | + X, y = data |
| 37 | + |
| 38 | + # data preprocessing: |
| 39 | + table = Tables.columntable(X) |
| 40 | + names = Tables.columnnames(table) |> collect |
| 41 | + A = Tables.matrix(table, transpose=true) |
| 42 | + |
| 43 | + lambda = learner.lambda |
| 44 | + |
| 45 | + # apply core algorithm: |
| 46 | + coefficients = (A*A' + learner.lambda*I)\(A*y) # vector |
| 47 | + |
| 48 | + # determine named coefficients: |
| 49 | + named_coefficients = [names[j] => coefficients[j] for j in eachindex(names)] |
| 50 | + |
| 51 | + # make some noise, if allowed: |
| 52 | + verbosity > 0 && @info "Coefficients: $named_coefficients" |
| 53 | + |
| 54 | + return RidgeFitted(learner, coefficients, named_coefficients) |
| 55 | +end |
| 56 | + |
| 57 | +LearnAPI.predict(model::RidgeFitted, ::Point, Xnew) = |
| 58 | + Tables.matrix(Xnew)*model.coefficients |
| 59 | + |
| 60 | +# accessor functions: |
| 61 | +LearnAPI.learner(model::RidgeFitted) = model.learner |
| 62 | +LearnAPI.coefficients(model::RidgeFitted) = model.named_coefficients |
| 63 | +LearnAPI.strip(model::RidgeFitted) = |
| 64 | + RidgeFitted(model.learner, model.coefficients, nothing) |
| 65 | + |
| 66 | +@trait( |
| 67 | + Ridge, |
| 68 | + constructor = Ridge, |
| 69 | + kinds_of_proxy=(Point(),), |
| 70 | + tags = ("regression",), |
| 71 | + functions = ( |
| 72 | + :(LearnAPI.fit), |
| 73 | + :(LearnAPI.learner), |
| 74 | + :(LearnAPI.clone), |
| 75 | + :(LearnAPI.strip), |
| 76 | + :(LearnAPI.obs), |
| 77 | + :(LearnAPI.features), |
| 78 | + :(LearnAPI.target), |
| 79 | + :(LearnAPI.predict), |
| 80 | + :(LearnAPI.coefficients), |
| 81 | + ) |
| 82 | +) |
| 83 | + |
| 84 | +# convenience method: |
| 85 | +LearnAPI.fit(learner::Ridge, X, y; kwargs...) = fit(learner, (X, y); kwargs...) |
| 86 | +``` |
| 87 | + |
| 88 | +# Implementation with data front end |
| 89 | + |
| 90 | +```julia |
| 91 | +using LearnAPI |
| 92 | +using LinearAlgebra, Tables |
| 93 | + |
| 94 | +struct Ridge{T<:Real} |
| 95 | + lambda::T |
| 96 | +end |
| 97 | + |
| 98 | +Ridge(; lambda=0.1) = Ridge(lambda) |
| 99 | + |
| 100 | +# struct for output of `fit`: |
| 101 | +struct RidgeFitted{T,F} |
| 102 | + learner::Ridge |
| 103 | + coefficients::Vector{T} |
| 104 | + named_coefficients::F |
| 105 | +end |
| 106 | + |
| 107 | +# struct for internal representation of training data: |
| 108 | +struct RidgeFitObs{T,M<:AbstractMatrix{T}} |
| 109 | + A::M # `p` x `n` matrix |
| 110 | + names::Vector{Symbol} # features |
| 111 | + y::Vector{T} # target |
| 112 | +end |
| 113 | + |
| 114 | +# implementation of `RandomAccess()` data interface for such representation: |
| 115 | +Base.getindex(data::RidgeFitObs, I) = |
| 116 | + RidgeFitObs(data.A[:,I], data.names, y[I]) |
| 117 | +Base.length(data::RidgeFitObs) = length(data.y) |
| 118 | + |
| 119 | +# data front end for `fit`: |
| 120 | +function LearnAPI.obs(::Ridge, data) |
| 121 | + X, y = data |
| 122 | + table = Tables.columntable(X) |
| 123 | + names = Tables.columnnames(table) |> collect |
| 124 | + return RidgeFitObs(Tables.matrix(table)', names, y) |
| 125 | +end |
| 126 | +LearnAPI.obs(::Ridge, observations::RidgeFitObs) = observations |
| 127 | + |
| 128 | +function LearnAPI.fit(learner::Ridge, observations::RidgeFitObs; verbosity=1) |
| 129 | + |
| 130 | + lambda = learner.lambda |
| 131 | + |
| 132 | + A = observations.A |
| 133 | + names = observations.names |
| 134 | + y = observations.y |
| 135 | + |
| 136 | + # apply core learner: |
| 137 | + coefficients = (A*A' + learner.lambda*I)\(A*y) # 1 x p matrix |
| 138 | + |
| 139 | + # determine named coefficients: |
| 140 | + named_coefficients = [names[j] => coefficients[j] for j in eachindex(names)] |
| 141 | + |
| 142 | + # make some noise, if allowed: |
| 143 | + verbosity > 0 && @info "Coefficients: $named_coefficients" |
| 144 | + |
| 145 | + return RidgeFitted(learner, coefficients, named_coefficients) |
| 146 | + |
| 147 | +end |
| 148 | + |
| 149 | +LearnAPI.fit(learner::Ridge, data; kwargs...) = |
| 150 | + fit(learner, obs(learner, data); kwargs...) |
| 151 | + |
| 152 | +# data front end for `predict`: |
| 153 | +LearnAPI.obs(::RidgeFitted, Xnew) = Tables.matrix(Xnew)' |
| 154 | +LearnAPI.obs(::RidgeFitted, observations::AbstractArray) = observations # involutivity |
| 155 | + |
| 156 | +LearnAPI.predict(model::RidgeFitted, ::Point, observations::AbstractMatrix) = |
| 157 | + observations'*model.coefficients |
| 158 | + |
| 159 | +LearnAPI.predict(model::RidgeFitted, ::Point, Xnew) = |
| 160 | + predict(model, Point(), obs(model, Xnew)) |
| 161 | + |
| 162 | +# methods to deconstruct training data: |
| 163 | +LearnAPI.features(::Ridge, observations::RidgeFitObs) = observations.A |
| 164 | +LearnAPI.target(::Ridge, observations::RidgeFitObs) = observations.y |
| 165 | +LearnAPI.features(learner::Ridge, data) = LearnAPI.features(learner, obs(learner, data)) |
| 166 | +LearnAPI.target(learner::Ridge, data) = LearnAPI.target(learner, obs(learner, data)) |
| 167 | + |
| 168 | +# accessor functions: |
| 169 | +LearnAPI.learner(model::RidgeFitted) = model.learner |
| 170 | +LearnAPI.coefficients(model::RidgeFitted) = model.named_coefficients |
| 171 | +LearnAPI.strip(model::RidgeFitted) = |
| 172 | + RidgeFitted(model.learner, model.coefficients, nothing) |
| 173 | + |
| 174 | +@trait( |
| 175 | + Ridge, |
| 176 | + constructor = Ridge, |
| 177 | + kinds_of_proxy=(Point(),), |
| 178 | + tags = ("regression",), |
| 179 | + functions = ( |
| 180 | + :(LearnAPI.fit), |
| 181 | + :(LearnAPI.learner), |
| 182 | + :(LearnAPI.clone), |
| 183 | + :(LearnAPI.strip), |
| 184 | + :(LearnAPI.obs), |
| 185 | + :(LearnAPI.features), |
| 186 | + :(LearnAPI.target), |
| 187 | + :(LearnAPI.predict), |
| 188 | + :(LearnAPI.coefficients), |
| 189 | + ) |
| 190 | +) |
| 191 | + |
| 192 | +``` |
0 commit comments