Skip to content

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

Open
@FelicitasTengenQC

Description

@FelicitasTengenQC

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions