How to install xgboost in Anaconda Python (Windows platform)?

前端 未结 21 2618
北恋
北恋 2020-11-30 19:25

I am a new Python user. I downloaded the latest Anaconda 3 2.4.1 (Python 3.5) from the below link: https://www.continuum.io/downloads

My PC Configurations are: Windo

21条回答
  •  广开言路
    2020-11-30 19:54

    I was able to install xgboost for Python in Windows yesterday by following this link. But when I tried to import using Anaconda, it failed. I recognized this is due to the fact that Anaconda has a different Python distribution. I then searched again and found this great article which made it!

    The trick is after installing successfully for regular Python, to have it work for Anaconda, you just need to pull up the Anaconda prompt and cd into this folder "code\xgboost\python-package", then run:

    python setup.py install
    

    And voila! The article says you need to add the path, but for me it worked directly. Good luck!

    Also copied below the original contents in case the link is not available...

    Once the last command completes the build is done. We can now install the Python module. What follows depends on the Python distribution you are using. For Anaconda, I will simply use the Anaconda prompt, and type the following in it (after the prompt, in my case [Anaconda3] C:\Users\IBM_ADMIN>):

    [Anaconda3] C:\Users\IBM_ADMIN>cd code\xgboost\python-package
    The point is to move to the python-package directory of XGBoost.  Then type:
    [Anaconda3] C:\Users\IBM_ADMIN\code\xgboost\python-package>python setup.py install
    

    We are almost done. Let's launch a notebook to test XGBoost. Importing it directly causes an error. In order to avoid it we must add the path to the g++ runtime libraries to the os environment path variable with:

    import os
    
    mingw_path = 'C:\\Program Files\\mingw-w64\\x86_64-5.3.0-posix-seh-rt_v4-rev0\\mingw64\\bin'
    
    os.environ['PATH'] = mingw_path + ';' + os.environ['PATH']
    

    We can then import xgboost and run a small example.

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

    We are all set!

提交回复
热议问题