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

Code cleanup #204

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 62 additions & 47 deletions src/builtins/Constant.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ struct ConstantRegressor{D<:Distributions.Sampleable} <: Probabilistic
distribution_type::Type{D}
end

ConstantRegressor(; distribution_type=Distributions.Normal) =
ConstantRegressor(; distribution_type = Distributions.Normal) =
ConstantRegressor(distribution_type)

function MLJBase.fit(::ConstantRegressor{D}, verbosity::Int, X, y) where D
function MLJBase.fit(::ConstantRegressor{D}, verbosity::Int, X, y) where {D}
fitresult = Distributions.fit(D, y)
cache = nothing
report = NamedTuple()
cache = nothing
report = NamedTuple()
return fitresult, cache, report
end

MLJBase.fitted_params(::ConstantRegressor, fitresult) = (target_distribution=fitresult,)
MLJBase.fitted_params(::ConstantRegressor, fitresult) = (target_distribution = fitresult,)

MLJBase.predict(::ConstantRegressor, fitresult, Xnew) = fill(fitresult, nrows(Xnew))

Expand All @@ -33,12 +33,13 @@ struct DeterministicConstantRegressor <: Deterministic end

function MLJBase.fit(::DeterministicConstantRegressor, verbosity::Int, X, y)
fitresult = mean(y)
cache = nothing
report = NamedTuple()
cache = nothing
report = NamedTuple()
return fitresult, cache, report
end

MLJBase.predict(::DeterministicConstantRegressor, fitresult, Xnew) = fill(fitresult, nrows(Xnew))
MLJBase.predict(::DeterministicConstantRegressor, fitresult, Xnew) =
fill(fitresult, nrows(Xnew))

##
## THE CONSTANT CLASSIFIER
Expand All @@ -56,14 +57,14 @@ obtain the training target mode instead.
struct ConstantClassifier <: Probabilistic end

# here `args` is `y` or `y, w`:
function MLJBase.fit(::ConstantClassifier, verbosity::Int, X, y, w=nothing)
function MLJBase.fit(::ConstantClassifier, verbosity::Int, X, y, w = nothing)
fitresult = Distributions.fit(MLJBase.UnivariateFinite, y, w)
cache = nothing
report = NamedTuple
cache = nothing
report = NamedTuple
return fitresult, cache, report
end

MLJBase.fitted_params(::ConstantClassifier, fitresult) = (target_distribution=fitresult,)
MLJBase.fitted_params(::ConstantClassifier, fitresult) = (target_distribution = fitresult,)

MLJBase.predict(::ConstantClassifier, fitresult, Xnew) = fill(fitresult, nrows(Xnew))

Expand All @@ -76,51 +77,65 @@ struct DeterministicConstantClassifier <: Deterministic end
function MLJBase.fit(::DeterministicConstantClassifier, verbosity::Int, X, y)
# dump missing target values and make into a regular array:
fitresult = mode(skipmissing(y) |> collect) # a CategoricalValue or CategoricalString
cache = nothing
report = NamedTuple()
cache = nothing
report = NamedTuple()
return fitresult, cache, report
end

MLJBase.predict(::DeterministicConstantClassifier, fitresult, Xnew) = fill(fitresult, nrows(Xnew))
MLJBase.predict(::DeterministicConstantClassifier, fitresult, Xnew) =
fill(fitresult, nrows(Xnew))

##
## METADATA
##

metadata_pkg.(
(ConstantRegressor, ConstantClassifier,
DeterministicConstantRegressor, DeterministicConstantClassifier),
name = "MLJModels",
uuid = "d491faf4-2d78-11e9-2867-c94bc002c0b7",
url = "https://github.com/alan-turing-institute/MLJModels.jl",
julia = true,
license = "MIT",
is_wrapper = false)

metadata_model(ConstantRegressor,
input = Table(Scientific),
target = AbstractVector{Continuous},
(
ConstantRegressor,
ConstantClassifier,
DeterministicConstantRegressor,
DeterministicConstantClassifier,
),
name = "MLJModels",
uuid = "d491faf4-2d78-11e9-2867-c94bc002c0b7",
url = "https://github.com/alan-turing-institute/MLJModels.jl",
julia = true,
license = "MIT",
is_wrapper = false,
)

metadata_model(
ConstantRegressor,
input = Table(Scientific),
target = AbstractVector{Continuous},
weights = false,
descr = "Constant regressor (Probabilistic).",
path = "MLJModels.ConstantRegressor")

metadata_model(DeterministicConstantRegressor,
input = Table(Scientific),
target = AbstractVector{Continuous},
descr = "Constant regressor (Probabilistic).",
path = "MLJModels.ConstantRegressor",
)

metadata_model(
DeterministicConstantRegressor,
input = Table(Scientific),
target = AbstractVector{Continuous},
weights = false,
descr = "Constant regressor (Deterministic).",
path = "MLJModels.DeterministicConstantRegressor")

metadata_model(ConstantClassifier,
input = Table(Scientific),
target = AbstractVector{<:Finite},
descr = "Constant regressor (Deterministic).",
path = "MLJModels.DeterministicConstantRegressor",
)

metadata_model(
ConstantClassifier,
input = Table(Scientific),
target = AbstractVector{<:Finite},
weights = true,
descr = "Constant classifier (Probabilistic).",
path = "MLJModels.ConstantClassifier")

metadata_model(DeterministicConstantClassifier,
input = Table(Scientific),
target = AbstractVector{<:Finite},
descr = "Constant classifier (Probabilistic).",
path = "MLJModels.ConstantClassifier",
)

metadata_model(
DeterministicConstantClassifier,
input = Table(Scientific),
target = AbstractVector{<:Finite},
weights = false,
descr = "Constant classifier (Deterministic).",
path = "MLJModels.DeterministicConstantClassifier")
descr = "Constant classifier (Deterministic).",
path = "MLJModels.DeterministicConstantClassifier",
)