Spacy 3.0.1 精度预测
Spacy 3.0.1 Accuracy prediction
如何在 3.0.1 版本中测试 spacy 预训练模型的准确性。我想看看我的输出我的测试模型有多准确 predicted.This 下面的 spacy 版本 2 的代码,但它在 spacy 版本中不起作用 3.can 请告诉我 spacy 版本 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)
这个方法我个人用过,希望对你的工作有所帮助:
对于你的情况,我认为:
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"]
如何在 3.0.1 版本中测试 spacy 预训练模型的准确性。我想看看我的输出我的测试模型有多准确 predicted.This 下面的 spacy 版本 2 的代码,但它在 spacy 版本中不起作用 3.can 请告诉我 spacy 版本 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)
这个方法我个人用过,希望对你的工作有所帮助: 对于你的情况,我认为:
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"]