README.md 6.88 KB
Newer Older
1
LightGBM R-package
Guolin Ke's avatar
Guolin Ke committed
2
3
4
5
==================

Installation
------------
Guolin Ke's avatar
Guolin Ke committed
6

7
### Preparation
Guolin Ke's avatar
Guolin Ke committed
8

9
You need to install git and [CMake](https://cmake.org/) first.
10

Guolin Ke's avatar
Guolin Ke committed
11
12
13
14
Note: 32-bit R/Rtools is not supported.

#### Windows Preparation

15
Installing [Rtools](https://cran.r-project.org/bin/windows/Rtools/) is mandatory, and only support the 64-bit version. It requires to add to PATH the Rtools MinGW64 folder, if it was not done automatically during installation.
Guolin Ke's avatar
Guolin Ke committed
16

17
The default compiler is Visual Studio (or [MS Build](https://visualstudio.microsoft.com/downloads/)) in Windows, with an automatic fallback to Rtools or any [MinGW64](https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/) (x86_64-posix-seh) available (this means if you have only Rtools and CMake, it will compile fine).
18

19
To force the usage of Rtools / MinGW, you can set `use_mingw` to `TRUE` in `R-package/src/install.libs.R`.
20

21
22
For users who wants to install online with GPU or want to choose a specific compiler, please check the end of this document for installation using a helper package ([Laurae2/lgbdl](https://github.com/Laurae2/lgbdl/)).

23
**Warning for Windows users**: it is recommended to use *Visual Studio* for its better multi-threading efficency in Windows for many core systems. For very simple systems (dual core computers or worse), MinGW64 is recommended for maximum performance. If you do not know what to choose, it is recommended to use [Visual Studio](https://visualstudio.microsoft.com/downloads/), the default compiler. **Do not try using MinGW in Windows on many core systems. It may result in 10x slower results than Visual Studio.**
Laurae's avatar
Laurae committed
24

25
#### macOS Preparation
Laurae's avatar
Laurae committed
26

27
gcc with OpenMP support must be installed first. Refer to [Installation-Guide](https://github.com/Microsoft/LightGBM/blob/master/docs/Installation-Guide.rst#macos) for installing gcc with OpenMP support.
28

29
### Install
30

Laurae's avatar
Laurae committed
31
Install LightGBM R-package with the following command:
32

Laurae's avatar
Laurae committed
33
```sh
34
35
git clone --recursive https://github.com/Microsoft/LightGBM
cd LightGBM/R-package
36
# export CXX=g++-7 CC=gcc-7  # for macOS (replace 7 with version of gcc installed on your machine)
Guolin Ke's avatar
Guolin Ke committed
37
R CMD INSTALL --build . --no-multiarch
Guolin Ke's avatar
Guolin Ke committed
38
```
Laurae's avatar
Laurae committed
39

40
Or build a self-contained R-package which can be installed afterwards:
Laurae's avatar
Laurae committed
41
42

```sh
43
44
45
git clone --recursive https://github.com/Microsoft/LightGBM
cd LightGBM/R-package
Rscript build_package.R
46
# export CXX=g++-7 CC=gcc-7  # for macOS (replace 7 with version of gcc installed on your machine)
Guolin Ke's avatar
Guolin Ke committed
47
R CMD INSTALL lightgbm_2.1.1.tar.gz --no-multiarch
48
``` 
Guolin Ke's avatar
Guolin Ke committed
49

Guolin Ke's avatar
Guolin Ke committed
50
51
52
Note: for the build with Visual Studio/MSBuild in Windows, you should use the Windows CMD or Powershell.

Windows users may need to run with administrator rights (either R or the command prompt, depending on the way you are installing this package). Linux users might require the appropriate user write permissions for packages.
53

54
Set `use_gpu` to `TRUE` in `R-package/src/install.libs.R` to enable the build with GPU support. You will need to install Boost and OpenCL first: details for installation can be found in [Installation-Guide](https://github.com/Microsoft/LightGBM/blob/master/docs/Installation-Guide.rst#build-gpu-version).
55
56

You can also install directly from R using the repository with `devtools`:
57

58
```r
Guolin Ke's avatar
Guolin Ke committed
59
library(devtools)
60
options(devtools.install.args = "--no-multiarch") # if you have 64-bit R only, you can skip this
Guolin Ke's avatar
Guolin Ke committed
61
install_github("Microsoft/LightGBM", subdir = "R-package")
62
```
63

Guolin Ke's avatar
Guolin Ke committed
64
If you are using a precompiled dll/lib locally, you can move the dll/lib into LightGBM root folder, modify `LightGBM/R-package/src/install.libs.R`'s 2nd line (change `use_precompile <- FALSE` to `use_precompile <- TRUE`), and install R-package as usual. **NOTE: If your R version is not smaller than 3.5.0, you should set `DUSE_R35=ON` in cmake options when build precompiled dll/lib**.
65

66
When your package installation is done, you can check quickly if your LightGBM R-package is working by running the following:
67
68
69
70
71
72
73
74
75

```r
library(lightgbm)
data(agaricus.train, package='lightgbm')
train <- agaricus.train
dtrain <- lgb.Dataset(train$data, label=train$label)
params <- list(objective="regression", metric="l2")
model <- lgb.cv(params, dtrain, 10, nfold=5, min_data=1, learning_rate=1, early_stopping_rounds=10)
```
76

77
Installation with Precompiled dll/lib from R Using GitHub
78
---------------------------------------------------------
79
80
81
82
83
84
85
86
87
88

You can install LightGBM R-package from GitHub with devtools thanks to a helper package for LightGBM.

### Prerequisites

You will need:

* Precompiled LightGBM dll/lib
* MinGW / Visual Studio / gcc (depending on your OS and your needs) with make in PATH environment variable
* git in PATH environment variable
89
* [CMake](https://cmake.org/) in PATH environment variable
90
91
92
* [lgbdl](https://github.com/Laurae2/lgbdl/) R-package, which can be installed using `devtools::install_github("Laurae2/lgbdl")`
* [Rtools](https://cran.r-project.org/bin/windows/Rtools/) if using Windows

93
94
95
96
In addition, if you are using a Visual Studio precompiled DLL, assuming you do not have Visual Studio installed (if you have it installed, ignore the warnings below):

* Visual Studio 2015/2017 precompiled DLL: download and install Visual Studio Runtime for [2015](https://www.microsoft.com/en-us/download/details.aspx?id=52685)/[2017](https://go.microsoft.com/fwlink/?LinkId=746572) (you will get an error about MSVCP140.dll missing otherwise)

97
98
Once you have all this setup, you can use `lgb.dl` from `lgbdl` package to install LightGBM from repository.

99
For instance, you can install the R-package from LightGBM master commit of GitHub with Visual Studio using the following from R:
100
101
102

```r
lgb.dl(commit = "master",
103
104
       compiler = "vs",
       repo = "https://github.com/Microsoft/LightGBM")
105
106
107
108
109
110
111
112
113
114
```

You may also install using a precompiled dll/lib using the following from R:

```r
lgb.dl(commit = "master",
       libdll = "C:\\LightGBM\\windows\\x64\\DLL\\lib_lightgbm.dll", # YOUR PRECOMPILED DLL
       repo = "https://github.com/Microsoft/LightGBM")
```

115
116
117
118
119
120
121
122
123
You may also install online using a LightGBM with proper GPU support using Visual Studio (as an example here) using the following from R:

```r
lgb.dl(commit = "master",
       compiler = "vs", # Remove this for MinGW + GPU installation
       repo = "https://github.com/Microsoft/LightGBM",
       use_gpu = TRUE)
```

124
125
126
For more details about options, please check [Laurae2/lgbdl](https://github.com/Laurae2/lgbdl/) R-package.

You may also read [Microsoft/LightGBM#912](https://github.com/Microsoft/LightGBM/issues/912#issuecomment-329496254) for a visual example for LightGBM installation in Windows with Visual Studio.
127

Guolin Ke's avatar
Guolin Ke committed
128
Examples
129
--------
Guolin Ke's avatar
Guolin Ke committed
130

131
132
133
134
135
136
137
138
139
Please visit [demo](demo):

* [Basic walkthrough of wrappers](demo/basic_walkthrough.R)
* [Boosting from existing prediction](demo/boost_from_prediction.R)
* [Early Stopping](demo/early_stopping.R)
* [Cross Validation](demo/cross_validation.R)
* [Multiclass Training/Prediction](demo/multiclass.R)
* [Leaf (in)Stability](demo/leaf_stability.R)
* [Weight-Parameter Adjustment Relationship](demo/weight_param.R)