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

[FEA] Recognize pandas_categorical field in LightGBM model #10

Open
zyxue opened this issue Jan 21, 2022 · 1 comment
Open

[FEA] Recognize pandas_categorical field in LightGBM model #10

zyxue opened this issue Jan 21, 2022 · 1 comment

Comments

@zyxue
Copy link

zyxue commented Jan 21, 2022

Reproduce

X = (
    pd.DataFrame(
        np.array(30 * [["a", 1]] + 30 * [["b", 2]] + 30 * [["c", 3]]),
        columns=["x1", "x2"],
    )
    .assign(x1=lambda df: df["x1"].astype("category"))
    .assign(x2=lambda df: df["x2"].astype(float))
)
y = np.array(60 * [5] + 30 * [10])

X.dtypes

bst = lgb.LGBMRegressor(n_estimators=5)
bst.fit(X, y)

bst.predict(X.head(2))
bst.booster_.save_model("model.txt")

model = treelite.Model.load('model.txt', model_format='lightgbm')
model.export_lib(toolchain="gcc", libpath='./mymodel.so', verbose=True)
predictor = treelite_runtime.Predictor('./mymodel.so', verbose=True)

the above all works, but not sure how to use predictor, as

dmat = treelite_runtime.DMatrix(X.head(2).to_numpy())
predictor.predict(dmat) 

leads to error like

     41         return 'float64'
     42     else:
---> 43         raise ValueError(f'Unrecognized NumPy type: {type_info}')
     44 
     45 

ValueError: Unrecognized NumPy type: object

The model file is like
model.txt

at the end of model.txt, it has

...

end of parameters

pandas_categorical:[["a", "b", "c"]]

My guess is that treelite doesn't make use of the pandas_categorical line yet, so trying to confirm.

Versions:

lightgbm==3.1.1
treelite==2.2.0
treelite-runtime==2.2.0
@hcho3
Copy link
Collaborator

hcho3 commented Jan 21, 2022

My guess is that treelite doesn't make use of the pandas_categorical line yet

You are right, Treelite does not yet recognize pandas_categorical field yet. I will mark this as a feature request.

@hcho3 hcho3 changed the title Does treelite support pandas categorical in LightGBM Recognize pandas_categorical field in LightGBM model Jan 21, 2022
@hcho3 hcho3 changed the title Recognize pandas_categorical field in LightGBM model [FEA] Recognize pandas_categorical field in LightGBM model Jan 21, 2022
@hcho3 hcho3 pinned this issue May 9, 2023
@hcho3 hcho3 transferred this issue from dmlc/treelite May 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants