-
Notifications
You must be signed in to change notification settings - Fork 7
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
MLJ extension #61
Merged
Merged
MLJ extension #61
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7935484
basic MLJ extension
tjjarvinen 2a02ea3
Update Project.toml
tjjarvinen b9aeb77
Add instruction on how to use MLJ
tjjarvinen 15b739d
add documentation strings
tjjarvinen 44f3ad1
add warnings to old SKLear to inform coming change to MLJ
tjjarvinen 6f5d589
fix tests and parameter extraction
tjjarvinen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Conceptually, does it make sense to recommend that MLJ is loaded first?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Loading order does not matter, so I would leave it as it is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example in
ext/ACEfit_MLJLinearModels_ext.jl
loads MLJ first - can we make them consistent?