Skip to content

Instantly share code, notes, and snippets.

@mvervuurt
Created July 25, 2019 12:19
Show Gist options
  • Save mvervuurt/13ac9a7cd6af03a4a10ad655f5294bdb to your computer and use it in GitHub Desktop.
Save mvervuurt/13ac9a7cd6af03a4a10ad655f5294bdb to your computer and use it in GitHub Desktop.
Custom Evaluation Metrics XGBoost: Precision and f1_score
def xgb_precision(proba_y: np.ndarray, dataset: xgb.DMatrix) -> Tuple[str, float]:
'''returns binary classification precision using 0.5 threshold.
proba_y: 1x2 shape or binary classification probabilities
dataset: xgboost DMatrix
'''
y = dataset.get_label()
tresh_func = np.vectorize(lambda x: 1 if x> 0.5 else 0)
pred_y = tresh_func(proba_y)
return 'clf_precision', precision_score(y, pred_y)
def xgb_f1(proba_y: np.ndarray, dataset: xgb.DMatrix) -> Tuple[str, float]:
'''returns classification f1_score using 0.5 threshold.
proba_y: 1x2 shape or binary classification probabilities
dataset: xgboost DMatrix
'''
y = dataset.get_label()
tresh_func = np.vectorize(lambda x: 1 if x> 0.5 else 0)
pred_y = tresh_func(proba_y)
return 'clf_f1', f1_score(y, pred_y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment