LightGBM icon indicating copy to clipboard operation
LightGBM copied to clipboard

Suggestion for python API in prediction

Open miguelgfierro opened this issue 9 years ago • 9 comments

This is a suggestion for the python API, not a bug. It is just something that seemed odd to me :-)

Here a code example:

df = pd.read_csv('dataset.csv')
X_train, X_test, y_train, y_test = function_that_creates_train_and_test_set(df)
lgb_train = lgb.Dataset(X_train.values, y_train.values, free_raw_data=False)
lgb_test = lgb.Dataset(X_test.values, y_test.values, reference=lgb_train, free_raw_data=False)
lgbm_clf_pipeline = lgb.train(params, lgb_train, num_boost_round=200)
y_pred = lgbm_clf_pipeline.predict(X_test.values)

For me it's a little bit odd that in predict the input is X_test.values rather than lgb_test

@guolinke just a suggestion, you can close it, once you have read it :)

miguelgfierro avatar Jun 16 '17 11:06 miguelgfierro

y_pred = lgbm_clf_pipeline.predict(X_test.values)

To debug data, I want below: for p, l, feat in zip(preds, eval_data.get_label(), eval_data.get_data()): print p, l, feat

Now only below:

for p, l in zip(preds, eval_data.get_label()): print p, l

hlee13 avatar Jun 16 '17 13:06 hlee13

@miguelgfierro @hlee13 I think the prediction with Dataset comes from XGBoost. But this is infeasible in LightGBM. The reason is, Dataset only store the bucketed bin values, not the original feature values.

Thus, The Dataset in LightGBM only be used for the training and validation.

Moreover, using Dataset to perform the prediction needs an additional conversion from raw data to DataSet. So we choose prediction by the raw data directly.

guolinke avatar Jun 16 '17 13:06 guolinke

ok understood. Thanks for the clarification

miguelgfierro avatar Jun 16 '17 16:06 miguelgfierro

Hey, I think I'm missing something.

My understanding of lgb is that:

  1. turn features into buckets
  2. build trees with bucketed features

Given that the trees are expressed in terms of mappings from bucketed features to y-values, shouldn't the buckets themselves be enough? What role does information present in the raw data but not in the bucketed version play? Furthermore, during the training, the y-values of the validation sets (which are lgb.Dataset objects) are explicitly computed and the resulting error metrics displayed. Couldn't this same procedure be applied to any lgb.Dataset object with the same bucket structure as the training?

As in, wouldn't this be as simple as just making the lgb.Booster.__inner_predict() function public? I guess the only issue with this is that it's not clear how easy it is to check if the input lgb.Dataset object has the same bin structure as required by the lgb.Booster object...

The particular use case I have in mind is this:

  1. pull big pd.Dataframe object
  2. turn it into a more space-efficient lgb.Dataset object, store to disk
  3. subset the lgb.Dataset object into training/validation/test
  4. train using the train/val
  5. predict on test

Is the issue that if we're constructing bins based on the full test/train/val, we're contaminating the test/train with information about the distribution of features in test?

j-mark-hou avatar Aug 07 '17 22:08 j-mark-hou

@j-mark-hou The main reason is for the efficiency. Mapping from y-value to bucketed values is time-costing, it needs about m*n*log(max_bin). For the training/val data, we need to predict over it many times, so the cost of bucketed conversion (only needs one-time) is acceptable. However, for the test data, we only predict it one time. Thus, bucketed conversion will bring much over-head. And for the prediction of test data, we can do it streamly, so it won't cost much memory.

For you use case, it can be:

  1. pull big pd.Dataframe object
  2. Split it to Train, Val, Test, and convert train/val to lgb.Dataset, save to disk
  3. Save Test to disk (you can use csv format)
  4. train using the train/val
  5. predict on Test (use file as input)

guolinke avatar Aug 08 '17 00:08 guolinke

am I understanding correctly that the m*n*log(max_bin) comes from (m features) x (n observations per features) * (log(max_bins) binary search time to pick the right bin)?

I agree this is costly, however in the particular use case I'm encountering, the alternative is costlier. The feature generation itself takes a few hours, and the resulting pandas dataframe is sufficiently large that writing/reading in whatever format is equally time prohibitive (if not moreso).

Presumably the lgb.Dataset object contains a mapping from (raw X <-> bucketed X), so it seems like it should be theoretically possible to (1) extract the (raw X <-> bucketed X) mapping from an lgb.Dataset object, (2) use it to turn the (raw X-> Y) Booster object into a (bucketed X -> Y) Booster object, and (3) use the resulting (bucketed X -> Y) Booster object to predict on a lgb.Dataset object? Is this (raw X -> bucketed X) mapping something that can be easily extracted from the lgb.Dataset object?

Thanks for your time, sorry to keep pushing on this point, but it would make my life much easier, and I'd be happy to write the code to do this if it seems not-too-unreasonably complex.

j-mark-hou avatar Aug 08 '17 00:08 j-mark-hou

yeah, you understand it correctly. welcome to contribute this feature. To support this, you may need:

  1. Check the BinMapper to know how to perform "Raw X <-> bucked X"
  2. Check the Dataset to check how to mapping the feature index
  3. Add a function into "Dataset" for the prediction, the parameters is a "boosting" object. In that function, you should convert the Bucked X -> Raw X, and call boosting->predict .
  4. Add a function into "Predictior", let it support the prediction by Dataset.
  5. Update c_apis.

Let me know if you have more questions.

guolinke avatar Aug 08 '17 02:08 guolinke

@j-mark-hou another reason of not allowing this is: It may cause the incorrect prediction if predicting with Dataset. e.g. The Dataset used in Prediction is not align with the Dataset used in training. It may need some techs to check this, e.g. the md5 .

guolinke avatar Aug 08 '17 02:08 guolinke

got it, I'll start looking into it. no guarantees, but let's see if I can do something.

j-mark-hou avatar Aug 08 '17 02:08 j-mark-hou