README.rst 14.4 KB
Newer Older
1
LightGBM Python-package
Guolin Ke's avatar
Guolin Ke committed
2
3
=======================

4
|License| |Python Versions| |PyPI Version| |PyPI Downloads| |conda Version| |conda Downloads| |API Docs|
Guolin Ke's avatar
Guolin Ke committed
5

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

Guolin Ke's avatar
Guolin Ke committed
9
10
11
Preparation
'''''''''''

12
13
14
15
16
32-bit Python is not supported.
Please install 64-bit version.
If you have a strong need to install with 32-bit Python, refer to `Build 32-bit Version with 32-bit Python section <#build-32-bit-version-with-32-bit-python>`__.

|
Guolin Ke's avatar
Guolin Ke committed
17

18
19
Install from `PyPI <https://pypi.org/project/lightgbm>`_
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
20

21
22
23
24
.. code:: sh

    pip install lightgbm

25
26
27
28
29
30
31
32
Compiled library that is included in the wheel file supports both **GPU** (don't confuse with CUDA version) and **CPU** versions out of the box.
This feature is available only for **Windows** and **Linux** currently.
To use **GPU** version you only need to install OpenCL Runtime libraries.
For NVIDIA and AMD GPU they are included in the ordinary drivers for your graphics card, so no action is required.
If you would like your AMD or Intel CPU to act like a GPU (for testing and debugging),
you can install `AMD APP SDK <https://github.com/microsoft/LightGBM/releases/download/v2.0.12/AMD-APP-SDKInstaller-v3.0.130.135-GA-windows-F-x64.exe>`_ on **Windows** and `PoCL <https://portablecl.org>`_ on **Linux**.
Many modern Linux distributions provide packages for PoCL, look for ``pocl-opencl-icd`` on Debian-based distributions and ``pocl`` on RedHat-based distributions.

33
For **Windows** users, `VC runtime <https://learn.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist>`_ is needed if **Visual Studio** is not installed.
34
35
36

For **macOS** users, the **OpenMP** library is needed.
You can install it by the following command: ``brew install libomp``.
37

38
|
Guolin Ke's avatar
Guolin Ke committed
39

40
41
Use LightGBM with PyArrow
*************************
42

43
To install all dependencies needed to use ``PyArrow`` in LightGBM, append ``[arrow]``.
44

45
.. code:: sh
46

47
    pip install 'lightgbm[arrow]'
48

49
|
50

51
52
53
Use LightGBM with Dask
**********************

54
Warning: Dask-package is only tested on macOS and Linux.
55
56
57
58
59
60
61

To install all dependencies needed to use ``lightgbm.dask``, append ``[dask]``.

.. code:: sh

    pip install 'lightgbm[dask]'

62
63
|

64
65
66
67
68
69
70
71
72
Use LightGBM with pandas
************************

To install all dependencies needed to use ``pandas`` in LightGBM, append ``[pandas]``.

.. code:: sh

    pip install 'lightgbm[pandas]'

73
74
75
76
77
78
79
80
81
82
83
84
85
|

Use LightGBM Plotting Capabilities
**********************************

To install all dependencies needed to use ``lightgbm.plotting``, append ``[plotting]``.

.. code:: sh

    pip install 'lightgbm[plotting]'

|

86
87
88
Use LightGBM with scikit-learn
******************************

89
To install all dependencies needed to use ``lightgbm.sklearn``, append ``[scikit-learn]``.
90
91
92
93
94

.. code:: sh

    pip install 'lightgbm[scikit-learn]'

95
96
|

97
Build from Sources
98
******************
Guolin Ke's avatar
Guolin Ke committed
99

100
.. code:: sh
Guolin Ke's avatar
Guolin Ke committed
101

102
    pip install lightgbm --no-binary lightgbm
103

104
105
For **macOS** users, you can perform installation either with **Apple Clang** or **gcc**.

106
- In case you prefer **Apple Clang**, you should install **OpenMP** (details for installation can be found in `Installation Guide <https://github.com/microsoft/LightGBM/blob/master/docs/Installation-Guide.rst#apple-clang>`__) first.
107

108
- In case you prefer **gcc**, you need to install it (details for installation can be found in `Installation Guide <https://github.com/microsoft/LightGBM/blob/master/docs/Installation-Guide.rst#gcc-1>`__) and specify compilers by running ``export CXX=g++-7 CC=gcc-7`` (replace "7" with version of **gcc** installed on your machine) first.
Guolin Ke's avatar
Guolin Ke committed
109

110
For **Windows** users, **Visual Studio** (or `VS Build Tools <https://visualstudio.microsoft.com/downloads/>`_) is needed.
Guolin Ke's avatar
Guolin Ke committed
111

112
113
|

114
115
116
117
118
Build Threadless Version
~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: sh

119
    pip install lightgbm --no-binary lightgbm --config-settings=cmake.define.USE_OPENMP=OFF
120

121
All requirements, except the **OpenMP** requirement, from `Build from Sources section <#build-from-sources>`__ apply for this installation option as well.
122
123
124

It is **strongly not recommended** to use this version of LightGBM!

125
126
|

127
128
129
130
131
Build MPI Version
~~~~~~~~~~~~~~~~~

.. code:: sh

132
    pip install lightgbm --no-binary lightgbm --config-settings=cmake.define.USE_MPI=ON
133

134
All requirements from `Build from Sources section <#build-from-sources>`__ apply for this installation option as well.
135

136
For **Windows** users, compilation with **MinGW-w64** is not supported.
137

138
**MPI** libraries are needed: details for installation can be found in `Installation Guide <https://github.com/microsoft/LightGBM/blob/master/docs/Installation-Guide.rst#build-mpi-version>`__.
139

140
141
|

142
Build GPU Version
143
~~~~~~~~~~~~~~~~~
Guolin Ke's avatar
Guolin Ke committed
144

145
.. code:: sh
146

147
    pip install lightgbm --no-binary lightgbm --config-settings=cmake.define.USE_GPU=ON
148

149
All requirements from `Build from Sources section <#build-from-sources>`__ apply for this installation option as well.
150

151
152
153
154
For **macOS** users, the GPU version is not supported.

**Boost** and **OpenCL** are needed: details for installation can be found in `Installation Guide <https://github.com/microsoft/LightGBM/blob/master/docs/Installation-Guide.rst#build-gpu-version>`__.
Almost always you also need to pass ``OpenCL_INCLUDE_DIR``, ``OpenCL_LIBRARY`` options for **Linux** and ``BOOST_ROOT``, ``BOOST_LIBRARYDIR`` options for **Windows** to **CMake** via ``pip`` options, like
155
156
157

.. code:: sh

158
    pip install lightgbm --no-binary lightgbm --config-settings=cmake.define.USE_GPU=ON --config-settings=cmake.define.OpenCL_INCLUDE_DIR="/usr/local/cuda/include/" --config-settings=cmake.define.OpenCL_LIBRARY="/usr/local/cuda/lib64/libOpenCL.so"
159

160
All available options that can be passed via ``cmake.define.{option}``.
161

162
- BOOST_ROOT
163

164
- Boost_DIR
165

166
- Boost_INCLUDE_DIR
167

168
- BOOST_LIBRARYDIR
169

170
- OpenCL_INCLUDE_DIR
171

172
- OpenCL_LIBRARY
173

174
For more details see `FindBoost <https://cmake.org/cmake/help/latest/module/FindBoost.html>`__ and `FindOpenCL <https://cmake.org/cmake/help/latest/module/FindOpenCL.html>`__.
175

176
177
178
179
180
Don't confuse with `CUDA version <#build-cuda-version>`__.
To use the GPU version within Python, pass ``{"device": "gpu"}`` respectively in parameters.

|

181
182
183
184
185
Build CUDA Version
~~~~~~~~~~~~~~~~~~

.. code:: sh

186
    pip install lightgbm --no-binary lightgbm --config-settings=cmake.define.USE_CUDA=ON
187

188
All requirements from `Build from Sources section <#build-from-sources>`__ apply for this installation option as well.
189

190
191
For **macOS** and **Windows** users, the CUDA version is not supported.

192
**CUDA** library is needed: details for installation can be found in `Installation Guide <https://github.com/microsoft/LightGBM/blob/master/docs/Installation-Guide.rst#build-cuda-version>`__.
193

194
Don't confuse with `GPU version <#build-gpu-version>`__.
195
To use the CUDA version within Python, pass ``{"device": "cuda"}`` respectively in parameters.
196

197
198
|

199
200
201
202
203
Build with MinGW-w64 on Windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: sh

204
    pip install lightgbm --no-binary lightgbm --config-settings=cmake.define.CMAKE_SH=CMAKE_SH-NOTFOUND --config-settings=cmake.args="-GMinGW Makefiles"
205

206
`MinGW-w64 <https://www.mingw-w64.org/>`_ should be installed first.
207

208
It is recommended to use **Visual Studio** for its better multithreading efficiency in **Windows** for many-core systems
209
210
211
212
(see `Question 4 <https://github.com/microsoft/LightGBM/blob/master/docs/FAQ.rst#4-i-am-using-windows-should-i-use-visual-studio-or-mingw-for-compiling-lightgbm>`__
and `Question 8 <https://github.com/microsoft/LightGBM/blob/master/docs/FAQ.rst#8-cpu-usage-is-low-like-10-in-windows-when-using-lightgbm-on-very-large-datasets-with-many-core-systems>`__).

|
Guolin Ke's avatar
Guolin Ke committed
213

214
215
216
217
218
Build 32-bit Version with 32-bit Python
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: sh

219
220
221
    pip install lightgbm --no-binary lightgbm --config-settings=cmake.args="-AWin32"

For **Windows** users, compilation with **MinGW-w64** is not supported.
222

223
For **macOS** and **Linux** users, the 32-bit version is not supported.
224
225
226

It is **strongly not recommended** to use this version of LightGBM!

227
228
|

229
Build with Time Costs Output
230
231
232
233
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: sh

234
    pip install lightgbm --no-binary lightgbm --config-settings=cmake.define.USE_TIMETAG=ON
235
236
237

Use this option to make LightGBM output time costs for different internal routines, to investigate and benchmark its performance.

238
239
|

240
241
242
Install from `conda-forge channel <https://anaconda.org/conda-forge/lightgbm>`_
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

243
244
245
246
247
248
``lightgbm`` conda packages are available from the ``conda-forge`` channel.

.. code:: sh

    conda install -c conda-forge lightgbm

249
These packages support **CPU**, **GPU** and **CUDA** versions out of the box.
250

251
**GPU**-enabled version is available only for **Windows** and **Linux** currently.
252

253
**CUDA**-enabled version (since ``lightgbm>=4.4.0``) is available only for **Linux** currently and will be automatically selected if you are on a system where CUDA is installed.
254

255
|
256

Guolin Ke's avatar
Guolin Ke committed
257
258
Install from GitHub
'''''''''''''''''''
Guolin Ke's avatar
Guolin Ke committed
259

260
All requirements from `Build from Sources section <#build-from-sources>`__ apply for this installation option as well.
Guolin Ke's avatar
Guolin Ke committed
261

Guolin Ke's avatar
Guolin Ke committed
262
263
.. code:: sh

264
    git clone --recursive https://github.com/microsoft/LightGBM.git
265
    cd LightGBM
266
    # export CXX=g++-14 CC=gcc-14  # macOS users, if you decided to compile with gcc, don't forget to specify compilers
267
    sh ./build-python.sh install
Guolin Ke's avatar
Guolin Ke committed
268

269
Note: ``sudo`` (or administrator rights in **Windows**) may be needed to perform the command.
270
Run ``sh ./build-python.sh install --user`` to install into user-specific instead of global site-packages directory.
271

272
Run ``sh ./build-python.sh install --no-isolation`` to assume all build and install dependencies are already installed, don't go to the internet to get them.
273

274
|
Guolin Ke's avatar
Guolin Ke committed
275

276
277
Run ``sh ./build-python.sh install --nomp`` to disable **OpenMP** support.
All requirements from `Build Threadless Version section <#build-threadless-version>`__ apply for this installation option as well.
278

279
280
Run ``sh ./build-python.sh install --mpi`` to enable **MPI** support.
All requirements from `Build MPI Version section <#build-mpi-version>`__ apply for this installation option as well.
281

282
283
284
285
Run ``sh ./build-python.sh install --gpu`` to enable GPU support.
All requirements from `Build GPU Version section <#build-gpu-version>`__ apply for this installation option as well.
To pass additional options to **CMake** use the following syntax: ``sh ./build-python.sh install --gpu --opencl-include-dir="/usr/local/cuda/include/"``,
see `Build GPU Version section <#build-gpu-version>`__ for the complete list of them.
286

287
288
Run ``sh ./build-python.sh install --cuda`` to enable CUDA support.
All requirements from `Build CUDA Version section <#build-cuda-version>`__ apply for this installation option as well.
289

290
291
Run ``sh ./build-python.sh install --mingw``, if you want to use **MinGW-w64** on **Windows** instead of **Visual Studio**.
All requirements from `Build with MinGW-w64 on Windows section <#build-with-mingw-w64-on-windows>`__ apply for this installation option as well.
292

293
294
Run ``sh ./build-python.sh install --bit32``, if you want to use 32-bit version.
All requirements from `Build 32-bit Version with 32-bit Python section <#build-32-bit-version-with-32-bit-python>`__ apply for this installation option as well.
wxchan's avatar
wxchan committed
295

296
297
Run ``sh ./build-python.sh install --time-costs``, if you want to output time costs for different internal routines.
All requirements from `Build with Time Costs Output section <#build-with-time-costs-output>`__ apply for this installation option as well.
298

299
|
300

301
302
303
304
If you get any errors during installation or due to any other reasons,
you may want to build dynamic library from sources by any method you prefer
(see `Installation Guide <https://github.com/microsoft/LightGBM/blob/master/docs/Installation-Guide.rst>`__).
For example, you can use ``MSBuild`` tool and `solution file <https://github.com/microsoft/LightGBM/blob/master/windows/LightGBM.sln>`__ from the repo.
305
306
307

.. code:: sh

308
  MSBuild.exe windows/LightGBM.sln /p:Configuration=DLL /p:Platform=x64 /p:PlatformToolset=v143
309

310
After compiling dynamic library just run ``sh ./build-python.sh install --precompile`` to install the Python-package using that library.
311

312
|
313

314
315
Build Wheel File
****************
316

317
You can run ``sh ./build-python.sh bdist_wheel`` to build a wheel file but not install it.
318

319
320
That script requires some dependencies like ``build``, ``scikit-build-core``, and ``wheel``.
In environments with restricted or no internet access, install those tools and then pass ``--no-isolation``.
321
322
323

.. code:: sh

324
  sh ./build-python.sh bdist_wheel --no-isolation
325

wxchan's avatar
wxchan committed
326
Troubleshooting
Guolin Ke's avatar
Guolin Ke committed
327
---------------
wxchan's avatar
wxchan committed
328

329
Refer to `FAQ <https://github.com/microsoft/LightGBM/tree/master/docs/FAQ.rst>`_.
wxchan's avatar
wxchan committed
330

331
332
333
Examples
--------

334
Refer to the walk through examples in `Python guide folder <https://github.com/microsoft/LightGBM/tree/master/examples/python-guide>`_.
335

336
337
338
339
340
341
Supported Python Versions
-------------------------

This project supports all Python versions until they reach end-of-life.
For details on the support calendar for Python versions, see https://devguide.python.org/versions/.

342
343
Development Guide
-----------------
wxchan's avatar
wxchan committed
344

345
346
347
348
To check that a contribution to the package matches its style expectations, run the following from the root of the repo.

.. code:: sh

349
    bash .ci/run-pre-commit-mypy.sh
350

351
352
353
354
355
356
357
358
359
360
361
362
363
To run the tests locally and compute test coverage, install the Python package using one of the options mentioned above.
Then run the following from the root of the repo.

.. code:: sh

    pytest \
        --cov=lightgbm \
        --cov-report="term" \
        --cov-report="html:htmlcov" \
        tests/python_package_test/

Then open `htmlcov/index.html` to view a clickable coverage report.

364
.. |License| image:: https://img.shields.io/github/license/microsoft/lightgbm.svg
365
   :target: https://github.com/microsoft/LightGBM/blob/master/LICENSE
366
.. |Python Versions| image:: https://img.shields.io/pypi/pyversions/lightgbm.svg?logo=python&logoColor=white
367
   :target: https://pypi.org/project/lightgbm
368
.. |PyPI Version| image:: https://img.shields.io/pypi/v/lightgbm.svg?logo=pypi&logoColor=white
369
   :target: https://pypi.org/project/lightgbm
370
.. |PyPI Downloads| image:: https://img.shields.io/pepy/dt/lightgbm?logo=pypi&logoColor=white&label=pypi%20downloads
371
   :target: https://pepy.tech/project/lightgbm
372
373
.. |conda Version| image:: https://img.shields.io/conda/vn/conda-forge/lightgbm?logo=conda-forge&logoColor=white&label=conda
   :target: https://anaconda.org/conda-forge/lightgbm
374
375
.. |conda Downloads| image:: https://img.shields.io/conda/d/conda-forge/lightgbm?logo=conda-forge&logoColor=white&label=conda%20downloads
   :target: https://anaconda.org/conda-forge/lightgbm/files
Nikita Titov's avatar
Nikita Titov committed
376
377
.. |API Docs| image:: https://readthedocs.org/projects/lightgbm/badge/?version=latest
   :target: https://lightgbm.readthedocs.io/en/latest/Python-API.html