三步在MacOS Anaconda安装ligthGBM

最近看了很多介绍在MacOS 安装lightGBM包的文章,大部分都是Build from Source的方法,这个方法是之前唯一可行的方法,比较繁琐,而且容易出错。其实,现在安装已经很简单,只需要3步左右。

三步安装ligthGBM

1. 安装homebrew,建议通过国内镜像来安装,国外镜像容易出现下载问题。

1)进入terminal终端(可通过 聚焦搜索 “终端”,点击进入);

2)在终端输入如下代码(此为国内镜像源);

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

3)会出现“开始执行Brew自动安装程序”的命令,按照指示输入 镜像序号-->Y-->password(电脑开机密码)即可安装好HomeBrew。

 2. 使用homebrew安装lightgbm

只需要一行代码,在terminal中输入

brew install lightgbm

3. 安装lightgbm的python版本

在Jupyter notebook中输入

pip install wheel
pip install lightgbm

然后试试是否安装好了

import lightgbm as lgb

OK!

折腾历史

1.比如下面这边文章介绍得非常详细:

MacBook Pro中安装LightGBM的超详细步骤 - 知乎

文中介绍的是Build from GitHub方法(文章使用国内镜像来安装homebrew,很快就成功了,我上次使用国际镜像来安装homebrew,文件下载一直出问题,折腾了很久)

但是到配置环境变量这个步骤时就出错了。

3)构建算法依赖;

cmake ..

make -j4

cmake ..

make -j4

CMake Error: The source directory "/Users/feilong" does not appear to contain CMakeLists.txt.

Specify --help for usage, or press the help button on the CMake GUI.

make: *** No targets specified and no makefile found. Stop.

对CMake编译也不是很熟悉,这个问题就卡住了。

2. 然后去看了最新的官方文档,Installation Guide — LightGBM 3.3.2.99 documentation

这里说一下:去看最新的官方文档是一个非常好的解决问题的办法,很多时候,很多算法包随着系统的升级,安装方法会越来约简单,以前非常复杂的安装过程,到后来就只需要一行代码。

官方介绍的安装lightgbm最简单的方法是使用homebrew,就一行代码

brew install lightgbm

很快也安装好了

(base) feilong@bogon ~ % brew install lightgbm

==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/libomp-13.0.0.catalina.bottle.

######################################################################## 100.0%

==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/lightgbm-3.3.1.catalina.bottle

#=#=#                                                                         

curl: (22) The requested URL returned error: 404 

Warning: Bottle missing, falling back to the default domain...

==> Downloading https://ghcr.io/v2/homebrew/core/lightgbm/manifests/3.3.1

######################################################################## 100.0%

==> Downloading https://ghcr.io/v2/homebrew/core/lightgbm/blobs/sha256:4b6a53e88126659be4d6c0a18e6d

==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:4b6a53e8812665

######################################################################## 100.0%

==> Installing dependencies for lightgbm: libomp

==> Installing lightgbm dependency: libomp

==> Pouring libomp-13.0.0.catalina.bottle.tar.gz

🍺  /usr/local/Cellar/libomp/13.0.0: 9 files, 1.6MB

==> Installing lightgbm

==> Pouring lightgbm--3.3.1.catalina.bottle.tar.gz

🍺  /usr/local/Cellar/lightgbm/3.3.1: 98 files, 18.1MB

==> Running `brew cleanup lightgbm`...

Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.

Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

然后check一下,确实安装好了

(base) feilong@bogon ~ % brew list
==> Formulae
ca-certificates	gmp		libevent	lightgbm	openssl@1.1
cmake		hwloc		libmpc		mpfr		zstd
gcc		isl		libomp		open-mpi

 然后在jupyter notebook中导入lightgbm

import lightgbm as lgm

提示错误信息: 

ImportError: No module named lightgbm

查了一些文档,说brew install lightgbm只是安装了command line interface(CLI)版本的lightgbm,此时是不能直接在python中使用的,需要安装python版本的lightgbm,即:

https://github.com/microsoft/LightGBM/tree/master/python-package#installation 

我在jupyter notebook中输入了两个命令:

pip install wheel
pip install lightgbm

然后在导入lightgbm,就可以了,大功告成!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值