我试图在我的函数中定义tp,tn,fp,fn。我怎样才能在我的函数中定义它们?
来自集合的导入了namedtuple 来自Corpus import read_classification_from_file import os
def compute_confusion_matrix(truth_dict, pred_dict,pos_tag=True,neg_tag=False):
tp=0
fn=0
tn=0
fp=0
for i in truth_dict.keys():
if (truth_dict[i] == pos_tag):
if(pred_dict[i] == pos_tag):
tp += 1
else:
fn += 1
else:
if(pred_dict[i] != pos_tag):
tn += 1
else:
fp += 1
ConfMat = namedtuple('ConfMat', 'tp tn fp fn')
p=ConfMat(tp, tn, fp, fn)
return p
def quality_score(tp,tn,fp,fn):
return (tp + tn)/(tp + tn + 10 * fp + fn)
def compute_quality_for_corpus(corpus_dir):
truth_dict = read_classification_from_file(os.path.join(corpus_dir, "!truth.txt"))
pred_dict = read_classification_from_file(os.path.join(corpus_dir, "!prediction.txt"))
cm2 = compute_confusion_matrix(truth_dict, pred_dict,pos_tag='SPAM',neg_tag='OK')
cm2=(cm.tp,cm.tn,cm.fp,cm.fn)
return quality_score(tp,tn,fp,fn)
答案 0 :(得分:0)
你也有一些带有cm2线(cm的外观)的拼写错误,但这很容易解决。当您访问命名元组中的字段时,您可以使用点表示法。
cm2 = compute_confusion_matrix(truth_dict,pred_dict,pos_tag='SPAM',neg_tag='OK')
return quality_score(cm2.tp,cm2.tn,cm2.fp,cm2.fn)