allow passing kwargs in .skb.apply()#1642
Conversation
|
I feel like I don't particularly like any of the solutions we discussed with @jeromedockes, but at the same time not having this feature would be too limiting, since people use other libraries that need these kwargs (and scikit-learn has some estimators that need them too). I think this version with fit_kwargs and predict_kwargs is the better solution. |
|
example passing sample weights (computed from X) to a ridge regressor: import polars as pl
from sklearn.linear_model import Ridge
import skrub
import skrub.datasets
import skrub.selectors as s
dataset = pl.from_pandas(skrub.datasets.fetch_employee_salaries().employee_salaries)
# skrub.TableReport(dataset).open()
data = skrub.var("data")
y_col = "current_annual_salary"
X = data.skb.select(s.all() - y_col).skb.mark_as_X()
y = data[y_col].skb.mark_as_y()
def compute_weights(X):
return (-(((X["year_first_hired"] - 2025) / 20) ** 2)).exp().rename("weight")
weight = X.skb.apply_func(compute_weights)
pred = X.skb.apply(skrub.TableVectorizer()).skb.apply(
Ridge(), y=y, fit_kwargs={"sample_weight": weight}
) |
rcap107
left a comment
There was a problem hiding this comment.
example passing sample weights (computed from X) to a ridge regressor:
import polars as pl from sklearn.linear_model import Ridge import skrub import skrub.datasets import skrub.selectors as s dataset = pl.from_pandas(skrub.datasets.fetch_employee_salaries().employee_salaries) # skrub.TableReport(dataset).open() data = skrub.var("data") y_col = "current_annual_salary" X = data.skb.select(s.all() - y_col).skb.mark_as_X() y = data[y_col].skb.mark_as_y() def compute_weights(X): return (-(((X["year_first_hired"] - 2025) / 20) ** 2)).exp().rename("weight") weight = X.skb.apply_func(compute_weights) pred = X.skb.apply(skrub.TableVectorizer()).skb.apply( Ridge(), y=y, fit_kwargs={"sample_weight": weight} )
Could you add an example like this to the docstring? Maybe we could also add a mention to xgboost since it's likely what a lot of people will use this for.
Aside from that, I think it's good
rcap107
left a comment
There was a problem hiding this comment.
Looks good to me, thanks @jeromedockes
This allows passing extra named arguments to the methods of estimators passed to
.skb.apply(). Still hesitating slightly about whether this is needed.Also, we should agree on the exact interface: a single dict like
kwargs={"fit": {"sample_weight": sw}}, several likefit_kwargs={"sample_weight": sw}(what is currently in this PR), other variations...