Recipe: How to parallalise execution of XGBoost and cross validation in Python?
MACHINE LEARNING RECIPES

How to parallalise execution of XGBoost and cross validation in Python?

This recipe helps you parallalise execution of XGBoost and cross validation in Python
In [1]:
## How to parallalise execution of XGBoost and cross validation in Python

def Snippet_190():
    print()
    print(format('How to parallalise 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("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("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("Parallel Thread XGBoost and CV: %f" % (elapsed))

Snippet_190()
******How to parallalise execution of XGBoost and cross validation in Python******
Single Thread XGBoost, Parallel Thread CV: 3.205920
Parallel Thread XGBoost, Single Thread CV: 0.322138
Parallel Thread XGBoost and CV: 0.142706


Stuck at work?
Can't find the recipe you are looking for. Let us know and we will find an expert to create the recipe for you. Click here
Companies using this Recipe