Sampling for bin construction
@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.
it is random sample.
@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")])))
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.
refer to https://github.com/Microsoft/LightGBM/blob/master/include/LightGBM/utils/random.h#L65
sample K from N.
@rgranvil it seems the final sampling has some problems
@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
@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
@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 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