本博客均整理自网上搜到的神奇方法,感谢原博客作者!

附上原博客链接:https://blog.csdn.net/weixin_41843918/article/details/85047492

 

 Mac安装LightGBM

 

0. 普通安装遇到的问题

如果我们安装普通安装方式在Mac上直接使用pip安装LightGBM,大概率会在运行时出现以下报错,无论是终端,Pycharm,还是Jupyter 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

如果遇到以上问题了,这是因为Mac系统缺少一些该库正常运行所需要的依赖和环境,网上给出了很多大同小异的解决方案,均因为系统不同或者XCode的版本不同而导致不一定能成功。

首先,遇到上述问题请直接卸载安装的LightGBM包:

pip uninstall lightgbm

如果上述语句执行完以后,发现Mac还遗留有名为LightGBM的文件夹,请务必自行删除!

本人尝试了很多种方式,花了几个小时才终于找到了一种方法能够完美搞定。下面贴上本人的环境:

 

1. 安装环境

  • 系统
    • macOS Mojave
    • 版本:10.14.3
  • XCode
    • 10.1

 

2. 正确安装

2.1 安装并更新homebrew

在保证卸载了错误安装的LightGBM包之后,请首先在Mac上安装homebrew。

安装方法是打开homebrew的官网,会提供安装代码:

Mac下安装lightgbm “image not found”问题解决方法

请注意,homebrew的安装代码和版本会更新,请一定亲自去homebrew官网使用最新的安装代码!

如果homebrew已经安装过,也请更新homebrew。

2.2 安装cmake依赖

homebrew搞定之后,就是安装cmake依赖。请依次执行以下代码,不建议一次性复制粘贴,最好是一句一句地粘贴:

brew install cmake
brew install [email protected] 
git clone --recursive https://github.com/Microsoft/LightGBM 
cd LightGBM
mkdir build 
cd build

这里请注意,有人的gcc可能是gcc8.x的版本,本人没有尝试过gcc8.x的情况,只能保证gcc7版本能够成功,gcc8.x的情况请诸君自行尝试,如若不行,请务必换成gcc7,从头重新执行本博客的安装过程!

确保上面的语句都执行正确,进入build文件夹之后,再开始以下步骤。

2.3 编译

在build文件夹下,命令行执行:

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

正确执行结果类似于下面这样:

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

2.4 构建

命令行执行:

make -j

如果没有报错,会显示以下结果:

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

到了这里,理论上来说会在上级目录LightGBM下生成lib_lightgbm.solightgbm两个文件。

可执行以下语句查看:

cd ..
ls

如果显示出来的文件里包含上面说的两个文件,那么到此执行正确,可直接执行2.6步,否则请看2.5步。

2.5 错误处理(本错误处理是原博客主给出的,本人没有遇到此错误)

如果出现以下错误:

/usr/local/Cellar/[email protected]/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.

在Finder里“按名称匹配”搜索_stdio.h文件,找到:

Copyright (c) 2000, 2005, 2007, 2009, 2010 Apple Inc. All rights reserved.

注释信息开头的_stdio.h,复制到上面错误提示的目录下:

/usr/local/Cellar/[email protected]/7.3.0/lib/gcc/7/gcc/x86_64-apple-darwin17.5.0/7.3.0/include-fixed/

再重新执行2.4步,然后项目构建成功,进入2.6步。

2.6 安装LightGBM

现在开始安装LightGBM了,因为项目环境已经构建好了,所以可以安装LightGBM了。命令行执行:

cd python-package
pip install lightgbm

如果提示安装成功,可以重新打开一个终端,输入以下代码看看是否成功安装:

$ python
>>> import lightgbm as lgb
>>> lgb.LGBMClassifier()

如果安装成功会显示以下信息:

>>> import lightgbm as lgb
/Users/leonardo_liu/Anaconda3/anaconda3/lib/python3.7/lightgbm/__init__.py:47: UserWarning: Starting from version 2.2.1, the library file in distribution wheels for macOS is built by the Apple Clang (Xcode_8.3.3) compiler.
This means that in case of installing LightGBM from PyPI via the ``pip install lightgbm`` command, you don't need to install the gcc compiler anymore.
Instead of that, you need to install the OpenMP library, which is required for running LightGBM on the system with the Apple Clang compiler.
You can install the OpenMP library by the following command: ``brew install libomp``.
  "You can install the OpenMP library by the following command: ``brew install libomp``.", UserWarning)
>>> lgb.LGBMClassifier()
LGBMClassifier(boosting_type='gbdt', class_weight=None, colsample_bytree=1.0,
        importance_type='split', 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=0)

上面的warning可以不用理睬,我尝试着安装了OpenMP之后还会接着警告。

只要显示出了LightGBM的模型参数,就说明LightGBM是可以正常使用的啦!

到此,终端上就可以正常使用LightGBM了,但是Pycharm,Jupyter Notebook上不一定,有可能大概率会继续报最一开始看到的那种Image not found的错误,不要惊慌,接着完成2.7步就大功告成了!

2.7 手动配置

因为我们在此之前只是把LightGBM成功安装到了我们自己搭建的项目环境中,而Pycharm,Jupyter Notebook等很可能是自动关联的anaconda环境,所以报错是因为anaconda环境里还没有我们的LightGBM包的必须环境。

我们进入LightGBM文件夹,将其中的lib_lightgbm.so文件复制到LightGBM/python-package/lightgbm/目录下,再将LightGBM/python-package里面的lightgbm整个文件夹拷贝到anaconda里面的包目录下。

具体的LightGBM的目录和anaconda包目录根据个人电脑不同。

我的LightGBM文件夹就在磁盘/用户/“账户名称”(根据个人电脑账户名称)里面,也可以在Mac上“按照名称匹配”搜索LightGBM,注意,一定是大写的L,G,B,M!

我的anaconda包目录为:

anaconda3/lib/python3.7

这里注意,如果之前错误安装过LightGBM,可能anaconda的包目录下会有一个名为lightgbm的文件夹,如果有的话请直接将其删除,然后再粘贴!

到此,粘贴完之后,就大功告成啦!

相关文章: