Reputation: 23
How to test accuracy of a spacy pretrained model in version 3.0.1. I want to see my output how accurate my tested model is predicted.This the code below for spacy version 2 but it doesn't work in spacy version 3.can somone tell me the code on spacy version 3.
from spacy.gold import GoldParse
from spacy.scorer import Scorer
def evaluate(nlp, examples, ent='PERSON'):
scorer = Scorer()
for input_, annot in examples:
text_entities = []
for entity in annot.get('entities'):
if ent in entity:
text_entities.append(entity)
doc_gold_text = nlp.make_doc(input_)
gold = GoldParse(doc_gold_text, entities=text_entities)
pred_value = nlp(input_)
scorer.score(pred_value, gold)
return scorer.scores
examples = [
("Trump says he's answered Mueller's Russia inquiry questions \u2013 live",{"entities":[[0,5,"PERSON"],[25,32,"PERSON"],[35,41,"GPE"]]}),
("Alexander Zverev reaches ATP Finals semis then reminds Lendl who is boss",{"entities":[[0,16,"PERSON"],[55,60,"PERSON"]]}),
("Britain's worst landlord to take nine years to pay off string of fines",{"entities":[[0,7,"GPE"]]}),
("Tom Watson: people's vote more likely given weakness of May's position",{"entities":[[0,10,"PERSON"],[56,59,"PERSON"]]}),
]
nlp = spacy.load('en_core_web_sm')
results = evaluate(nlp, examples)
print(results)
Upvotes: 1
Views: 1238
Reputation: 96
Personnally i had used this method, and i wish it will help you in your work: In your case, I think:
from spacy.training import Example
#get test data
test_data = [
("Trump says he's answered Mueller's Russia inquiry questions \u2013
live",{"entities":[[0,5,"PERSON"],[25,32,"PERSON"],[35,41,"GPE"]]}),
("Alexander Zverev reaches ATP Finals semis then reminds Lendl who is
boss",{"entities":[[0,16,"PERSON"],[55,60,"PERSON"]]}),
("Britain's worst landlord to take nine years to pay off string of fines",
{"entities":[[0,7,"GPE"]]}),
("Tom Watson: people's vote more likely given weakness of May's position",
{"entities":[[0,10,"PERSON"],[56,59,"PERSON"]]}),
]
#formatted test data in order to adapt with the new version 3 of Spacy
#get nlp object
nlp = spacy.load('en_core_web_sm')
new_test_data = []
for text, annots in test_data:
new_test_data.append(Example.from_dict(nlp.make_doc(text), annots))
#end formatted test data
#begin evaluation
#using , the evaluate() methos
scores_model = nlp.evaluate(new_test_data)
#print scores that you want
#precision_model = scores_model["ents_p"]
#recall_model = scores_model["ents_r"]
#f_score_model = scores_model["ents_f"]
#scores_entities = scores_model["ents_per_type"]
Upvotes: 2