-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
97 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Defines a simple deterministic regressor for MLJ testing purposes | ||
# only. MLJ users should use RidgeRegressor from MultivariateStats. | ||
|
||
import MLJBase | ||
using LinearAlgebra | ||
|
||
export SimpleRidgeRegressor | ||
|
||
mutable struct SimpleRidgeRegressor <: MLJBase.Deterministic | ||
lambda::Float64 | ||
end | ||
|
||
function SimpleRidgeRegressor(; lambda=0.0) | ||
simpleridgemodel = SimpleRidgeRegressor(lambda) | ||
message = MLJBase.clean!(simpleridgemodel) | ||
isempty(message) || @warn message | ||
return simpleridgemodel | ||
end | ||
|
||
function MLJ.clean!(model::SimpleRidgeRegressor) | ||
warning = "" | ||
if model.lambda < 0 | ||
warning *= "Need lambda ≥ 0. Resetting lambda=0. " | ||
model.lambda = 0 | ||
end | ||
return warning | ||
end | ||
|
||
function MLJBase.fitted_params(::SimpleRidgeRegressor, fitresult) | ||
return (coefficients=fitresult) | ||
end | ||
|
||
function MLJBase.fit(model::SimpleRidgeRegressor, verbosity::Int, X, y) | ||
x = MLJBase.matrix(X) | ||
fitresult = (x'x - model.lambda*I)\(x'y) | ||
cache = nothing | ||
report = NamedTuple() | ||
return fitresult, cache, report | ||
end | ||
|
||
|
||
function MLJBase.predict(model::SimpleRidgeRegressor, fitresult, Xnew) | ||
x = MLJBase.matrix(Xnew) | ||
return x*fitresult | ||
end | ||
|
||
# to hide from models generated from calls to models() | ||
MLJBase.is_wrapper(::Type{<:SimpleRidgeRegressor}) = true | ||
|
||
|
||
# metadata: | ||
MLJBase.load_path(::Type{<:SimpleRidgeRegressor}) = "MLJ.SimpleRidgeRegressor" | ||
MLJBase.package_name(::Type{<:SimpleRidgeRegressor}) = "MLJ" | ||
MLJBase.package_uuid(::Type{<:SimpleRidgeRegressor}) = "" | ||
MLJBase.is_pure_julia(::Type{<:SimpleRidgeRegressor}) = true | ||
MLJBase.input_scitype_union(::Type{<:SimpleRidgeRegressor}) = MLJBase.Continuous | ||
MLJBase.target_scitype_union(::Type{<:SimpleRidgeRegressor}) = MLJBase.Continuous |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module TestSimpleRidgeRegressor | ||
|
||
using Test | ||
using MLJ | ||
|
||
# These are calculated values, the coefficients (parmeters) should be [1, -1 , 2] | ||
ip = [1 0 3 4 5; 2 1 3 -3 4; 0 1 -3 2 1] | ||
op = [-1, 1, -6, 11, 3] | ||
|
||
ip = MLJ.table(ip') | ||
|
||
model = MLJ.SimpleRidgeRegressor(lambda = 0.0) | ||
|
||
fitresult, report ,cache = MLJ.fit(model, 0, ip, op); | ||
|
||
@test MLJ.predict(model, fitresult, Float64[-1,0,2]')[1] ≈ 3 | ||
|
||
end | ||
true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters