precision,recall 计算代码

之前写precision,recall的代码都是用了两层循环,今天看到一个比较优美的写法,用python 的set,以及交集intersection,所以Mark一下。这个文档将会Mark所有觉得不错的计算评测标准的代码

precision Recall

def cal_precision_recall(targets,predictions,k):

# targets是真实值,predictions是预测值
	pred = predictions[:k]
	num_hit = len(set(predictions).intersection(set(targets)))
	precision = float(num_hit/predictions)
	recall = float(num_hit/targets)
	return precison , recall 
	


你可能感兴趣的:(自然语言处理)