0
votes

I am trying to use XGBoost library using .train function and DMatrix but I am a little stuck because of an error :

Traceback (most recent call last):

File "", line 1, in runfile('E:/CrossValidation.py', wdir='E:/')

File "C:\Users\users\Anaconda3\envs\Lightgbm\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile execfile(filename, namespace)

File "C:\Users\users\Anaconda3\envs\Lightgbm\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "E:/CrossValidation.py", line 218, in mainXGB()

File "E:/CrossValidation.py", line 214, in mainXGB crossval_preds, val_preds = cv.train(X_data=X_train.values, y_data=y_train.values, X_test=X_val.values, params=xgb_params)

File "E:/CrossValidation.py", line 136, in train early_stopping_rounds=early_stopping_rounds)

File "C:\Users\users\Anaconda3\envs\Lightgbm\lib\site-packages\xgboost\training.py", line 204, in train xgb_model=xgb_model, callbacks=callbacks)

File "C:\Users\users\Anaconda3\envs\Lightgbm\lib\site-packages\xgboost\training.py", line 32, in _train_internal bst = Booster(params, [dtrain] + [d[0] for d in evals])

File "C:\Users\users\Anaconda3\envs\Lightgbm\lib\site-packages\xgboost\training.py", line 32, in bst = Booster(params, [dtrain] + [d[0] for d in evals])

TypeError: 'DMatrix' object does not support indexing

Here my piece of code :

dtrain =  xgb.DMatrix(X_data[train_idx], label=np.log1p(y_data[train_idx])) # datas.slice(train_idx)
dtest = xgb.DMatrix(X_data[val_idx], label=np.log1p(y_data[val_idx]))
print('data created for xgboost')
model = self.model_base.train(params=params, dtrain=dtrain,    num_boost_round=number_iteration, evals=[dtest],   early_stopping_rounds=early_stopping_rounds)

Does anyone know how to solve the problem ?

1

1 Answers

2
votes

The problem is with the evals argument. A list of tuples is expected, so change evals=[dtest] to evals=[(dtest, "Test")].