-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from tjjarvinen/mlj
MLJ extension
- Loading branch information
Showing
8 changed files
with
188 additions
and
1 deletion.
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,53 @@ | ||
module ACEfit_MLJLinearModels_ext | ||
|
||
using MLJ | ||
using ACEfit | ||
using MLJLinearModels | ||
|
||
""" | ||
ACEfit.solve(solver, A, y) | ||
Overloads `ACEfit.solve` to use MLJLinearModels solvers, | ||
when `solver` is [MLJLinearModels](https://github.com/JuliaAI/MLJLinearModels.jl) solver. | ||
# Example | ||
```julia | ||
using MLJ | ||
using ACEfit | ||
# Load Lasso solver | ||
LassoRegressor = @load LassoRegressor pkg=MLJLinearModels | ||
# Create the solver itself and give it parameters | ||
solver = LassoRegressor( | ||
lambda = 0.2, | ||
fit_intercept = false | ||
# insert more fit params | ||
) | ||
# fit ACE model | ||
linear_fit(training_data, basis, solver) | ||
# or lower level | ||
ACEfit.fit(solver, A, y) | ||
``` | ||
""" | ||
function ACEfit.solve(solver::Union{ | ||
MLJLinearModels.ElasticNetRegressor, | ||
MLJLinearModels.HuberRegressor, | ||
MLJLinearModels.LADRegressor, | ||
MLJLinearModels.LassoRegressor, | ||
MLJLinearModels.LinearRegressor, | ||
MLJLinearModels.QuantileRegressor, | ||
MLJLinearModels.RidgeRegressor, | ||
MLJLinearModels.RobustRegressor, | ||
}, | ||
A, y) | ||
Atable = MLJ.table(A) | ||
mach = machine(solver, Atable, y) | ||
MLJ.fit!(mach) | ||
params = fitted_params(mach) | ||
return Dict{String, Any}("C" => map( x->x.second, params.coefs) ) | ||
end | ||
|
||
end |
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,46 @@ | ||
module ACEfit_MLJScikitLearnInterface_ext | ||
|
||
using ACEfit | ||
using MLJ | ||
using MLJScikitLearnInterface | ||
using PythonCall | ||
|
||
|
||
""" | ||
ACEfit.solve(solver, A, y) | ||
Overloads `ACEfit.solve` to use scikitlearn solvers from MLJ. | ||
# Example | ||
```julia | ||
using MLJ | ||
using ACEfit | ||
# Load ARD solver | ||
ARDRegressor = @load ARDRegressor pkg=MLJScikitLearnInterface | ||
# Create the solver itself and give it parameters | ||
solver = ARDRegressor( | ||
n_iter = 300, | ||
tol = 1e-3, | ||
threshold_lambda = 10000 | ||
# more params | ||
) | ||
# fit ACE model | ||
linear_fit(training_data, basis, solver) | ||
# or lower level | ||
ACEfit.fit(solver, A, y) | ||
``` | ||
""" | ||
function ACEfit.solve(solver, A, y) | ||
Atable = MLJ.table(A) | ||
mach = machine(solver, Atable, y) | ||
MLJ.fit!(mach) | ||
params = fitted_params(mach) | ||
c = params.coef | ||
return Dict{String, Any}("C" => pyconvert(Array, c) ) | ||
end | ||
|
||
end |
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
[deps] | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
MLJ = "add582a8-e3ab-11e8-2d5e-e98b27df1bc7" | ||
MLJLinearModels = "6ee0df7b-362f-4a72-a706-9e79364fb692" | ||
MLJScikitLearnInterface = "5ae90465-5518-4432-b9d2-8a1def2f0cab" | ||
PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" |
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,42 @@ | ||
using ACEfit | ||
using LinearAlgebra | ||
using MLJ | ||
using MLJScikitLearnInterface | ||
|
||
@info("Test MLJ interface on overdetermined system") | ||
Nobs = 10_000 | ||
Nfeat = 100 | ||
A = randn(Nobs, Nfeat) / sqrt(Nobs) | ||
y = randn(Nobs) | ||
P = Diagonal(1.0 .+ rand(Nfeat)) | ||
|
||
|
||
@info(" ... MLJLinearModels LinearRegressor") | ||
LinearRegressor = @load LinearRegressor pkg=MLJLinearModels | ||
solver = LinearRegressor() | ||
results = ACEfit.solve(solver, A, y) | ||
C = results["C"] | ||
@show norm(A * C - y) | ||
@show norm(C) | ||
|
||
|
||
@info(" ... MLJLinearModels LassoRegressor") | ||
LassoRegressor = @load LassoRegressor pkg=MLJLinearModels | ||
solver = LassoRegressor() | ||
results = ACEfit.solve(solver, A, y) | ||
C = results["C"] | ||
@show norm(A * C - y) | ||
@show norm(C) | ||
|
||
|
||
@info(" ... MLJ SKLearn ARD") | ||
ARDRegressor = @load ARDRegressor pkg=MLJScikitLearnInterface | ||
solver = ARDRegressor( | ||
n_iter = 300, | ||
tol = 1e-3, | ||
threshold_lambda = 10000 | ||
) | ||
results = ACEfit.solve(solver, A, y) | ||
C = results["C"] | ||
@show norm(A * C - y) | ||
@show norm(C) |