TypeError: ('invalid cache item: tuple') in XGBoost

1.1k Views Asked by At

I am trying to get classify value using XGBoost model.
However I got an Error when I declared XGBoost model.
How can I fix this error ?

train = pd.read_csv("./data/train.csv")
test_x = pd.read_csv("./data/test.csv")
train_y = train['Y']
train_x = train.drop('Y', axis=1)

kf = KFold(n_splits=4, shuffle=True, random_state=71)
tr_idx, va_idx = list(kf.split(train_x))[0]
tr_x, va_x = train_x.iloc[tr_idx], train_x.iloc[va_idx]
tr_y, va_y = train_y.iloc[tr_idx], train_y.iloc[va_idx]

dtrain = xgb.DMatrix(tr_x, label=tr_y)
dvalid = xgb.DMatrix(va_x, label=va_y), 
dtest = xgb.DMatrix(test_x)

print('-' * 10)

params = {'objective' : 'binary:logistic', 
            'eval_metric' : 'logloss',
            'verbosity': 0, 'random_state' : 71}
num_round = 10000

watch_list = [(dtrain, 'train'),(dvalid, 'eval')]
model = xgb.train(params, dtrain, num_round, watch_list) ← error occured here

and error message is here.

TypeError: ('invalid cache item: tuple', [<xgboost.core.DMatrix object at 0x000001B9D6930190>, <xgboost.core.DMatrix object at 0x000001B9D6930190>, (<xgboost.core.DMatrix object at 0x000001B9DBBCA880>,)])
1

There are 1 best solutions below

0
On

The dvalid which you are creating is a tuple of size 1 due to the comma you have put in the end. If you do a = 1, this creates a tuple of size one. If you remove the comma, you get rid of that error

dvalid = xgb.DMatrix(va_x, label=va_y), # Remove the comma here.
``