Complete Machine Learning Guide to Parameter Tuning in Gradient Boosting in Python

Please am referring to your topic on parameter tunning using GBM. Below is your code which I edited to include my dataframe code:


def modelfit(alg, ml_Telco, predictors, performCV=True, printFeatureImportance=True, cv_folds=5):
#Fit the algorithm on the data[predictors], ml_Telco[‘Churn’])

#Predict training set:
ml_Telco_predictions = alg.predict(ml_Telco[predictors])
ml_Telco_predprob = alg.predict_proba(dtrain[predictors])[:,1]

#Perform cross-validation:
if performCV:
    cv_score = cross_validation.cross_val_score(alg, ml_Telco[predictors], ml_Telco['Churn'], cv=cv_folds, scoring='roc_auc')

#Print model report:
print ("\nModel Report")
print ("Accuracy : %.4g" % metrics.accuracy_score(ml_Telco['Churn'].values, ml_Telco_predictions))
print( "AUC Score (Train): %f" % metrics.roc_auc_score(ml_Telco['Churn'], ml_Telco_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)
    feat_imp.plot(kind='bar', title='Feature Importances')
    plt.ylabel('Feature Importance Score')

predictors = [x for x in x_train.columns if x not in [‘Churn’]]
gbm0 = GradientBoostingClassifier(random_state=10)
df=pd.concat([x_train, y_train], axis=1)
modelfit(gbm0, x_train, predictors)

But I got the following error message:

2 gbm0 = GradientBoostingClassifier(random_state=10)
3 df=pd.concat([x_train, y_train], axis=1)
----> 4 modelfit(gbm0, x_train, predictors)

in modelfit(alg, ml_Telco, predictors, performCV, printFeatureImportance, cv_folds)
2 def modelfit(alg, ml_Telco, predictors, performCV=True, printFeatureImportance=True, cv_folds=5):
3 #Fit the algorithm on the data
----> 4[predictors], ml_Telco[‘Churn’])
6 #Predict training set:

2657 return self._engine.get_loc(key)
2658 except KeyError:
-> 2659 return self._engine.get_loc(self._maybe_cast_indexer(key))
2660 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
2661 if indexer.ndim > 1 or indexer.size > 1:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: ‘Churn’

Please, how do I fix this?

© Copyright 2013-2019 Analytics Vidhya