<jupyter_start><jupyter_text>gridcv<jupyter_code>from sklearn.ensemble import GradientBoostingClassifier  #GBM algorithm
from sklearn import cross_validation, metrics   #Additional scklearn functions
from sklearn.grid_search import GridSearchCV   #Perforing grid search
def modelfit(alg, dtrain, predictors, performCV=True, printFeatureImportance=True, cv_folds=5):
    #Fit the algorithm on the data
    alg.fit(dtrain[predictors], y)
        
    #Predict training set:
    dtrain_predictions = alg.predict(dtrain[predictors])
    dtrain_predprob = alg.predict_proba(dtrain[predictors])[:,1]
    
    #Perform cross-validation:
    if performCV:
        cv_score = cross_validation.cross_val_score(alg, dtrain[predictors], y, cv=cv_folds, scoring='roc_auc')
    
    #Print model report:
    print ("\nModel Report")
    print ("Accuracy : %.4g" % metrics.accuracy_score(y , dtrain_predictions))
    print ("AUC Score (Train): %f" % metrics.roc_auc_score(y , dtrain_predprob))
    
    if performCV:
        print ("CV Score : Mean - %.7g | Std - %.7g | Min - %.7g | Max - %.7g" % (np.mean(cv_score),np.std(cv_score),np.min(cv_score),np.max(cv_score)))
        
    #Print Feature Importance:
    if printFeatureImportance:
        feat_imp = pd.Series(alg.feature_importances_, predictors).sort_values(ascending=False)
        plt.figure(figsize=(20,20))
        feat_imp.plot(kind='bar', title='Feature Importances')
        plt.ylabel('Feature Importance Score')
#Choose all predictors except target & IDcols
predictors = df.columns
gbm0 = GradientBoostingClassifier(random_state=10)
modelfit(gbm0, df, predictors)
param_test1 = {'n_estimators':[20, 30, 40, 50, 60, 70, 80, 90]}
gsearch1 = GridSearchCV(estimator = GradientBoostingClassifier(learning_rate=0.05, min_samples_split=500,
                        min_samples_leaf=50,max_depth=8,max_features='sqrt',subsample=0.8,random_state=10), 
                        param_grid = param_test1, scoring='roc_auc',n_jobs=4,iid=False, cv=5)
gsearch1.fit(df[predictors], y)
gsearch1.grid_scores_, gsearch1.best_params_, gsearch1.best_score_
## Test 2
param_test2 = {'max_depth':[5, 7, 9, 11, 13, 15] ,'min_samples_split': [200, 400, 600, 800, 1000]}
gsearch2 = GridSearchCV(estimator = GradientBoostingClassifier(learning_rate=0.05, n_estimators=90, max_features='sqrt', subsample=0.8, random_state=10), 
param_grid = param_test2, scoring='roc_auc',n_jobs=4,iid=False, cv=5)
gsearch2.fit(df[predictors], y)
gsearch2.grid_scores_, gsearch2.best_params_, gsearch2.best_score_
#test 3
param_test3 = {'min_samples_split': [800, 1000, 1200, 1400, 1600] , 'min_samples_leaf': [30, 40, 50, 60, 70]}
gsearch3 = GridSearchCV(estimator = GradientBoostingClassifier(learning_rate=0.05, n_estimators=90,\
                                    max_depth=7,max_features='sqrt', subsample=0.8, random_state=10), 
param_grid = param_test3, scoring='roc_auc',n_jobs=4,iid=False, cv=5)
gsearch3.fit(df[predictors], y)
gsearch3.grid_scores_, gsearch3.best_params_, gsearch3.best_score_
modelfit(gsearch3.best_estimator_, df, predictors)
#test 4
param_test4 = {'max_features': [7, 9, 11, 13, 15, 17, 19, 21]}
gsearch4 = GridSearchCV(estimator = GradientBoostingClassifier(learning_rate=0.05, min_samples_split = 1000, n_estimators=70,max_depth=7,\
                                                               max_features='sqrt', subsample=0.8, random_state=10,min_samples_leaf = 50), 
param_grid = param_test4, scoring='roc_auc',n_jobs=4,iid=False, cv=5)
gsearch4.fit(df[predictors], y )
gsearch4.grid_scores_, gsearch4.best_params_, gsearch4.best_score_
#test 5
param_test5 = {'subsample':[0.6,0.7,0.75,0.8,0.85,0.9]}
gsearch5 = GridSearchCV(estimator = GradientBoostingClassifier(learning_rate=0.05, min_samples_split = 1000, n_estimators=70,max_depth=7,\
                                                            subsample=0.8, \
                                                               random_state=10,min_samples_leaf = 50,max_features=17), 
param_grid = param_test5, scoring='roc_auc',n_jobs=4,iid=False, cv=5)
gsearch5.fit(df[predictors], y )
gsearch5.grid_scores_, gsearch5.best_params_, gsearch5.best_score_
gbm_tuned_2 = GradientBoostingClassifier(learning_rate=0.05, min_samples_split = 1000, n_estimators=500,max_depth=10,\
                                        subsample=0.8, random_state=10,min_samples_leaf = 50,max_features=17)
modelfit(gbm_tuned_2, df, predictors)
test.info()
prediction_proba_2 = gbm_tuned_2.predict_proba(test)
submit = make_submission(prediction_proba_2[:,1])<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup