9
$\begingroup$

I am trying to run xgboost in scikit learn. And I only use Pandas to load data into dataframe. How am i supposed to use pandas df with xgboost. I am confused by the DMatrix routine required to run xgboost algo.

$\endgroup$
16
$\begingroup$

You can use the dataframe's .values method to access raw data once you have manipulated the columns as you need them.

E.g.

train = pd.read_csv("train.csv")
target = train['target']
train = train.drop(['ID','target'],axis=1)
test = pd.read_csv("test.csv")
test = test.drop(['ID'],axis=1)

xgtrain = xgb.DMatrix(train.values, target.values)
xgtest = xgb.DMatrix(test.values)

Obviously you may need to change which columns you drop or use as the training target. The above was for a Kaggle competition, so there was no target data for xgtest (it is held back by the organisers).

$\endgroup$
  • $\begingroup$ When trying this way xgb.DMatrix(X_train.values, y_train.values) I am seeing TypeError: can not initialize DMatrix from dict $\endgroup$ – javadba Sep 30 '18 at 21:15
  • $\begingroup$ @javadba: It definitely worked in 2016 on my mcahine! I cannot test this at the moment as I cannot install xgboost. It is possible some library code has changed. More likely there is something different about your situation. I found stackoverflow.com/questions/35402461/… but that simply advises you to do exactly what this answer does (i.e. use .values) $\endgroup$ – Neil Slater Oct 1 '18 at 6:58
7
$\begingroup$

There is some good news there is a library pandas_ml which supports XGBoost probably this will streamline workflow simply.

http://pandas-ml.readthedocs.io/en/latest/xgboost.html

$\endgroup$
6
$\begingroup$

You can now use Pandas DataFrames directly with XGBoost. Definitely works with xgboost 0.81.

For example where X_train, X_val, y_train, and y_val are DataFrames:

import xgboost as xgb

mod = xgb.XGBRegressor(
    gamma=1,                 
    learning_rate=0.01,
    max_depth=3,
    n_estimators=10000,                                                                    
    subsample=0.8,
    random_state=34
) 

mod.fit(X_train, y_train)
predictions = mod.predict(X_val)
rmse = sqrt(mean_squared_error(y_val, predictions))
print("score: {0:,.0f}".format(rmse))

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.