I am fairly new to Python. I implemented a short cross-validation tool for gradient boosting methods.
import numpy as np
from sklearn.metrics import roc_auc_score as auc
from sklearn import cross_validation
from time import time
def heldout_auc(model, X_test, y_test):
score = np.zeros((model.get_params()["n_estimators"],), dtype=np.float64)
for i, y_pred in enumerate(model.staged_decision_function(X_test)):
score[i] = auc(y_test, y_pred)
return score
def cv_boost_estimate(X,y,model,n_folds=3):
cv = cross_validation.StratifiedKFold(y, n_folds=n_folds, shuffle=True, random_state=11)
val_scores = np.zeros((model.get_params()["n_estimators"],), dtype=np.float64)
t = time()
i = 0
for train, test in cv:
i = i + 1
print('FOLD : ' + str(i) + '-' + str(n_folds))
model.fit(X.iloc[train,], y.iloc[train])
val_scores += heldout_auc(model, X.iloc[test,], y.iloc[test])
val_scores /= n_folds
return val_scores,(time()-t)
Then I can look for the optimal number of trees with the following:
print('AUC : ' + str(max(auc)) + ' - index : ' + str(auc.tolist().index(max(auc))))
Everything is working, but the syntax feels uneasy and "not Pythonic". Does someone have improvements to suggest?
numpy
that encourages non-looping methods. Whether the 1st loop can be eleminated depends on whatauc
can handle. The 2nd looks iterative by nature. \$\endgroup\$