LightGBM icon indicating copy to clipboard operation
LightGBM copied to clipboard

Sampling for bin construction

Open rgranvil opened this issue 9 years ago • 9 comments

@guolinke If I understand correctly, bin_construct_sample_cnt is used to set the number of observations used to create the bins. Could you confirm whether this is a random sample or a sample of the top n observations? Thanks.

rgranvil avatar Sep 15 '17 00:09 rgranvil

it is random sample.

guolinke avatar Sep 15 '17 00:09 guolinke

@guolinke Could you explain what is happening in this example? I have a single predictor, x1, with two possible values: 1 or 2. When the dataset is sorted by x1, I get the error:

[LightGBM] [Fatal] Cannot construct Dataset since there are not useful features. Error in lgb.call("LGBM_DatasetCreateFromMat_R", ret = handle, private$raw_data, : api error: Cannot construct Dataset since there are not useful features.

When I randomize the order of observations, it gives the expected results.

Code:

library(lightgbm)

# Create a sample dataset
set.seed(1)
df <- data.frame(x1 = as.numeric(c(rep(1,400000), rep(2, 100000))))
df$y <- df$x1 + runif(nrow(df))

lgb.unloader()
lgb.model1 <- lgb.train(
  data = lgb.Dataset(data = as.matrix(df[, c("x1")]), label = df$y)
  ,obj = "regression_l2"
  ,nrounds = 1
)

# Randomize order of observations, then refit
lgb.unloader()
df <- df[sample(1:nrow(df)), ]
lgb.model2 <- lgb.train(
  data = lgb.Dataset(data = as.matrix(df[, c("x1")]), label = df$y)
  ,obj = "regression_l2"
  ,nrounds = 1
)
table(predict(lgb.model2, as.matrix(df[, c("x1")])))

rgranvil avatar Sep 15 '17 03:09 rgranvil

The issue seems to be related to bin_construct_sample_cnt. In the above example, I tested values of bin_construct_sample_cnt between 1,000 and 600,000 in increments of 1,000. It seemed to only produce the error when bin_construct_sample_cnt was between approximately 126,000 and 133,000 or between 167,000 and 200,000. Other values would run fine.

rgranvil avatar Sep 15 '17 11:09 rgranvil

refer to https://github.com/Microsoft/LightGBM/blob/master/include/LightGBM/utils/random.h#L65

sample K from N.

guolinke avatar Sep 15 '17 13:09 guolinke

@rgranvil it seems the final sampling has some problems

guolinke avatar Sep 15 '17 13:09 guolinke

@guolinke Yes, it seems that in the Sample function, some values of N and K would make it very unlikely to sample from the bottom of the data.

Would this sampling approach work as an alternative? https://stackoverflow.com/a/9345144

rgranvil avatar Sep 16 '17 01:09 rgranvil

@guolinke or is there a reason this method couldn't be used for all K < N?

else if (K > N / 2) { for (int i = 0; i < N; ++i) { double prob = (K - ret.size()) / static_cast(N - i); if (NextFloat() < prob) { ret.push_back(i); } }

rgranvil avatar Sep 16 '17 02:09 rgranvil

@rgranvil This algorithm is O(N), while another is O(K). I think we can change it to the hashset solution for the stable result.

The Fisher-Yates shuffle seems good. can you help to contribute it ?

guolinke avatar Sep 16 '17 15:09 guolinke

@guolinke If it was R coding, I could probably help out, but I don't know nearly enough C++ to be of any help implementing it

rgranvil avatar Sep 17 '17 16:09 rgranvil