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

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

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

You can use a command prompt to install via command line:

Guolin Ke's avatar
Guolin Ke committed
11
12
13
```
cd R-package
R CMD INSTALL --build  .
Guolin Ke's avatar
Guolin Ke committed
14
15
```

16
17
18
19
20
21
You can also install directly from R using the repository with `devtools`:

```r
devtools::install_github("Microsoft/LightGBM", subdir = "R-package")
```

22
For the `devtools` install scenario, you can safely ignore this message:
23

24
25
26
27
```r
Warning message:
GitHub repo contains submodules, may not function as expected! 
```
28

29
30
31
If you want to build the self-contained R package, you can run ```unix_build_package.sh```(for UNIX) or ```win_build_package.cmd ```(for Windows). Then use ```R CMD INSTALL lightgbm_0.1.tar.gz``` to install.

When your package installation is done, you can check quickly if your LightGBM R package is working by running the following:
32
33
34
35
36
37
38
39
40

```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)
```
Guolin Ke's avatar
Guolin Ke committed
41
42
### OSX installation 

43
44
45
46
47
The default installation cannot successfully complete in OSX because clang doesn't support OpenMP.

You can use the following script to change default compiler to gcc, then compile LightGBM R package:

```bash
Guolin Ke's avatar
Guolin Ke committed
48
brew install gcc --without-multilib
49
mkdir -p ~/.R
Guolin Ke's avatar
Guolin Ke committed
50
touch ~/.R/Makevars
51
cat <<EOF >>~/.R/Makevars
Guolin Ke's avatar
Guolin Ke committed
52
53
54
C=gcc-6
CXX=g++-6
CXX1X=g++-6
55
56
LDFLAGS=-L/usr/local/Cellar/gcc/6.3.0/lib
CPPFLAGS=-I/usr/local/Cellar/gcc/6.3.0/include
Guolin Ke's avatar
Guolin Ke committed
57
58
59
60
SHLIB_OPENMP_CFLAGS = -fopenmp
SHLIB_OPENMP_CXXFLAGS = -fopenmp
SHLIB_OPENMP_FCFLAGS = -fopenmp
SHLIB_OPENMP_FFLAGS = -fopenmp
61
EOF
Guolin Ke's avatar
Guolin Ke committed
62
63
```

64
65
66
67
68
Note:

* For `LDFLAGS=-L/usr/local/Cellar/gcc/6.3.0/lib` and `CPPFLAGS=-I/usr/local/Cellar/gcc/6.3.0/include`, you may need to change `6.3.0` to your gcc version.
* For `gcc-6` and `g++-6`, you may need to change to your gcc version (like `gcc-7` and `g++7` if using gcc with version 7).
* For `CXX1X`, if you are using R 3.4 or a more recent version, you must change it to `CXX11`.
69
70

To check your LightGBM installation, the test is identical to Linux/Windows versions (check the test provided just before OSX Installation part)
Guolin Ke's avatar
Guolin Ke committed
71

72
73
74
75
76
77
78
79
80
81
82
83
84
85
Performance note
------------

With `gcc`, it is recommended to use `-O3 -mtune=native` instead of the default `-O2 -mtune=core2` by modifying the appropriate file (`Makeconf` or `Makevars`) if you want to achieve maximum speed.

Benchmark example using Intel Ivy Bridge CPU on 1M x 1K dataset:

| Compilation Flag | Performance Index |
| --- | ---: |
| `-O2 -mtune=core2` | 100.00% |
| `-O2 -mtune=native` | 100.90% |
| `-O3 -mtune=native` | 102.78% |
| `-O3 -ffast-math -mtune=native` | 100.64% |

Guolin Ke's avatar
Guolin Ke committed
86
Examples
87
------------
Guolin Ke's avatar
Guolin Ke committed
88

89
90
91
92
93
94
95
96
97
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)