Last active
December 19, 2015 17:58
-
-
Save soh-i/5994766 to your computer and use it in GitHub Desktop.
recall/precision/f-measureを実装してみる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import random | |
try: | |
from benchmarking_test import BenchmarkTest | |
except: | |
raise ImportError | |
def simulation(): | |
ans = [random.randint(1,1000) for x in range(100)] | |
pred = [random.randint(1,1000) for x in range(100)] | |
bt = BenchmarkTest(pred, ans) | |
print u'Precision: %f\t' % bt.get_precision(), | |
print u'Recall: %f\t' % bt.get_recall(), | |
print u'F-measure: %f\n' % bt.get_fmeasure(), | |
def main(): | |
# 100回シミュレーションしてみる | |
count = 100 | |
for i in range(count): | |
simulation() | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import division | |
class BenchmarkTest(object): | |
def __init__(self, ans, pred): | |
if not isinstance(ans, list) or not isinstance(pred, list): | |
raise | |
self.ans = set(ans) | |
self.pred = set(pred) | |
def get_recall(self): | |
intersect = len(self.ans.intersection(self.pred)) | |
try: | |
recall = intersect / len(self.pred) | |
except: | |
raise ZeroDivisionError | |
self.recall = recall | |
return self.recall | |
def get_precision(self): | |
intersect = len(self.ans.intersection(self.pred)) | |
try: | |
precision = intersect / len(self.ans) | |
except: | |
raise ZeroDivisionError | |
self.precision = precision | |
return self.precision | |
def get_fmeasure(self): | |
try: | |
f = 2*(self.precision * self.recall) / (self.recall + self.precision) | |
except: | |
raise | |
self.f = f | |
return self.f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment