Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update deprecated document example in "Transformers ..." section of manual #1040

Closed
ablaom opened this issue Aug 24, 2023 · 0 comments
Closed
Assignees

Comments

@ablaom
Copy link
Member

ablaom commented Aug 24, 2023

The DoubleRegressor example in the this section uses deprecated @from_network macro.

Here's an update to that example:

using MLJ
using MLJBase


# # THE AVERAGER (no changes)

mutable struct Averager <: Static
    mix::Float64
end

MLJ.transform(a::Averager, _, y1, y2) = (1 - a.mix)*y1 + a.mix*y2

# # THE COMPOSITE MODEL

mutable struct DoubleRegressor <: DeterministicNetworkComposite
       regressor1
       regressor2
       averager
end

function MLJBase.prefit(composite::DoubleRegressor, verbosity, X, y)
    Xs = source(X)
    ys = source(y)

    averager = Averager(0.5)

    mach0 = machine(OneHotEncoder(), Xs)
    W = transform(mach0, Xs) # one-hot encode the input

    mach1 = machine(:regressor1, W, ys)
    y1 = predict(mach1, W)

    mach2 = machine(:regressor2, W, ys)
    y2 = predict(mach2, W)

    mach3 = machine(:averager)
    yhat = transform(mach3, y1, y2)

    return (; predict=yhat)
end

Ridge = @load RidgeRegressor pkg=MultivariateStats
KNN = @load KNNRegressor


# # DEMONSTRATION

composite = DoubleRegressor(Ridge(), KNN(), Averager(0.5))
# DoubleRegressor(
#   regressor1 = RidgeRegressor(
#         lambda = 1.0,
#         bias = true),
#   regressor2 = KNNRegressor(
#         K = 5,
#         algorithm = :kdtree,
#         metric = Distances.Euclidean(0.0),
#         leafsize = 10,
#         reorder = true,
#         weights = NearestNeighborModels.Uniform()),
#   averager = Averager(
#         mix = 0.5))

composite.averager.mix = 0.25 # adjust mix from default of 0.5

evaluate(composite, (@load_reduced_ames)..., measure=rms)
# PerformanceEvaluation object with these fields:
#   model, measure, operation, measurement, per_fold,
#   per_observation, fitted_params_per_fold,
#   report_per_fold, train_test_rows, resampling, repeats
# Extract:
# ┌────────────────────────┬───────────┬─────────────┬─────────┬────────────────────────────────────────────────────────┐
# │ measure                │ operation │ measurement │ 1.96*SE │ per_fold                                               │
# ├────────────────────────┼───────────┼─────────────┼─────────┼────────────────────────────────────────────────────────┤
# │ RootMeanSquaredError() │ predict   │ 26800.0     │ 3280.0  │ [21400.0, 23700.0, 26800.0, 25900.0, 30800.0, 30700.0] │
# └────────────────────────┴───────────┴─────────────┴─────────┴────────────────────────────────────────────────────────┘
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants