Mac安装lightGBM

2023-04-27 18:04:34 浏览数 (2)

https://lightgbm.readthedocs.io/en/latest/Installation-Guide.html#macos

官网提供了三种安装方式 (通过不断尝试采坑,第三种方式gcc可以安装成功)

正确安装过程可以直接跳到第三种方法查看!!!


第一种 -- 虽然安装了,brew list也能看到lightgbm包,但是有以下问题:

  • python import lightgbm报错
  • 再次执行pip install lightgbm成功,但是运行程序仍然报错
  • 所以这第一种方法不可行,删除 pip uninstall lightgbm

brew install lightgbm

第二种 -- 通过cmake和OpenMP

代码语言:javascript复制
brew install cmake 
brew install libomp 
git clone --recursive https://github.com/microsoft/LightGBM 

当执行git clone时,报错

*****@localhost:~/opt/software$ git clone --recursive https://github.com/microsoft/LightGBM

-bash: /usr/local/bin/git: No such file or directory

fix方法是执行 brew install git

再次执行,问题解决

*****@localhost:~/opt/software$ git clone --recursive https://github.com/microsoft/LightGBM

正克隆到 'LightGBM'...

fatal: 无法访问 'https://github.com/microsoft/LightGBM/':LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443

fix方法是,git clone --recursive git://github.com/microsoft/LightGBM (把https改成git即可下载),以下是log信息

*****@localhost:~/opt/software$ git clone --recursive git://github.com/microsoft/LightGBM

正克隆到 'LightGBM'...

代码语言:javascript复制
remote: Enumerating objects: 25038, done.
remote: Counting objects: 100% (714/714), done.
remote: Compressing objects: 100% (416/416), done.
remote: Total 25038 (delta 447), reused 478 (delta 292), pack-reused 24324
接收对象中: 100% (25038/25038), 18.73 MiB | 560.00 KiB/s, 完成.

处理 delta 中: 100% (18394/18394), 完成.

子模组 'include/boost/compute'(https://github.com/boostorg/compute)已对路径 'external_libs/compute' 注册

子模组 'eigen'(https://gitlab.com/libeigen/eigen.git)已对路径 'external_libs/eigen' 注册

子模组 'external_libs/fast_double_parser'(https://github.com/lemire/fast_double_parser.git)已对路径 'external_libs/fast_double_parser' 注册

子模组 'external_libs/fmt'(https://github.com/fmtlib/fmt.git)已对路径 'external_libs/fmt' 注册

正克隆到 '/Users/*****/opt/software/LightGBM/external_libs/compute'...

fatal: 无法访问 'https://github.com/boostorg/compute/':LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443

fatal: 无法克隆 'https://github.com/boostorg/compute' 到子模组路径 '/Users/****/opt/software/LightGBM/external_libs/compute'

克隆 'external_libs/compute' 失败。按计划重试

正克隆到 '/Users/*****/opt/software/LightGBM/external_libs/eigen'...

代码语言:javascript复制
remote: Enumerating objects: 114807, done.
remote: Counting objects: 100% (2018/2018), done.
remote: Compressing objects: 100% (966/966), done.
remote: Total 114807 (delta 1348), reused 1671 (delta 1050), pack-reused 112789

接收对象中: 100% (114807/114807), 103.36 MiB | 595.00 KiB/s, 完成.

处理 delta 中: 100% (94205/94205), 完成.

正克隆到 '/Users/*****/opt/software/LightGBM/external_libs/fast_double_parser'...

代码语言:javascript复制
remote: Enumerating objects: 692, done.
remote: Counting objects: 100% (192/192), done.
remote: Compressing objects: 100% (124/124), done.
remote: Total 692 (delta 95), reused 99 (delta 41), pack-reused 500

接收对象中: 100% (692/692), 802.86 KiB | 749.00 KiB/s, 完成.

处理 delta 中: 100% (349/349), 完成.

正克隆到 '/Users/*****/opt/software/LightGBM/external_libs/fmt'...

fatal: 无法访问 'https://github.com/fmtlib/fmt.git/':LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443

fatal: 无法克隆 'https://github.com/fmtlib/fmt.git' 到子模组路径 '/Users/*****/opt/software/LightGBM/external_libs/fmt'

克隆 'external_libs/fmt' 失败。按计划重试

正克隆到 '/Users/*****/opt/software/LightGBM/external_libs/compute'...

代码语言:javascript复制
remote: Enumerating objects: 21733, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 21733 (delta 1), reused 2 (delta 0), pack-reused 21728

接收对象中: 100% (21733/21733), 8.51 MiB | 520.00 KiB/s, 完成.

处理 delta 中: 100% (17567/17567), 完成.

正克隆到 '/Users/*****/opt/software/LightGBM/external_libs/fmt'...

代码语言:javascript复制
remote: Enumerating objects: 28962, done.
remote: Counting objects: 100% (322/322), done.
remote: Compressing objects: 100% (132/132), done.
remote: Total 28962 (delta 196), reused 247 (delta 139), pack-reused 28640

接收对象中: 100% (28962/28962), 13.80 MiB | 507.00 KiB/s, 完成.

处理 delta 中: 100% (19550/19550), 完成.

子模组路径 'external_libs/compute':检出 '36350b7de849300bd3d72a05d8bf890ca405a014'

子模组路径 'external_libs/eigen':检出 '3147391d946bb4b6c68edd901f2add6ac1f31f8c'

子模组路径 'external_libs/fast_double_parser':检出 'ace60646c02dc54c57f19d644e49a61e7e7758ec'

子模组 'benchmark/dependencies/abseil-cpp'(https://github.com/abseil/abseil-cpp.git)已对路径 'external_libs/fast_double_parser/benchmarks/dependencies/abseil-cpp' 注册

子模组 'benchmark/dependencies/double-conversion'(https://github.com/google/double-conversion.git)已对路径 'external_libs/fast_double_parser/benchmarks/dependencies/double-conversion' 注册

代码语言:javascript复制
正克隆到 '/Users/***/opt/software/LightGBM/external_libs/fast_double_parser/benchmarks/dependencies/abseil-cpp'...
remote: Enumerating objects: 16257, done.
remote: Counting objects: 100% (383/383), done.
remote: Compressing objects: 100% (287/287), done.
remote: Total 16257 (delta 216), reused 178 (delta 96), pack-reused 15874

接收对象中: 100% (16257/16257), 10.54 MiB | 699.00 KiB/s, 完成.

处理 delta 中: 100% (12490/12490), 完成.

代码语言:javascript复制
正克隆到 '/Users/*****/opt/software/LightGBM/external_libs/fast_double_parser/benchmarks/dependencies/double-conversion'...
remote: Enumerating objects: 1338, done.
remote: Counting objects: 100% (182/182), done.
remote: Compressing objects: 100% (140/140), done.
remote: Total 1338 (delta 98), reused 85 (delta 35), pack-reused 1156

接收对象中: 100% (1338/1338), 7.14 MiB | 755.00 KiB/s, 完成.

处理 delta 中: 100% (870/870), 完成.

子模组路径 'external_libs/fast_double_parser/benchmarks/dependencies/abseil-cpp':检出 'd936052d32a5b7ca08b0199a6724724aea432309'

子模组路径 'external_libs/fast_double_parser/benchmarks/dependencies/double-conversion':检出 'f4cb2384efa55dee0e6652f8674b05763441ab09'

子模组路径 'external_libs/fmt':检出 'b6f4ceaed0a0a24ccf575fab6c56dd50ccf6f1a9'

子模组路径 'external_libs/compute':检出 '36350b7de849300bd3d72a05d8bf890ca405a014'

子模组路径 'external_libs/fmt':检出 'b6f4ceaed0a0a24ccf575fab6c56dd50ccf6f1a9'

https://blog.csdn.net/chao2016/article/details/80189703

第三种 -- 通过cmake和OpenMP

brew install cmake

brew install open-mpi

brew install gcc 或者 brew install gcc --without-multilib(这个是网上说的,但我用这条命令报错;所以用前面的直接brew install gcc)

git clone 过程如同上面,如果实在如法glone,找一个外企的小伙伴帮忙,可以访问; 但要注意,一定要加 --recursive 否则编译会报错

git clone --recursive git://github.com/microsoft/LightGBM (--recursive一定不能少,否则会少文件,编译的时候提示找不到文件)

代码语言:javascript复制
cd LightGBM 
export CXX=g  -7 CC=gcc-7  # replace "7" with version of gcc installed on your machine 
mkdir build 
cd build 
cmake .. 
make -j4 

注意这里我就采坑了,通过 gcc --version 或者 gcc dumpversion看到的都是13;于是把下面的7改成13,但是编译过程中各种报错,

于是brew uninstall gcc; 再次install提示我gcc的版本是11;真相大白!!!!

***@localhost:~$ brew uninstall gcc

Error: Refusing to uninstall /usr/local/Cellar/gcc/11.2.0_3

because it is required by open-mpi, which is currently installed.

You can override this and force removal with:

brew uninstall --ignore-dependencies gcc

此时还没结束!!!!(这里只是把lightGBM download下来并且编译成功了!!!离成功还差一步!!!) https://blog.csdn.net/qq_15111861/article/details/80767080

我们需要在终端进入cd到lightGBM/python-package下 继续 export CXX=g -11 CC=gcc-11 python setup.py install

遗憾的是,虽然import lightgbm成功了,但是跑程序就会报错,仍然未能解决问题!!!

程序会报一些 early_stop_rounds参数,silent参数等不存在的问题!! 或者是 lightgbm Process finished with exit code SIGSEGV

  • 一开始猜测是不是lightgbm包更新了,版本过高导致有些接口变了?
  • 于是查看版本 import lightgbm as lgb, print(lgb.__version__) 看到版本是3.3.2,而以前代码对应的版本是、3.2.1
  • 于是想着回退版本
  • 在一台新的linux机器上安装lightgbm简直不要太容易; 直接 pip install lightgbm即可; 而且版本也是最新的、3.3.2 同样的代码可以跑通
  • 于是打消了顾虑,觉得可能不是版本问题
  • 当看到linux上跑模型打印的log中的warning信息, /root/anaconda3/lib/python3.8/site-packages/lightgbm/sklearn.py:726: UserWarning: 'early_stopping_rounds' argument is deprecated and will be removed in a future release of LightGBM. Pass 'early_stopping()' callback via 'callbacks' argument instead.
  • 再次觉得应该就是版本过高了,于是回退版本

操作如下

先删除已经编译过的版本

第一步:

代码语言:javascript复制
(base)******@localhost:~/opt/software$ sudo pip uninstall lightgbm
WARNING: The directory '/Users/****/Library/Caches/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you should use sudo's -H flag.
Found existing installation: lightgbm 3.2.1
Uninstalling lightgbm-3.2.1:
  Would remove:
    /Users/***/opt/software/anaconda3/lib/python3.9/site-packages/lightgbm-3.2.1.dist-info/*
    /Users/***/opt/software/anaconda3/lib/python3.9/site-packages/lightgbm/*
Proceed (Y/n)? Y
  Successfully uninstalled lightgbm-3.2.1
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment 
instead: https://pip.pypa.io/warnings/venv

第二步:

cd ~/opt/software

sudo rm -rf LightGBM

~/opt/software 目录下有下载好的LightGBM.tar.gz的安装包,直接通过finder进入该目录解压即可

切换到旧版本lightgbm安装包的过程

  1. 先查看一下分支情况
代码语言:javascript复制
(base) ***@localhost:~/opt/software$ cd LightGBM
(base)***@localhost:~/opt/software/LightGBM$ git status

位于分支 master

您的分支与上游分支 'origin/master' 一致。

无文件要提交,干净的工作区

代码语言:javascript复制
(base) ***@localhost:~/opt/software/LightGBM$ git branch
* master
(base)****@localhost:~/opt/software/LightGBM$ git branch -v
* master f1856956 [python-package] add support for pandas nullable types (fixes #4173) (#4927)
  1. 直接checkout到旧版本

git checkout v3.2.1

然后再次进行如下操作

代码语言:javascript复制
cd LightGBM 
export CXX=g  -11 CC=gcc-11  # replace "7" with version of gcc installed on your machine 
mkdir build 
cd build 
cmake .. 
make -j4 
cd ../python-package 
export CXX=g  -11 CC=gcc-11 
python setup.py install 

成功!!!!!

0 人点赞