Skip to content
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

[python-package] Fitting on Polars Dataframe fails due to missing setter for fetures_names_in_ #6849

Open
FelicitasTengenQC opened this issue Feb 28, 2025 · 1 comment
Assignees
Labels

Comments

@FelicitasTengenQC
Copy link

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

import lightgbm as lgb
import numpy as np
import polars as pl

n = 500
rng = 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 AttributeError
regressor = lgb.LGBMRegressor(**parameters)
regressor.fit(df, y).predict(df)


# Rerunning with the PatchedRegressor fixes the issue
class PatchedRegressor(lgb.LGBMRegressor):

    @property
    def feature_names_in_(self):
        return self._feature_name

    @feature_names_in_.setter
    def feature_names_in_(self, x):
        self._feature_name = x


regressor = PatchedRegressor(**parameters)
regressor.fit(df, y).predict(df)
 

Environment info

Lightgbm: 4.6.0
Polars: 1.22.0
Numpy: 2.1.3
Python: 3.11.11

Additional Comments

@jameslamb 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
@jameslamb jameslamb added the bug label Feb 28, 2025
@borchero
Copy link
Collaborator

borchero commented Mar 3, 2025

I'll be investigating this week 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants