You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When trying to fit a lightgbm model using a polars Dataframe as input, the code fails with the attribute error: AttributeError: property 'feature_names_in_' of 'LGBMRegressor' object has no setter
Inheriting from the Model and defining a feature_names_in property with a setter fixes the issue.
This error does not occur when using a pandas DataFrame as Input (Version 2.2.2).
Reproducible example
importlightgbmaslgbimportnumpyasnpimportpolarsaspln=500rng=np.random.default_rng(42)
data= {"x1": rng.integers(0, 2, size=n), "x2": rng.integers(0, 2, size=n)}
df=pl.DataFrame(data)
y=data["x1"] +data["x2"] +data["x1"] *data["x2"]
y=y+rng.normal(scale=0.01, size=n)
parameters= {
"learning_rate": 0.1,
"min_data_in_bin": 1,
"min_data_in_leaf": 1,
"num_iterations": 3,
"num_leaves": 4,
"verbosity": -1,
}
# This fails with an AttributeErrorregressor=lgb.LGBMRegressor(**parameters)
regressor.fit(df, y).predict(df)
# Rerunning with the PatchedRegressor fixes the issueclassPatchedRegressor(lgb.LGBMRegressor):
@propertydeffeature_names_in_(self):
returnself._feature_name@feature_names_in_.setterdeffeature_names_in_(self, x):
self._feature_name=xregressor=PatchedRegressor(**parameters)
regressor.fit(df, y).predict(df)
The text was updated successfully, but these errors were encountered:
jameslamb
changed the title
Fitting on Polars Dataframe fails due to missing setter for fetures_names_in_
[python-package] Fitting on Polars Dataframe fails due to missing setter for fetures_names_in_
Feb 28, 2025
Description
When trying to fit a lightgbm model using a polars Dataframe as input, the code fails with the attribute error:
AttributeError: property 'feature_names_in_' of 'LGBMRegressor' object has no setter
Inheriting from the Model and defining a
feature_names_in
property with a setter fixes the issue.This error does not occur when using a pandas DataFrame as Input (Version 2.2.2).
Reproducible example
Environment info
Lightgbm: 4.6.0
Polars: 1.22.0
Numpy: 2.1.3
Python: 3.11.11
Additional Comments
The text was updated successfully, but these errors were encountered: