Skip to content

Composite model API

Anthony Blaom, PhD edited this page Nov 20, 2019 · 1 revision

Simple composite model to wrap a deterministic regressor in a learned Box-Cox target transformation

The following is just a simple demonstration of the general syntax for constructing a composite model type. This particular example can be done much quicker with the @pipeline macro.

using MLJ 

@load RidgeRegressor pkg=MultivariateStats
@load KNNRegressor

Xs = source()              # no data required here
ys = source(kind=:target)  # no data required here

box = UnivariateBoxCoxTransformer()
box_ = machine(box, ys)
z = transform(box_, ys)

rgs = RidgeRegressor(lambda=0.1) 
rgs_ = machine(rgs, Xs, z)
zhat = predict(rgs_, Xs)

yhat = inverse_transform(box_, zhat)

# export model specification (learning network) as new model type
# `WrappedRegressor` and instantiate the type:
wrapped_rgs = @from_network WrappedRegressor(regressor=rgs) <= yhat

Inspect composite model instance:

julia> params(wrapped_rgs)
(regressor = (lambda = 0.1,),)

Swap out the regressor being wrapped and re-inspect:

julia> wrapped_rgs.regressor=KNNRegressor(K=7)
julia> params(wrapped_rgs)
julia> params(wrapped_rgs)
(regressor = (K = 7,
              algorithm = :kdtree,
              metric = Distances.Euclidean(0.0),
              leafsize = 10,
              reorder = true,
              weights = :uniform,),)

Evaluating the model against some data:

julia> X, y = @load_boston
julia> evaluate(wrapped_rgs, X, y, resampling=CV(), measure=rms, verbosity=0)
(measure = MLJBase.RMS[rms],
 measurement = [8.7132],
 per_fold = Array{Float64,1}[[7.51883, 9.40474, 10.2224, 9.23827, 10.1398, 4.22803]],
 per_observation = Missing[missing],)