【发布时间】:2017-12-03 07:40:51
【问题描述】:
我正在尝试评估使用 spacy lib 创建的经过训练的 NER 模型。 通常对于这类问题,您可以使用 f1 分数(准确率和召回率之间的比率)。我在文档中找不到训练有素的 NER 模型的准确度函数。
我不确定它是否正确,但我正在尝试通过以下方式(示例)并使用来自sklearn 的f1_score:
from sklearn.metrics import f1_score
import spacy
from spacy.gold import GoldParse
nlp = spacy.load("en") #load NER model
test_text = "my name is John" # text to test accuracy
doc_to_test = nlp(test_text) # transform the text to spacy doc format
# we create a golden doc where we know the tagged entity for the text to be tested
doc_gold_text= nlp.make_doc(test_text)
entity_offsets_of_gold_text = [(11, 15,"PERSON")]
gold = GoldParse(doc_gold_text, entities=entity_offsets_of_gold_text)
# bring the data in a format acceptable for sklearn f1 function
y_true = ["PERSON" if "PERSON" in x else 'O' for x in gold.ner]
y_predicted = [x.ent_type_ if x.ent_type_ !='' else 'O' for x in doc_to_test]
f1_score(y_true, y_predicted, average='macro')`[1]
> 1.0
任何想法或见解都是有用的。
【问题讨论】: