0
votes

When I try to run cross-valdation using the code:

cv_results = xgb.cv(params=params,dtrain=dmatrix_train, num_boost_round=10, nfold=1)

I get the following error:

TypeError                                 Traceback (most recent call last)
<ipython-input-101-65647e385c18> in <module>()
----> 1 cv_results = xgb.cv(params=params,dtrain=dmatrix_train, num_boost_round=10, nfold=1)

Can anyone point to me what I am doing wrong?

C:\ProgramData\Anaconda35\lib\site-packages\xgboost-0.40-py3.6.egg\xgboost.py in cv(params, dtrain, num_boost_round, nfold, metrics, obj, feval, fpreproc, show_stdv, seed)
    798     """
    799     results = []
--> 800     cvfolds = mknfold(dtrain, nfold, params, seed, metrics, fpreproc)
    801     for i in range(num_boost_round):
    802         for f in cvfolds:

C:\ProgramData\Anaconda35\lib\site-packages\xgboost-0.40-py3.6.egg\xgboost.py in mknfold(dall, nfold, param, seed, evals, fpreproc)
    722     randidx = np.random.permutation(dall.num_row())
    723     kstep = len(randidx) / nfold
--> 724     idset = [randidx[(i * kstep): min(len(randidx), (i + 1) * kstep)] for i in range(nfold)]
    725     ret = []
    726     for k in range(nfold):

C:\ProgramData\Anaconda35\lib\site-packages\xgboost-0.40-py3.6.egg\xgboost.py in <listcomp>(.0)
    722     randidx = np.random.permutation(dall.num_row())
    723     kstep = len(randidx) / nfold
--> 724     idset = [randidx[(i * kstep): min(len(randidx), (i + 1) * kstep)] for i in range(nfold)]
    725     ret = []
    726     for k in range(nfold):

TypeError: slice indices must be integers or None or have an __index__ method
1

1 Answers

3
votes

You are passing the parameter value n_fold=1 This doesn't make sense. Cross validation is all about partitioning the data in several partitions and validating the model holding out one partition among them. so 1 is an invalid value, try n_fold=3 or higher. Then your error should be gone.

Read more about cross validation here. http://scikit-learn.org/stable/modules/cross_validation.html