Example 2 – the ScikitLearn.jl package

Let's also take a look at the ScikitLearn.jl package, which defines a consistent API for fitting machine learning models and doing prediction. 

The following is how the FitBit type is defined:

""" `FitBit(model)` will behave just like `model`, but also supports
`isfit(fb)`, which returns true IFF `fit!(model, ...)` has been called """
mutable struct FitBit
model
isfit::Bool
FitBit(model) = new(model, false)
end

function fit!(fb::FitBit, args...; kwargs...)
fit!(fb.model, args...; kwargs...)
fb.isfit = true
fb
end

isfit(fb::FitBit) = fb.isfit

Here, we can see that the FitBit object contains a model object and that it adds a new functionality that keeps track of whether a model has been fitted or not:


@forward FitBit.model transform, predict, predict_proba, predict_dist, get_classes

It uses the @forward macro to delegate all the major functions, that is, transform, predict, and so on.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.253.152