Created
November 2, 2012 23:56
-
-
Save vene/4005112 to your computer and use it in GitHub Desktop.
Grouped score in scikit-learn proof of concept
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from sklearn.metrics import zero_one_score | |
# Input data corresponds to 4 words: | |
# - descalcarea (des-cal-ca-rea, predicted: de-s-cal-ca-rea) | |
# - somnolezi (som-no-lezi, predicted: som-no-lezi) | |
# - salandere (sa-lan-de-re, predicted: sa-lan-de-re) | |
y_pred = np.array( | |
[False, True, True, False, False, True, False, True, False, | |
False, False, False, True, False, True, False, False, False, | |
False, True, False, False, True, False, True, False, False, | |
True, False, True, False, True, False, True, False, True, | |
False, False], dtype=bool) | |
y_true = np.array( | |
[False, False, True, False, False, True, False, True, False, | |
False, False, False, True, False, True, False, False, False, | |
False, True, False, False, True, False, True, False, False, | |
True, False, True, False, True, False, True, False, True, | |
False, False], dtype=bool) | |
# maps samples to the words they belong to | |
groups = np.array( | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, | |
2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]) | |
def all_or_nothing_score(y_true, y_pred, groups=None): | |
y_true, y_pred = (np.asarray(y) for y in (y_true, y_pred)) | |
if groups is None: | |
groups = np.ones_like(y_true) | |
else: | |
groups = np.asarray(groups) | |
hits = [np.all(y_true[np.where(groups == this_group)] == | |
y_pred[np.where(groups == this_group)]) | |
for this_group in np.unique(groups)] | |
return np.mean(hits) | |
# what proportion of candidate hyphens were predicted correctly? | |
print zero_one_score(y_true, y_pred) # 0.973684210526 | |
# what proportion of words did we get completely right? | |
print all_or_nothing_score(y_true, y_pred, groups) # 0.75 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment