Skip to content

Instantly share code, notes, and snippets.

@jaradc
Last active October 6, 2017 17:52
Show Gist options
  • Save jaradc/9ae050e6ca04102388538f7bfee237d0 to your computer and use it in GitHub Desktop.
Save jaradc/9ae050e6ca04102388538f7bfee237d0 to your computer and use it in GitHub Desktop.
Create a custom scorer in sklearn
def specificity_scorer(y_true, y_pred):
tn, fp, fn, tp = metrics.confusion_matrix(y_true, y_pred).ravel()
return tn / (tn+fp)
specificity = metrics.make_scorer(specificity_scorer, greater_is_better=True)
# use in cross_val_score
cv = cross_val_score(xgb, dt.features_.values, y, scoring=specificity, cv=5, verbose=2)
# use in GridSearchCV
grid = GridSearchCV(clf, {}, scoring=specificity)
grid.fit(X_train, y_train)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment