|
| 1 | +export LinearRegressor, RidgeRegressor, LassoRegressor, ElasticNetRegressor, |
| 2 | + RobustRegressor, HuberRegressor, QuantileRegressor, LADRegressor, |
| 3 | + LogisticClassifier, MultinomialClassifier |
| 4 | + |
| 5 | +include("regressors.jl") |
| 6 | +include("classifiers.jl") |
| 7 | + |
| 8 | +const REG_MODELS = (LinearRegressor, RidgeRegressor, LassoRegressor, ElasticNetRegressor, |
| 9 | + RobustRegressor, HuberRegressor, QuantileRegressor, LADRegressor) |
| 10 | +const CLF_MODELS = (LogisticClassifier, MultinomialClassifier) |
| 11 | +const ALL_MODELS = (REG_MODELS..., CLF_MODELS...) |
| 12 | + |
| 13 | +#= ========== |
| 14 | + REGRESSORS |
| 15 | + ========== =# |
| 16 | + |
| 17 | +function MLJBase.fit(m::Union{REG_MODELS...}, verb::Int, X, y) |
| 18 | + Xmatrix = MLJBase.matrix(X) |
| 19 | + reg = glr(m) |
| 20 | + solver = m.solver === nothing ? _solver(reg, size(Xmatrix)) : m.solver |
| 21 | + # get the parameters |
| 22 | + θ = fit(reg, Xmatrix, y; solver=solver) |
| 23 | + # return |
| 24 | + return θ, nothing, NamedTuple{}() |
| 25 | +end |
| 26 | + |
| 27 | +MLJBase.predict(m::Union{REG_MODELS...}, θ, Xnew) = apply_X(MLJBase.matrix(Xnew), θ) |
| 28 | + |
| 29 | +function MLJBase.fitted_params(m::Union{REG_MODELS...}, θ) |
| 30 | + m.fit_intercept && return (coefs = θ[1:end-1], intercept = θ[end]) |
| 31 | + return (coefs = θ, intercept = nothing) |
| 32 | +end |
| 33 | + |
| 34 | +#= =========== |
| 35 | + CLASSIFIERS |
| 36 | + =========== =# |
| 37 | + |
| 38 | +function MLJBase.fit(m::Union{CLF_MODELS...}, verb::Int, X, y) |
| 39 | + Xmatrix = MLJBase.matrix(X) |
| 40 | + yplain = convert.(Int, MLJBase.int(y)) |
| 41 | + classes = MLJBase.classes(y[1]) |
| 42 | + nclasses = length(classes) |
| 43 | + if nclasses == 2 |
| 44 | + # recode |
| 45 | + yplain[yplain .== 1] .= -1 |
| 46 | + yplain[yplain .== 2] .= 1 |
| 47 | + c = 1 |
| 48 | + else |
| 49 | + c = nclasses |
| 50 | + end |
| 51 | + clf = glr(m) |
| 52 | + solver = m.solver === nothing ? _solver(clf, size(Xmatrix)) : m.solver |
| 53 | + # get the parameters |
| 54 | + θ = fit(clf, Xmatrix, yplain, solver=solver) |
| 55 | + # return |
| 56 | + return (θ, c, classes), nothing, NamedTuple{}() |
| 57 | +end |
| 58 | + |
| 59 | +function MLJBase.predict(m::Union{CLF_MODELS...}, (θ, c, classes), Xnew) |
| 60 | + Xmatrix = MLJBase.matrix(Xnew) |
| 61 | + preds = apply_X(Xmatrix, θ, c) |
| 62 | + # binary classification |
| 63 | + if c == 1 |
| 64 | + preds .= sigmoid.(preds) |
| 65 | + preds = hcat(1.0 .- preds, preds) # scores for -1 and 1 |
| 66 | + return [MLJBase.UnivariateFinite(classes, preds[i, :]) for i in 1:size(Xmatrix,1)] |
| 67 | + end |
| 68 | + # multiclass |
| 69 | + preds .= softmax(preds) |
| 70 | + return [MLJBase.UnivariateFinite(classes, preds[i, :]) for i in 1:size(Xmatrix,1)] |
| 71 | +end |
| 72 | + |
| 73 | +function MLJBase.fitted_params(m::Union{CLF_MODELS...}, (θ, c, classes)) |
| 74 | + if c > 1 |
| 75 | + if m.fit_intercept |
| 76 | + W = reshape(θ, div(length(θ), c), c) |
| 77 | + return (coefs = W, intercept = nothing) |
| 78 | + end |
| 79 | + W = reshape(θ, p+1, c) |
| 80 | + return (coefs = W[1:p, :], intercept = W[end, :]) |
| 81 | + end |
| 82 | + # single class |
| 83 | + m.fit_intercept && return (coefs = θ[1:end-1], intercept = θ[end]) |
| 84 | + return (coefs = θ, intercept = nothing) |
| 85 | +end |
| 86 | + |
| 87 | +#= ======================= |
| 88 | + METADATA FOR ALL MODELS |
| 89 | + ======================= =# |
| 90 | + |
| 91 | +MLJBase.metadata_pkg.(ALL_MODELS, |
| 92 | + name="MLJLinearModels", |
| 93 | + uuid="6ee0df7b-362f-4a72-a706-9e79364fb692", |
| 94 | + url="https://github.com/alan-turing-institute/MLJLinearModels.jl", |
| 95 | + julia=true, |
| 96 | + license="MIT", |
| 97 | + is_wrapper=false) |
| 98 | + |
| 99 | +descr_(M) = descr(M) * |
| 100 | + "\n→ based on [MLJLinearModels](https://github.com/alan-turing-institute/MLJLinearModels.jl)" * |
| 101 | + "\n→ do `@load $(MLJBase.name(M)) pkg=\"MLJLinearModels\" to use the model.`" * |
| 102 | + "\n→ do `?$(MLJBase.name(M))` for documentation." |
| 103 | +lp_(M) = "MLJLinearModels.$(MLJBase.name(M))" |
| 104 | + |
| 105 | +for M in REG_MODELS |
| 106 | + MLJBase.metadata_model(M, |
| 107 | + input=MLJBase.Table(MLJBase.Continuous), |
| 108 | + target=AbstractVector{MLJBase.Continuous}, |
| 109 | + weights=false, |
| 110 | + descr=descr_(M), path=lp_(M)) |
| 111 | +end |
| 112 | +for M in CLF_MODELS |
| 113 | + MLJBase.metadata_model(M, |
| 114 | + input=MLJBase.Table(MLJBase.Continuous), |
| 115 | + target=AbstractVector{<:MLJBase.Finite}, |
| 116 | + weights=false, |
| 117 | + descr=descr_(M), path=lp_(M)) |
| 118 | +end |
0 commit comments