66> ` transform ` ). In this example we also implement an ** accessor function** , called
77> ` feature_importance ` , returning the absolute values of the linear coefficients. The
88> ridge regressor has a target variable and ` predict ` makes literal predictions of the
9- > target (rather than, say, probablistic predictions); this behaviour is flagged by the
10- > ` target_proxies ` model trait. Other traits articulate the model's training data type
9+ > target (rather than, say, probabilistic predictions); this behavior is flagged by the
10+ > ` predict_proxy ` model trait. Other traits articulate the model's training data type
1111> requirements and the input/output type of ` predict ` .
1212
1313We begin by describing an implementation of LearnAPI.jl for basic ridge
@@ -35,7 +35,7 @@ nothing # hide
3535```
3636
3737The subtyping ` MyRidge <: LearnAPI.Model ` is optional but recommended where it is not
38- otherwise disruptive (it allows models to be displayed in a standard way, for example) .
38+ otherwise disruptive.
3939
4040Instances of ` MyRidge ` are called ** models** and ` MyRidge ` is a ** model type** .
4141
@@ -75,7 +75,7 @@ function LearnAPI.fit(model::MyRidge, verbosity, X, y)
7575 feature_importances =
7676 [features[j] => abs(coefficients[j]) for j in eachindex(features)]
7777 sort!(feature_importances, by=last) |> reverse!
78- verbosity > 1 && @info "Features in order of importance: $(first.(feature_importances))"
78+ verbosity > 0 && @info "Features in order of importance: $(first.(feature_importances))"
7979 report = (; feature_importances)
8080
8181 return fitted_params, state, report
@@ -92,15 +92,15 @@ Regarding the return value of `fit`:
9292 or [ ` LearnAPI.ingest! ` ] ( @ref ) method (see [ Fit, update! and ingest!] ( @ref ) ).
9393
9494- The ` report ` is for other byproducts of training, apart from the learned parameters (the
95- ones will need to provide ` predict ` below).
95+ ones we'll need to provide ` predict ` below).
9696
97- Our ` fit ` method assumes that ` X ` is a table (satifies the [ Tables.jl
97+ Our ` fit ` method assumes that ` X ` is a table (satisfies the [ Tables.jl
9898spec] ( https://github.com/JuliaData/Tables.jl ) ) whose rows are the observations; and it
9999will need need ` y ` to be an ` AbstractFloat ` vector. A model implementation is free to
100100dictate the representation of data that ` fit ` accepts but articulates its requirements
101101using appropriate traits; see [ Training data types] ( @ref ) below. We recommend against data
102102type checks internal to ` fit ` ; this would ordinarily be the responsibility of a higher
103- level API, using those trasits .
103+ level API, using those traits .
104104
105105
106106## Operations
@@ -139,43 +139,39 @@ LearnAPI.feature_importances(::MyRidge, fitted_params, report) =
139139nothing # hide
140140```
141141
142- Another example of an accessor function is [ ` training_losses ` ] ( @ref ) .
142+ Another example of an accessor function is [ ` LearnAPI. training_losses` ] ( @ref ) .
143143
144144
145145## [ Model traits] (@id traits)
146146
147147Our model has a target variable, in the sense outlined in [ Scope and undefined
148148notions] (@ref scope), and ` predict ` returns an object with exactly the same form as the
149- target. We indicate this behaviour by declaring
149+ target. We indicate this behavior by declaring
150150
151151``` @example anatomy
152- LearnAPI.target_proxies (::Type{<:MyRidge}) = (; predict= LearnAPI.TrueTarget() )
152+ LearnAPI.predict_proxy (::Type{<:MyRidge}) = LearnAPI.TrueTarget()
153153nothing # hide
154154```
155155Or, you can use the shorthand
156156
157157``` @example anatomy
158- @trait MyRidge target_proxies = (; predict= LearnAPI.TrueTarget() )
158+ @trait MyRidge predict_proxy= LearnAPI.TrueTarget()
159159nothing # hide
160160```
161161
162162More generally, ` predict ` only returns a * proxy* for the target, such as probability
163163distributions, and we would make a different declaration here. See [ Target proxies] ( @ref )
164164for details.
165165
166- ` LearnAPI.target_proxies ` is an example of a ** model trait** . A complete list of traits
166+ ` LearnAPI.predict_proxy ` is an example of a ** model trait** . A complete list of traits
167167and the contracts they imply is given in [ Model Traits] ( @ref ) .
168168
169- > ** MLJ only.** The values of all traits constitute a model's ** metadata** , which is
170- > recorded in the searchable MLJ Model Registry, assuming the implementation-providing
171- > package is registered there.
172-
173169We also need to indicate that a target variable appears in training (this is a supervised
174170model). We do this by declaring * where* in the list of training data arguments (in this
175171case ` (X, y) ` ) the target variable (in this case ` y ` ) appears:
176172
177173``` @example anatomy
178- @trait MyRidge position_of_target = 2
174+ @trait MyRidge position_of_target= 2
179175nothing # hide
180176```
181177
@@ -184,7 +180,7 @@ As explained in the introduction, LearnAPI.jl does not attempt to define strict
184180descriptors, as in
185181
186182``` @example anatomy
187- @trait MyRidge descriptors = (:regression,)
183+ @trait MyRidge descriptors= (:regression,)
188184nothing # hide
189185```
190186
@@ -195,7 +191,7 @@ Finally, we are required to declare what methods (excluding traits) we have expl
195191overloaded for our type:
196192
197193``` @example anatomy
198- @trait MyRidge methods = (
194+ @trait MyRidge methods= (
199195 :fit,
200196 :predict,
201197 :feature_importances,
@@ -206,26 +202,25 @@ nothing # hide
206202## Training data types
207203
208204Since LearnAPI.jl is a basement level API, one is discouraged from including explicit type
209- checks in an implementation of ` fit ` . Instead one uses traits to make promisises about the
205+ checks in an implementation of ` fit ` . Instead one uses traits to make promises about the
210206acceptable type of ` data ` consumed by ` fit ` . In general, this can be a promise regarding
211- the ordinary type of ` data ` and/ or the [ scientific
212- type] ( https://github.com/JuliaAI/ScientificTypes.jl ) of ` data ` . Alternatively, one may
213- only make a promise about the type/scitype of * observations* in the data . See [ Model
214- Traits] ( @ref ) for further details. In this case we'll be happy to restrict the scitype of
215- the data:
207+ the ordinary type of ` data ` or the [ scientific
208+ type] ( https://github.com/JuliaAI/ScientificTypes.jl ) of ` data ` (but not
209+ both). Alternatively, one may only make a promise about the type/scitype of * observations*
210+ in the data . See [ Model Traits] ( @ref ) for further details. In this case we'll be happy to
211+ restrict the scitype of the data:
216212
217213``` @example anatomy
218214import ScientificTypesBase: scitype, Table, Continuous
219- @trait MyRidge fit_data_scitype = Tuple{Table(Continuous), AbstractVector{Continuous}}
215+ @trait MyRidge fit_scitype = Tuple{Table(Continuous), AbstractVector{Continuous}}
220216nothing # hide
221217```
222218
223219This is a contract that ` data ` is acceptable in the call ` fit(model, verbosity, data...) `
224220whenever
225221
226- ``` @example anatomy
222+ ``` julia
227223scitype (data) <: Tuple{Table(Continuous), AbstractVector{Continuous}}
228- nothing # hide
229224```
230225
231226Or, in other words:
@@ -239,33 +234,23 @@ Or, in other words:
239234 AbstractVector{Continuous}` - meaning that it is an abstract vector with ` <: AbstractFloat `
240235 elements.
241236
242- ## Input/output types for operations
237+ ## Input types for operations
243238
244- An optional promise that an operation, such as ` predict ` , returns an object of given
245- scientific type is articulated in this way:
239+ An optional promise about what ` data ` is guaranteed to work in a call like
240+ ` predict(model, fitted_params, data...) ` is articulated this way:
246241
247242``` @example anatomy
248- @trait output_scitypes = (; predict=AbstractVector{<:Continuous})
249- nothing # hide
243+ @trait MyRidge predict_input_scitype = Tuple{AbstractVector{<:Continuous}}
250244```
251245
252- If ` predict ` had instead returned probability distributions that implement the
253- ` Distributions.pdf ` interface, then one could instead make the declaration
246+ Note that ` data ` is always a ` Tuple ` , even if it has only one component ( the typical
247+ case), which explains the ` Tuple ` on the right-hand side.
254248
255- ``` julia
256- @trait MyRidge output_scitypes = (; predict= AbstractVector{Density{<: Continuous }})
257- ```
258-
259- Similarly, there exists a trait called [ ` output_type ` ] ( @ref ) for making promises on the
260- ordinary type resturned by an operation.
261-
262- Finally, we'll make a promise about what ` data ` is acceptable in a call like
263- ` predict(model, fitted_params, data...) ` . Note that ` data ` is always a ` Tuple ` , even if it
264- has only one component (the typical case).
249+ Optionally, we may express our promise using regular types, using the
250+ [ ` LearnAPI.predict_input_type ` ] ( @ref ) trait.
265251
266- ``` example anatomy
267- @trait MyRidge input_scitype = (; predict=Tuple{AbstractVector{<:Continuous}})
268- ```
252+ One can optionally make promises about the outut of an operation. See [ Model Traits] ( @ref )
253+ for details.
269254
270255## [ Illustrative fit/predict workflow] (@id workflow)
271256
@@ -283,21 +268,21 @@ X = (; a, b, c) |> Tables.rowtable
283268y = 2a - b + 3c + 0.05*rand(n)
284269nothing # hide
285270```
286- Instantiate a model with relevant hyperparameters:
271+ Instantiate a model with relevant hyperparameters (which is all the object stores) :
287272
288273``` @example anatomy
289274model = MyRidge(lambda=0.5)
290275```
291276
292- Train the model:
277+ Train the model (the ` 0 ` means do so silently) :
293278
294279``` @example anatomy
295280import LearnAPI: fit, predict, feature_importances
296281
297- fitted_params, state, fit_report = fit(model, 1 , X[train], y[train])
282+ fitted_params, state, fit_report = fit(model, 0 , X[train], y[train])
298283```
299284
300- Inspect the learned paramters and report:
285+ Inspect the learned parameters and report:
301286
302287``` @example anatomy
303288@info "training outcomes" fitted_params fit_report
0 commit comments