Example 1: gridsearchcv
x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=50)
xgb=XGBClassifier()
----------------------------------------------------------------------
from sklearn.model_selection import GridSearchCV
parameters=[{'learning_rate':[0.1,0.2,0.3,0.4],'max_depth':[3,4,5,6,7,8],'colsample_bytree':[0.5,0.6,0.7,0.8,0.9]}]
gscv=GridSearchCV(xgb,parameters,scoring='accuracy',n_jobs=-1,cv=10)
grid_search=gscv.fit(x,y)
grid_search.best_params_
-----------------------------------------------------------------------
x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=50)
xgb=XGBClassifier(colsample_bytree=0.8, learning_rate=0.4, max_depth=4)
xgb.fit(x,y)
pred=xgb.predict(x_test)
print('Accuracy= ',accuracy_score(y_test,pred))
-----------------------------------------------------------------------
from sklearn.model_selection import cross_val_predict
xgb=XGBClassifier(colsample_bytree=0.8, learning_rate=0.4, max_depth=4)
cvs=cross_val_score(xgb,x,y,scoring='accuracy',cv=10)
print('cross_val_scores= ',cvs.mean())
y_pred=cross_val_predict(xgb,x,y,cv=10)
conf_mat=confusion_matrix(y_pred,y)
conf_mat
---------------------------------------------------------------------------
gbm=GradientBoostingRegressor(max_depth=7,min_samples_leaf=1,n_estimators=100)
cvs=cross_val_score(xgb,x,y,scoring='r2',cv=5)
print('cross_val_scores= ',cvs.mean())
-------------------------------------------------------------------------------
parameters=[{'learning_rate':[0.1,0.2,0.3,0.4],'max_depth':[3,4,5,6,7,8],'colsample_bytree':[0.5,0.6,0.7,0.8,0.9]}]
parameters=[{'max_depth':[5,7,9,10],'min_samples_leaf':[1,2],'n_estimators':[100,250,500]}]
parameters=[{'max_depth':[5,7,9,10],'min_samples_leaf':[1,2],'n_estimators':[100,250,500]}]
parameters={'n_neighbors':[5,6,8,10,12,14,15]}
parameters={'penalty':['l1','l2'],'C':[1,2,3,4,5]}
parameters={'var_smoothing': np.logspace(0,-9, num=100)}
parameters=[{'C':[0.1,0.5,1,2,3],'kernel':['rbf','poly']}]
parameters=[{'base_estimator':[lr],'learning_rate':[1,0.1,0.001],'n_estimators':[100,150,250]}]
parameters=[{'criterion':['gini','entropy'],'max_depth':[5,7,9,10],'min_samples_leaf':[1,2]}]
Example 2: Exhaustive search over specified parameter values for an estimator
from sklearn import svm, datasets
from sklearn.model_selection import GridSearchCV
iris = datasets.load_iris()
parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}
svc = svm.SVC()
clf = GridSearchCV(svc, parameters)
clf.fit(iris.data, iris.target)
sorted(clf.cv_results_.keys())