在windows64位Anaconda3环境下安装XGBoost

安装步骤参考的是:

“Installing XGBoost For Anaconda on Windows”:https://www.ibm.com/developerworks/community/blogs/jfp/entry/Installing_XGBoost_For_Anaconda_on_Windows?lang=zh


 一、安装前的准备

In order to install and use XGBoost with Python you need three software on your windows machine:


具体实现:

(1)选用Anaconda3的Python3.X版本,然后下载 Git并安装好。

(2)在C:\Users\Administrator路径下创建XGBoost文件夹,在此文件下右击鼠标菜单选择Git Bash

(3)Then download XGBoost by typing the following commands. 

$ git clone --recursive https://github.com/dmlc/xgboost
$ cd xgboost
$ git submodule init
$ git submodule update

(4)安装 MinGW-W64 下载地址

(5)点击Next后选择 x86_64这项,其他选项不要改:

默认安装地址:

C:\Program Files\mingw-w64\x86_64-7.2.0-posix-seh-rt_v5-rev1

(6)将Git和mingw32-make所在路径分别添加到系统环境变量中:

E:\program Files\Git\cmd
C:\Program Files\mingw-w64\x86_64-7.2.0-posix-seh-rt_v5-rev1\mingw64\bin

 

(7)关掉Git Bash终端并重新打开,检查环境变量是否添加成功:

$ which mingw32-make

(8)成功的话应该是输出这样的:

(9)为了简便起见,更改下名称:

$ alias make='mingw32-make'


二、接下来开始搭建XGBoost

(1)在xgboost路径下运行Git Bash

(2)分别输入以下命令:

每次命令输入成功后再进行下一个命令。如果不成功,用Git命令里面多重复几次。
$ cd dmlc-core
$ make -j4
$ cd ../rabit
$ make lib/librabit_empty.a -j4
$ cd ..
$ cp make/mingw64.mk config.mk
$ make -j4

...

...

直到最后命令完成后,Build成功,关闭Git Bash。

(3)接下来安装Python模块

我们用Anaconda Prompt终端执行分别如下命令:

1 cd XGBoost\xgboost\python-package
2 python setup.py install

注:我有两台电脑,两台安装都是在Anaconda Prompt终端输入,如果是用win+R系统自己的cmd输入,我发现总是失败
(更新:后来发现cmd可以了,而且在执行完前面两个命令后,在cmd中输入pip install xgboost就可以正常使用xgboost了,且直接import xgboost as xgb就行,省略了后面涉及的import os,mingw_path3,os.environ这3条命令)。

以下为具体:

 


三、 现在可以正常使用XGBoost了

(1)在jupyter notebook (在IPython中进行也可以)中依次输入:

1 import os
2 mingw_path = 'C:\\Program Files\\mingw-w64\\x86_64-7.2.0-posix-seh-rt_v5-rev1\\mingw64\\bin'
3 os.environ['PATH'] = mingw_path + ';' + os.environ['PATH']    

成功运行的例子:

 1 import xgboost as xgb
 2 import numpy as np
 3 
 4 data = np.random.rand(5,10) # 5 entities, each contains 10 features
 5 label = np.random.randint(2, size=5) # binary target
 6 dtrain = xgb.DMatrix( data, label=label)
 7 
 8 dtest = dtrain
 9 
10 param = {'bst:max_depth':2, 'bst:eta':1, 'silent':1, 'objective':'binary:logistic' }
11 param['nthread'] = 4
12 param['eval_metric'] = 'auc'
13 
14 evallist  = [(dtest,'eval'), (dtrain,'train')]
15 
16 num_round = 10
17 bst = xgb.train( param, dtrain, num_round, evallist )
18 
19 bst.dump_model('dump.raw.txt')

输出:

[0]    eval-auc:0.5    train-auc:0.5
[1]    eval-auc:0.5    train-auc:0.5
[2]    eval-auc:0.5    train-auc:0.5
[3]    eval-auc:0.5    train-auc:0.5
[4]    eval-auc:0.5    train-auc:0.5
[5]    eval-auc:0.5    train-auc:0.5
[6]    eval-auc:0.5    train-auc:0.5
[7]    eval-auc:0.5    train-auc:0.5
[8]    eval-auc:0.5    train-auc:0.5
[9]    eval-auc:0.5    train-auc:0.5

 

(2)用IPython同样可以:

IPython环境下例子

 

你看,都成功了!

 

 




posted on 2018-01-28 19:54  星辰之衍  阅读(3407)  评论(0编辑  收藏  举报

导航