Lightgbm: R Package lgb.Dataset.construct() throwing api error: cannot open data file k

Created on 31 May 2017  路  4Comments  路  Source: microsoft/LightGBM

This Issue had the same error but its resolution apparently doesn't apply - https://github.com/Microsoft/LightGBM/issues/219 . Please let me know if I should've made this a comment in that thread instead of opening it here.

Environment info

Operating System: Windows Server 2008 R2
CPU: Intel
C++/Python/R version: R 3.3.1

Error Message:

api error: cannot open data file k

Reproducible examples

library(lightgbm)
data(ChickWeight) # Built in data set

ChickWeight <- sapply(ChickWeight, function(x) as.numeric(as.character(x)))
head(ChickWeight)
apply(ChickWeight,2,class)

y <- ChickWeight[,"weight"]
x <- ChickWeight[,!colnames(ChickWeight)=="weight"]

dtrain <- lgb.Dataset(data = as.matrix(x),
                      label = as.numeric(y),
                      free_raw_data=FALSE
)

lgb.Dataset.construct(dtrain)

Error in lgb.call("LGBM_DatasetCreateFromMat_R", ret = handle, private$raw_data, :
api error: cannot open data file k

It's only numeric data. I tried it with data.matrix instead of as.matrix. I also tried making it a sparse matrix first with a different input data set.

r-package

Most helpful comment

@hack-r can you try to reinstall LightGBM? It is working correctly here.

If you did not restart R, use lgb.unloader(wipe = TRUE) to kill lightgbm resources from R (you will have to redo library(lightgbm) to access LightGBM again. There are conflicts when you get an error and you do not reload lightgbm in R.

> library(lightgbm)
Loading required package: R6
> data(ChickWeight) # Built in data set
> 
> ChickWeight <- sapply(ChickWeight, function(x) as.numeric(as.character(x)))
> head(ChickWeight)
     weight Time Chick Diet
[1,]     42    0     1    1
[2,]     51    2     1    1
[3,]     59    4     1    1
[4,]     64    6     1    1
[5,]     76    8     1    1
[6,]     93   10     1    1
> apply(ChickWeight,2,class)
   weight      Time     Chick      Diet 
"numeric" "numeric" "numeric" "numeric" 
> 
> y <- ChickWeight[,"weight"]
> x <- ChickWeight[,!colnames(ChickWeight)=="weight"]
> 
> dtrain <- lgb.Dataset(data = as.matrix(x),
+                       label = as.numeric(y),
+                       free_raw_data=FALSE
+ )
> 
> lgb.Dataset.construct(dtrain)
> 

All 4 comments

@hack-r can you try to reinstall LightGBM? It is working correctly here.

If you did not restart R, use lgb.unloader(wipe = TRUE) to kill lightgbm resources from R (you will have to redo library(lightgbm) to access LightGBM again. There are conflicts when you get an error and you do not reload lightgbm in R.

> library(lightgbm)
Loading required package: R6
> data(ChickWeight) # Built in data set
> 
> ChickWeight <- sapply(ChickWeight, function(x) as.numeric(as.character(x)))
> head(ChickWeight)
     weight Time Chick Diet
[1,]     42    0     1    1
[2,]     51    2     1    1
[3,]     59    4     1    1
[4,]     64    6     1    1
[5,]     76    8     1    1
[6,]     93   10     1    1
> apply(ChickWeight,2,class)
   weight      Time     Chick      Diet 
"numeric" "numeric" "numeric" "numeric" 
> 
> y <- ChickWeight[,"weight"]
> x <- ChickWeight[,!colnames(ChickWeight)=="weight"]
> 
> dtrain <- lgb.Dataset(data = as.matrix(x),
+                       label = as.numeric(y),
+                       free_raw_data=FALSE
+ )
> 
> lgb.Dataset.construct(dtrain)
> 

Thanks so much @Laurae2 . I did restart R before, but I didn't know about lgb.unloader(wipe = TRUE). That did the trick.

@Laurae2 's solution helped me in a similar instance as well.

@Laurae2 you save my day!

Was this page helpful?
0 / 5 - 0 ratings