The XGBoost library has its own implementation of cross validation through xgboost.cv(). It looks like it requires data be stored as a DMatrix.

Instead of using xgboost.cv(), I could use XGBoost's sklearn API to perform cross validation using sklearn with GridSearchCV() or RandomizedGridCV, or cross_validate(). If I use the sklearn implementations of cross validation, I could use pandas.DataFrame's and familar sklearn functions/classes.

I have two questions:

  1. Are there any important differences between performing cross validation using XGBoost directly and using sklearn? Is xgboost.cv() optimized in some way that the sklearn cross validation functions are not? I understand there are some superficial difference in how to train the models.
  2. Is there any benefit to using DMatrix over pandas.DataFrames? It looks like in older versions of XGBoost, you couldn't use pandas.DataFrames.

1 Answer 1


There are a few differences between using the XGBoost library's own implementation of cross-validation (i.e., xgboost.cv()) and using the cross-validation functions provided by scikit-learn (i.e., GridSearchCV, RandomizedSearchCV, cross_validate).

One difference is that xgboost.cv() only works with XGBoost models, whereas the scikit-learn functions can be used with a variety of model types. This means that if you want to use XGBoost with scikit-learn's cross-validation functions, you will need to use XGBoost's scikit-learn API, which wraps the XGBoost library's functionality in a way that is compatible with scikit-learn.

Another difference is that xgboost.cv() is optimized specifically for XGBoost models, whereas the scikit-learn functions are more general and can be used with any estimator. This means that xgboost.cv() may be faster or more efficient when working with XGBoost models, but it may not be as flexible as the scikit-learn functions.

As for the use of DMatrix versus pandas.DataFrame, DMatrix is a data structure provided by the XGBoost library that is optimized for use with XGBoost models. It can be more efficient to use DMatrix when working with large datasets, since it stores the data in a more compact format and can take advantage of XGBoost's internal memory management. However, it is also possible to use pandas.DataFrame with XGBoost models by converting the DataFrame to a DMatrix using the xgboost.DMatrix() function.

In short, if you need the flexibility and compatibility of scikit-learn's functions, or if you are using a model other than XGBoost, you may want to use the scikit-learn functions. On the other hand, if you are only using XGBoost models and want the efficiency and optimization provided by the XGBoost library, you may prefer to use xgboost.cv().


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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