Skip to content

Instantly share code, notes, and snippets.

@pmbaumgartner
Last active August 14, 2018 17:12
Show Gist options
  • Save pmbaumgartner/383040487c409a819ecd to your computer and use it in GitHub Desktop.
Save pmbaumgartner/383040487c409a819ecd to your computer and use it in GitHub Desktop.
Making a custom scorer in sklearn that only looks at certain labels when calculating model metrics. For example: This creates a f1_macro scorer object that only looks at the '-1' and '1' labels of a target variable.
from sklearn.metrics import f1_score, make_scorer
scorer = make_scorer(f1_score, labels=[-1, 1], average='macro')
# use like:
cv = cross_val_score(model, X, Y, scoring=scorer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment