LightGBM icon indicating copy to clipboard operation
LightGBM copied to clipboard

[python-package] How do I make LightGBM's output deterministic?

Open ashutoshraj opened this issue 2 years ago • 7 comments

Hi, I have managed to finally solve the problem. The issue was related to column order with categorical features when generating the lightgbm.Dataset. You have to use categorical_feature parameter to fix it. So in short, once you set {"deterministic": True, "force_col_wise": True} and use categorical_feature parameter to generate the dataset it works just fine. I have also set {"seed": 42}, but there is probably no need to do it.

I am now using version lightgbm==3.3.2

Originally posted by @santigc6 in https://github.com/microsoft/LightGBM/issues/5243#issuecomment-1141288922

ashutoshraj avatar Feb 15 '24 18:02 ashutoshraj

Hi,

Following the recommended approach, I've initialized the LGBMRegressor with specific parameters as shown below. Despite this setup, I encounter an issue with result reproducibility after restarting the Jupyter kernel. If I execute all cells without restarting the kernel, the results remain consistent. However, post-kernel restart, the outcomes vary. Here's how I'm instantiating the model:

cat_features is a list of categorical column names.

model = LGBMRegressor(verbose=-1, categorical_feature=cat_features, deterministic=True, force_col_wise=True, random_seed=42)

Could you please advise on ensuring consistent results, irrespective of kernel restarts?

ashutoshraj avatar Feb 15 '24 18:02 ashutoshraj

Thanks for using LightGBM.

Please provide a minimal, reproducible example showing exactly the code you're running. That should also include the following information:

  • operating system
  • version of LightGBM and how you installed it
  • precisely what you mean by "outcomes vary"

We'd be happy to help, but we'll get to an answer faster if you can please narrow down what you're asking for.

jameslamb avatar Feb 16 '24 03:02 jameslamb

Hi @jameslamb,

Thanks for your response. Upon further investigation and debugging of the code. it has come to my attention that the variability in the LightGBM output was due to change in order of column index. After restarting the kernel is likely due to the use of set operations on your feature lists. This can lead to inconsistencies in the order of columns in your DataFrame after operations involving sets. Somehow it was not causing problem when I simply re-run the whole code again without restarting the kernel.

ashutoshraj avatar Feb 17 '24 08:02 ashutoshraj

Operating System: macOS Sonoma 14.3.1 LightGBM version: 3.3.5

ashutoshraj avatar Feb 17 '24 08:02 ashutoshraj

the variability in the LightGBM output was due to change in order of column index. After restarting the kernel is likely due to the use of set operations on your feature lists.

Are you saying that LightGBM is rearranging the columns of a pandas DataFrame you're passing it? That would be a bug and something we'd like to fix.

Please provide a reproducible example and/or a link to the code in LightGBM you're referring to.

jameslamb avatar Feb 17 '24 14:02 jameslamb

Before providing the training dataframe, the order of columns was altered due to the set operation. I have categorized the columns into categorical, continuous, and exogenous variables. To maintain uniqueness and ensure that each feature is included only once, I avoided using the set operation to remove duplicates. The new approach retains the original functionality while complying with the requirement to avoid set operations, thus preserving the order of features.

There are no bugs or issues with LightGBM, and I am able to reproduce the results.

ashutoshraj avatar Feb 18 '24 15:02 ashutoshraj

This issue has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this.

github-actions[bot] avatar Feb 19 '25 00:02 github-actions[bot]