close

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

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to install xgboost in Anaconda Python (Windows platform) in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

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

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

    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!

  2. install xgboost in Anaconda Python (Windows platform)

    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!

Method 1

After trying some things the only thing that worked for me is:

conda install -c anaconda py-xgboost

Method 2

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!

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read