Note
Go to the endto download the full example code.
Demo for using xgboost with sklearn
importmultiprocessingfromurllib.errorimportHTTPErrorfromsklearn.datasetsimportfetch_california_housing,make_regressionfromsklearn.model_selectionimportGridSearchCVimportxgboostasxgbif__name__=="__main__":print("Parallel Parameter optimization")try:X,y=fetch_california_housing(return_X_y=True)exceptHTTPError:# Use a synthetic dataset instead if we couldn'tX,y=make_regression(n_samples=20640,n_features=8,random_state=1234)# Make sure the number of threads is balanced.xgb_model=xgb.XGBRegressor(n_jobs=multiprocessing.cpu_count()//2,tree_method="hist")clf=GridSearchCV(xgb_model,{"max_depth":[2,4,6],"n_estimators":[50,100,200]},verbose=1,n_jobs=2,)clf.fit(X,y)print(clf.best_score_)print(clf.best_params_)