Skip to content

Instantly share code, notes, and snippets.

@sharvaridhote
Created February 14, 2021 15:10
Show Gist options
  • Select an option

  • Save sharvaridhote/c4f8e1088aa4d15258f23c54eea4cb39 to your computer and use it in GitHub Desktop.

Select an option

Save sharvaridhote/c4f8e1088aa4d15258f23c54eea4cb39 to your computer and use it in GitHub Desktop.
evaluate model
def evaluate(tokenizer, textcat, texts, cats):
"""
Evaluate the performance of TextCategoriser prediction
Calculate accuracy, f1 score, precision, recall
parameters:
nlp: object - spacy
textcat: TextCategoriser
texts : input text to be evaluated
cats : input label
"""
docs = (tokenizer(text) for text in texts)
tp = 0.0 # True positives
fp = 1e-8 # False positives
fn = 1e-8 # False negatives
tn = 0.0 # True negatives
for i, doc in enumerate(textcat.pipe(docs)):
gold = cats[i]
for label, score in doc.cats.items():
if label not in gold:
continue
if label == "NEGATIVE":
continue
if score >= 0.5 and gold[label] >= 0.5:
tp += 1.0
elif score >= 0.5 > gold[label]:
fp += 1.0
elif score < 0.5 and gold[label] < 0.5:
tn += 1
elif score < 0.5 <= gold[label]:
fn += 1
# calculate metrics
accuracy = (tp + tn) / (tp + fp + fn + tn)
precision = tp / (tp + fp)
recall = tp / (tp + fn)
if (precision + recall) == 0:
f_score = 0.0
else:
f_score = 2 * (precision * recall) / (precision + recall)
return {"textcat_a": accuracy, "textcat_p": precision, "textcat_r": recall, "textcat_f": f_score}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment