在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環境下例子

 

你看,都成功了!

 

 





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM