Mac下安装lightgbm

时间:2019-12-20 20:15:21   收藏:0   阅读:1380

Mac下安装lightgbm

1.安装环境

$ clang -v
Apple LLVM version 10.0.0 (clang-1000.11.45.5)
Target: x86_64-apple-darwin18.2.0
Thread model: posix

2.安装错误

直接使用命令:

import lightgbm as lgb

使用时直接调用:
import lightgbm as lgb

import lightgbm as lgb

然后在pycharm、notebook中使用时会报错:

OSError: dlopen(/Usrs/chao/.pyenv/versions/3.6.2/lib/python3.6/site-packages/lightgbm/lib_lightgbm.so, 6): Library not loaded: /Usrs/chao/.../libgomp.1.dylib
Referenced from: /Usrs/chao/.pyenv/versions/3.6.2/lib/python3.6/site-packages/lightgbm/lib_lightgbm.so
Reason: image not found

那么请先执行:这一步会卸载原来错误安装的库

pip uninstall lightgbm

3.正确安装

保证之前错误的安装已经卸载掉了,然后进行下面的操作。

1.确保homebrew安装,并已经更新

brew update
brew upgrade

2.安装cmake依赖

brew install cmake
brew install gcc@7 --without-multilib
git clone --recursive https://github.com/Microsoft/LightGBM ; 
cd LightGBM
mkdir build ; 
cd build

cmake ..  # 这一步会出错,看下方编译部分
make -j

3.编译

在执行编译cmake..的时候出现如下错误

CMake Error at /usr/local/Cellar/cmake/3.15.5/share/cmake/Modules/CMakeDetermineCCompiler.cmake:49 (message):
  Could not find compiler set in environment variable CC:

  gcc-8.
Call Stack (most recent call first):
  CMakeLists.txt:7 (PROJECT)

这个时候我们需要更换命令,注意查看自己电脑的gcc版本

cd /usr/local/bin
# 查看文件中的g++和gcc版本

我的是g++-9gcc-9版本

cmake -DCMAKE_CXX_COMPILER=g++-9 -DCMAKE_C_COMPILER=gcc-9 ..

执行结果如下

-- The C compiler identification is GNU 9.2.0
-- The CXX compiler identification is GNU 9.2.0
// ...
-- Found OpenMP: TRUE   
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/ryxiong/LightGBM/build

4.构建

make -j4
Scanning dependencies of target lightgbm
Scanning dependencies of target _lightgbm
[  1%] Building CXX object CMakeFiles/_lightgbm.dir/src/io/dataset.cpp.o
[  3%] Building CXX object CMakeFiles/lightgbm.dir/src/io/dataset.cpp.o
// ...
[ 92%] Building CXX object CMakeFiles/_lightgbm.dir/src/treelearner/voting_parallel_tree_learner.cpp.o
[ 93%] Building CXX object CMakeFiles/_lightgbm.dir/src/metric/metric.cpp.o
[ 95%] Building CXX object CMakeFiles/lightgbm.dir/src/network/linkers_mpi.cpp.o
[ 96%] Building CXX object CMakeFiles/_lightgbm.dir/src/io/tree.cpp.o
[100%] Linking CXX executable ../lightgbm
[100%] Linking CXX shared library ../lib_lightgbm.so
[100%] Built target lightgbm
[100%] Built target _lightgbm
cd ..
ls

错误处理

如果出现以下错误:

/usr/local/Cellar/gcc@7/7.3.0/lib/gcc/7/gcc/x86_64-apple-darwin17.5.0/7.3.0/include-fixed/stdio.h:78:10: fatal error: _stdio.h: No such file or directory
 #include <_stdio.h>
          ^~~~~~~~~~
compilation terminated.
Copyright (c) 2000, 2005, 2007, 2009, 2010 Apple Inc. All rights reserved.
/usr/local/Cellar/gcc@7/7.3.0/lib/gcc/7/gcc/x86_64-apple-darwin17.5.0/7.3.0/incl

再重新执行第4步,然后项目构建成功。

安装lightgbm

cd python-package
pip install lightgbm
$ python
>>> import lightgbm as lgb
>>> lgb.LGBMClassifier()
LGBMClassifier(boosting_type='gbdt', class_weight=None, colsample_bytree=1.0,
        learning_rate=0.1, max_depth=-1, min_child_samples=20,
        min_child_weight=0.001, min_split_gain=0.0, n_estimators=100,
        n_jobs=-1, num_leaves=31, objective=None, random_state=None,
        reg_alpha=0.0, reg_lambda=0.0, silent=True, subsample=1.0,
        subsample_for_bin=200000, subsample_freq=1)
ImportError: No module named "lightgbm"

手动配置

cp lib_lightgbm.so /Users/ricky/LightGBM/build/LightGBM/build/LightGBM/python-package/lightgbm/
cp -r /Users/ricky/LightGBM/build/LightGBM/build/LightGBM/python-package/lightgbm ./
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!