# MLJ w/Scikitlearn: passing return\_std to predict

**URL:** <https://discourse.julialang.org/t/mlj-w-scikitlearn-passing-return-std-to-predict/91654>\
**Category:** Machine Learning\
**Tags:** mlj, scikitlearn\
**Created:** [December 14, 2022, 7:52pm UTC](https://discourse.julialang.org/t/mlj-w-scikitlearn-passing-return-std-to-predict/91654 "2022-12-14T19:52:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![evolbio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evolbio/32/30793_2.png) [@evolbio](https://discourse.julialang.org/u/evolbio)\
**Post date:** [December 14, 2022, 7:52pm UTC](https://discourse.julialang.org/t/mlj-w-scikitlearn-passing-return-std-to-predict/91654/1 "2022-12-14T19:52:26Z")

</div>

Various Scikitlearn models accept return\_std=true when calling predict, for example BayesianRidgeRegressor, see [this example](https://scikit-learn.org/stable/auto_examples/linear_model/plot_bayesian_ridge_curvefit.html#plot-the-true-and-predicted-curves-with-log-marginal-likelihood-l). For example, with a BayesianRidgeRegressor or similar machine, I would like to call  
`y_predict, y_std = predict(machine, X, return_std=true)`  
I have looked through ScikitLearn.jl and MLJScikitLearnInterface.jl and do not see anyway to make this work, but maybe I am missing something simple like the right way to pass additional arguments? Thanks.

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [December 15, 2022, 3:44pm UTC](https://discourse.julialang.org/t/mlj-w-scikitlearn-passing-return-std-to-predict/91654/2 "2022-12-15T15:44:48Z")

</div>

You’re not missing something, there’s currently no way to pass that argument. It might be good to open an issue at MLJScikitLearnInterface to discuss this (and you could paste what follows).

I doubt that MLJ’s `predict` signature will be adapted to match this one but I’ll let @ablaom or @samuel_okon discuss that).

What could work is to pass the `return_std` as a new field of `BayesianRidgeRegressor` here [MLJScikitLearnInterface.jl/linear-regressors.jl at 36882f14321e7e9889aac31447eeed0102eb052f · JuliaAI/MLJScikitLearnInterface.jl · GitHub](https://github.com/JuliaAI/MLJScikitLearnInterface.jl/blob/36882f14321e7e9889aac31447eeed0102eb052f/src/models/linear-regressors.jl#L37)

then pick that up at predict time here [MLJScikitLearnInterface.jl/macros.jl at 36882f14321e7e9889aac31447eeed0102eb052f · JuliaAI/MLJScikitLearnInterface.jl · GitHub](https://github.com/JuliaAI/MLJScikitLearnInterface.jl/blob/36882f14321e7e9889aac31447eeed0102eb052f/src/macros.jl#L197)

this would also require ScikitLearn.jl to allow passing a `return_std=true` to `predict`, that might also require opening an issue there cc @cstjean

---

<div class="post-metadata">

**Author:** ![evolbio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evolbio/32/30793_2.png) [@evolbio](https://discourse.julialang.org/u/evolbio)\
**Post date:** [December 15, 2022, 3:56pm UTC](https://discourse.julialang.org/t/mlj-w-scikitlearn-passing-return-std-to-predict/91654/3 "2022-12-15T15:56:52Z")

</div>

Very helpful. I will follow up as suggested.

---

<div class="post-metadata">

**Author:** ![evolbio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evolbio/32/30793_2.png) [@evolbio](https://discourse.julialang.org/u/evolbio)\
**Post date:** [December 16, 2022, 9:56pm UTC](https://discourse.julialang.org/t/mlj-w-scikitlearn-passing-return-std-to-predict/91654/4 "2022-12-16T21:56:22Z")

</div>

@tlienart posted on the [ScikitLearn.jl site](https://github.com/cstjean/ScikitLearn.jl/issues/117#issuecomment-1354416770) showing that ScikitLearn already supports the `return_std=true` arg via kwargs. So perhaps the only barrier to using this from MLJ is to get MLJScikitLearnInterface.jl to support the call. I posted [an issue on the MLJScikitLearnInterface site](https://github.com/JuliaAI/MLJScikitLearnInterface.jl/issues/46#issue-1498700897) and it looks like sometime in January the package maintainer will look into this. So eventually there may be a fix. Meanwhile if anyone has a suggestion for a workaround for making the call from MLJ, I would be happy to hear about that. Thanks.

PS: Other than directly calling ScikitLearn.jl, which I will try, but would rather have a call within MLJ.

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [December 19, 2022, 8:27am UTC](https://discourse.julialang.org/t/mlj-w-scikitlearn-passing-return-std-to-predict/91654/5 "2022-12-19T08:27:28Z")

</div>

the pure MLJ path is likely not going to be trivial though I’m sure Anthony will provide good input on that front in January. The difficulty is that it would potentially require a change in the API (to allow kwargs to be passed to `MLJBase.predict` and the semantics of `return_std` are a bit weird so it would require a change that would potentially not generalise very well.

Another approach would be to add a `BayesianRidge ` _probabilistic_ model to MLJScikitLearn and make it return gaussian densities from which you can extract `std`, probably the cleanest path.

Lastly, since you were asking for a workaround, this `std` is very easy to compute: [scikit-learn/\_bayes.py at dc580a8ef5ee2a8aea80498388690e2213118efd · scikit-learn/scikit-learn · GitHub](https://github.com/scikit-learn/scikit-learn/blob/dc580a8ef5ee2a8aea80498388690e2213118efd/sklearn/linear_model/_bayes.py#L353-L358) so in the meantime you could just mimic that. The `fitted_params(model)` will give you e.g. the `alpha` etc that you need for the computation.

---

<div class="post-metadata">

**Author:** ![evolbio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evolbio/32/30793_2.png) [@evolbio](https://discourse.julialang.org/u/evolbio)\
**Post date:** [December 19, 2022, 1:51pm UTC](https://discourse.julialang.org/t/mlj-w-scikitlearn-passing-return-std-to-predict/91654/6 "2022-12-19T13:51:07Z")

</div>

Thank you, I appreciate all of the help you have given on this. I agree that all of the Bayesian routines in SLK should probabilistic models in MLJ, that would be best in the long run.

For now, I managed to call ScikitLearn.jl directly. In doing that, I found that calling ScilearnKit.jl is easy from REPL, but to make the call from a function in the way that I needed, I had to resort to an obscure workaround:

> [@Error when calling ScikitLearn from a function in a Package](https://discourse.julialang.org/t/error-when-calling-scikitlearn-from-a-function-in-a-package/46131/8):
>
> Thanks! This works, using ScikitLearn, PyCall const LogisticRegression = PyNULL() function \_\_init\_\_() @eval @sk\_import linear\_model: LogisticRegression end function logistic\_skl(points::AbstractMatrix{\<:Real}, labels::AbstractVector{Bool}) log\_reg = fit!(LogisticRegression(penalty="l2"), points', labels) w = vec(log\_reg.coef\_) b = only(log\_reg.intercept\_) return w, b end However, I do get a warning from modifying the const: WARNING: redefinition of constant LogisticReg…

Perhaps there is some other way, but I could not find it. It seems strange that something as basic as calling via a function requires an undocumented hack. In any case, I have a simple workaround for now that allows me to move ahead.

---

<div class="post-metadata">

**Author:** ![ablaom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ablaom/32/4889_2.png) [@ablaom](https://discourse.julialang.org/u/ablaom)\
**Post date:** [December 19, 2022, 4:30pm UTC](https://discourse.julialang.org/t/mlj-w-scikitlearn-passing-return-std-to-predict/91654/7 "2022-12-19T16:30:12Z")

</div>

Not following this discussion in detail as on leave. However, perhaps the poster may find [SossMLJ.jl](https://github.com/cscherrer/SossMLJ.jl) useful. It provides some Bayesian models with MLJ interfaces. They are not registered, and so cannot be loaded with `@load` but can still be loaded explicitly. If I recall correctly, `predict` returns distribution-like objects, although they do not support the `pdf` interface (only `rand` ?). There is also a `predict_joint` for predicting a _single_ multivariate distribution ; see the docs for details.

---

<div class="post-metadata">

**Author:** ![ablaom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ablaom/32/4889_2.png) [@ablaom](https://discourse.julialang.org/u/ablaom)\
**Post date:** [January 9, 2023, 1:33am UTC](https://discourse.julialang.org/t/mlj-w-scikitlearn-passing-return-std-to-predict/91654/8 "2023-01-09T01:33:26Z")

</div>

> <https://github.com/JuliaAI/MLJScikitLearnInterface.jl/issues/47>
>
> A case in point is BayesianRidgeRegressor, as pointed out at https://github.com/…JuliaAI/MLJScikitLearnInterface.jl/issues/46.
> 
> Be good to first identify what other sk-learn models with MLJ implementations have the \`return\_std\` predict kwarg. Help with this appreciated.
