Banjo
Banjo

Reputation: 1251

Installing lightgbm in R

I have tried different things to install the lightgbm package but I can´t get it done. I tried all methods at the github repository but they don't work. I run Windows 10 and R 3.5 (64 bit). There is someone with similar problems. So I tried his solution:

-->

  devtools::install_github("Laurae2/lgbdl", force = TRUE)
  library(lgbdl)
  lgb.dl(commit = "master",
  compiler = "vs",
  repo = "https://github.com/Microsoft/LightGBM")

 *** arch - i386
installing via 'install.libs.R' to C:/Users/X1/Documents/R/win- 
library/3.5/lightgbm
Error in eval(ei, envir) : Cannot find lib_lightgbm.dll
* removing 'C:/Users/XXX/Documents/R/win-library/3.5/lightgbm'
In R CMD INSTALL
installation of package 
�C:/Users/XXX/AppData/Local/Temp/RtmpczNLaN/LightGBM/R-package� had non- 
zero exit status[1] FALSE

Any idea how to fix this?

Upvotes: 5

Views: 9017

Answers (4)

Rodrigo Guinea
Rodrigo Guinea

Reputation: 328

So you want to run LightGBM in R, but you are not a software engineer... then, you can use R and Python (because it is a pain to install the R package).

First, write this in your R console or R Markdown:

library(reticulate)
library(knitr)
knitr::knit_engines$set(python3 = reticulate::eng_python)

Second, write this in your R console or R Markdown:

Sys.which("python3")
os = import("os")
os$listdir(".")

Third, open your notepad, save it as "script.py", code your functions and call the appropriate libraries. Something like this:

import pandas as pd
import lightgbm as lgb

def python_to_r_lgbm(X_train, y_train, X_test):
    lgb_train = lgb.Dataset(X_train, y_train)

    params = {'bagging_fraction': 0.75, 
              'boosting_type': 'gbdt', 
              'drop_rate': 0.15, 
              'feature_fraction': 1, 
              'lambda_l1': 2.95, 
              'lambda_l2': 2.35, 
              'learning_rate': 0.01, 
              'max_depth': 3, 'max_leaves': 19, 
              'min_data_in_leaf': 8, 
              'objective': 'regression', 
              'metrics': ['l1', 'l2', 'huber'], 
              'verbose': -1}

    gbm = lgb.train(params, lgb_train, 1800)

    light_gbm_test = gbm.predict(X_test)

    return light_gbm_test

Then, run this in your R console or R Markdown:

source_python('script.py')

Finally, call it in your R console (or R Markdown) your new function:

python_to_r_lgbm(xTrain_df, y_train, xTest_df)

This function will train your model with the xTrain_df and y_train and will give you the predictions using xTest_df.

*Make sure to have all the files in the same folder. Also, make sure to have Python3 (obviously) and the lightgbm library installed (https://anaconda.org/conda-forge/lightgbm).

Jokes aside, I hope this helps you. I also hope not to be lynch by an angry mob because I did not answer the question directly :D

The source: https://rviews.rstudio.com/2019/03/18/the-reticulate-package-solves-the-hardest-problem-in-data-science-people/ Although I did some modifications.

Upvotes: 0

Lê Anh Tuấn
Lê Anh Tuấn

Reputation: 46

It works for me, hope it helps.

  1. Make sure you install all mandatory software

  2. Download the precomplied .dll file from https://github.com/Microsoft/LightGBM/releases and put it in .\LightGBM\

  3. In install.libs.R, set use_precompile <- TRUE

  4. Copy CMakeLists.txt from root directory into .\LightGBM\R-package\inst\bin

  5. In R console type: install.packages(file.path("C:\yourdirectory\", "LightGBM", "R-package"), repos = NULL, type = "source")

Upvotes: 3

pwjvr
pwjvr

Reputation: 158

For some poor soul struggling through this and if the above mentioned fixes did not work. What I had to do to get it working was:

  • Ensure you have the following in your path environment variables:

    • Rtools (point to the bin folder in the install directory of rtools)

    • Rtools mingw_64 (point to the mingw_64 folder in the rtools install directory)

    • Base R (point to the bin folder in your base R install directory, i.e. where you install R version whatever)

    • CMake (point to the bin folder in your cmake install directory)

    • Visual Studio (allows you to build with VS Build Tools, otherwise will fallback to RTools or any MinGW64 available as stated on the github page)

    • Git

  • Before installing LightGBM, install the following packages in R itself:

    • data.tools

    • magrittr

    • R6

    • jsonlite

  • Once all the above is done, run with the git installation instructions as on the github page here

    For those not able to access the link (or if it should move), the command are the following:

    git clone --recursive https://github.com/microsoft/LightGBM
    
    cd LightGBM
    
    Rscript build_r.R
    

Upvotes: 3

JKMburu
JKMburu

Reputation: 37

I have managed to install it after a "million" tries. I had to ensure there was only one R version installed - 64 bit and that all the other steps were done with the expected environmental variables set.

Upvotes: 1

Related Questions