How to parallelise execution of XGBoost and cross validation in Python

In [1]:
def Snippet_190(): 
    print()
    print(format('How to parallelise execution of XGBoost and cross validation in Python','*^82'))    
    
    import warnings
    warnings.filterwarnings("ignore")
    
    # load libraries
    import time
    from sklearn import datasets
    from sklearn.model_selection import train_test_split, cross_val_score
    from xgboost import XGBClassifier
    
    # load the iris datasets
    dataset = datasets.load_wine()
    X = dataset.data; y = dataset.target
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)

    
    # Single Thread XGBoost, Parallel Thread CV
    start = time.time()
    model = XGBClassifier(nthread=1)
    results = cross_val_score(model, X, y, cv=10, scoring='neg_log_loss', n_jobs=-1)
    elapsed = time.time() - start
    print(); print("Single Thread XGBoost, Parallel Thread CV: %f" % (elapsed))
    
    
    # Parallel Thread XGBoost, Single Thread CV
    start = time.time()
    model = XGBClassifier(nthread=-1)
    results = cross_val_score(model, X, y, cv=10, scoring='neg_log_loss', n_jobs=1)
    elapsed = time.time() - start
    print(); print("Parallel Thread XGBoost, Single Thread CV: %f" % (elapsed))
    
    
    # Parallel Thread XGBoost and CV
    start = time.time()
    model = XGBClassifier(nthread=-1)
    results = cross_val_score(model, X, y, cv=10, scoring='neg_log_loss', n_jobs=-1)
    elapsed = time.time() - start
    print(); print("Parallel Thread XGBoost and CV: %f" % (elapsed))
Snippet_190()
******How to parallelise execution of XGBoost and cross validation in Python******

Single Thread XGBoost, Parallel Thread CV: 4.230636

Parallel Thread XGBoost, Single Thread CV: 0.321292

Parallel Thread XGBoost and CV: 0.138592
In [ ]: