def test_measures_with_empty_rel_and_ret(self): sheet1 = EvaluationSheet(Scoresheet(), [], []) sheet2 = EvaluationSheet(Scoresheet(), [], 10) sheet3 = EvaluationSheet(Scoresheet(), []) for sheet in (sheet1, sheet2, sheet3): assert_raises(UndefinedError, sheet.precision) assert_raises(UndefinedError, sheet.recall) assert_raises(UndefinedError, sheet.f_score) assert_raises(UndefinedError, sheet.fallout) assert_raises(UndefinedError, sheet.miss) assert_raises(UndefinedError, sheet.accuracy) assert_raises(UndefinedError, sheet.generality)
def test_weighted(self): known = {(0, 1): 1, (0, 2): 3, (1, 2): 1, (1, 3): 2, (2, 4): 1, (0, 3): 2 / 3, (0, 4): 0.75, (1, 4): 0.5, (2, 3): 2 / 3, (3, 4): 0.4} known = Scoresheet(known) graph_distance = GraphDistance(self.G).predict() assert_dict_almost_equal(graph_distance, known)
def test_measures_with_empty_rel_and_ret(self): sheet1 = EvaluationSheet(Scoresheet(), [], []) sheet2 = EvaluationSheet(Scoresheet(), [], 10) sheet3 = EvaluationSheet(Scoresheet(), []) for sheet in (sheet1, sheet2, sheet3): for method in [ "precision", "recall", "f_score", "fallout", "miss", "accuracy", "generality", ]: with pytest.raises(UndefinedError): getattr(sheet, method)()
def test_cosine(self): known = { (1, 5): 10 / sqrt(104), (2, 3): 7 / sqrt(150), (1, 4): 7 / sqrt(130), (4, 5): 2 / sqrt(20), } found = nbr.Cosine(self.G).predict(weight="weight") assert found == pytest.approx(Scoresheet(known))
def test_adamic_adar(self): known = { (1, 5): 10 / log(30), (1, 4): 2 / log(5) + 5 / log(30), (2, 3): 2 / log(5) + 5 / log(26), (4, 5): 2 / log(30), } found = nbr.AdamicAdar(self.G).predict(weight="weight") assert found == pytest.approx(Scoresheet(known))
def test_pearson(self): known = { (1, 5): 0.61237243, (2, 3): 2 / 3, (1, 4): 1, (4, 5): 0.61237243 } found = Pearson(self.G).predict() assert_dict_almost_equal(found, Scoresheet(known))
def test_nmeasure(self): known = { (1, 5): sqrt(2 / 5), (2, 3): sqrt(8 / 13), (1, 4): 1, (4, 5): sqrt(2 / 5) } found = NMeasure(self.G).predict() assert_dict_almost_equal(found, Scoresheet(known))
def test_cosine(self): known = { (1, 5): 1 / sqrt(2), (2, 3): 2 / sqrt(6), (1, 4): 1, (4, 5): 1 / sqrt(2) } found = Cosine(self.G).predict() assert_dict_almost_equal(found, Scoresheet(known))
def test_resource_allocation(self): known = { (1, 5): 1 / 3, (2, 3): 77 / 130, (1, 4): 17 / 30, (4, 5): 1 / 15 } found = ResourceAllocation(self.G).predict(weight='weight') assert_dict_almost_equal(found, Scoresheet(known))
def test_adamic_adar(self): known = { (1, 5): 1 / log(3), (2, 3): 2 / log(2), (1, 4): 1 / log(2) + 1 / log(3), (4, 5): 1 / log(3) } found = AdamicAdar(self.G).predict() assert_dict_almost_equal(found, Scoresheet(known))
def test_pearson(self): known = { (1, 5): 0.9798502, (2, 3): 0.2965401, (1, 4): 0.4383540, (4, 5): 0.25 } found = Pearson(self.G).predict(weight='weight') assert_dict_almost_equal(found, Scoresheet(known))
def test_nmeasure(self): known = { (1, 5): sqrt(50 / 173), (2, 3): sqrt(98 / 925), (1, 4): sqrt(98 / 701), (4, 5): sqrt(8 / 41) } found = NMeasure(self.G).predict(weight='weight') assert_dict_almost_equal(found, Scoresheet(known))
def test_adamic_adar(self): known = { (1, 5): 1 / log(3), (2, 3): 2 / log(2), (1, 4): 1 / log(2) + 1 / log(3), (4, 5): 1 / log(3), } found = nbr.AdamicAdar(self.G).predict() assert found == pytest.approx(Scoresheet(known))
def test_nmeasure(self): known = { (1, 5): sqrt(50 / 173), (2, 3): sqrt(98 / 925), (1, 4): sqrt(98 / 701), (4, 5): sqrt(8 / 41), } found = nbr.NMeasure(self.G).predict(weight="weight") assert found == pytest.approx(Scoresheet(known))
def test_adamic_adar(self): known = { (1, 5): 10 / log(30), (1, 4): 2 / log(5) + 5 / log(30), (2, 3): 2 / log(5) + 5 / log(26), (4, 5): 2 / log(30) } found = AdamicAdar(self.G).predict(weight='weight') assert_dict_almost_equal(found, Scoresheet(known))
def test_pearson(self): known = { (1, 5): 0.61237243, (2, 3): 2 / 3, (1, 4): 1, (4, 5): 0.61237243 } found = nbr.Pearson(self.G).predict() assert found == pytest.approx(Scoresheet(known))
def test_cosine(self): known = { (1, 5): 1 / sqrt(2), (2, 3): 2 / sqrt(6), (1, 4): 1, (4, 5): 1 / sqrt(2), } found = nbr.Cosine(self.G).predict() assert found == pytest.approx(Scoresheet(known))
def test_nmeasure(self): known = { (1, 5): sqrt(2 / 5), (2, 3): sqrt(8 / 13), (1, 4): 1, (4, 5): sqrt(2 / 5), } found = nbr.NMeasure(self.G).predict() assert found == pytest.approx(Scoresheet(known))
def test_resource_allocation(self): known = { (1, 5): 1 / 3, (2, 3): 77 / 130, (1, 4): 17 / 30, (4, 5): 1 / 15 } found = nbr.ResourceAllocation(self.G).predict(weight="weight") assert found == pytest.approx(Scoresheet(known))
def test_pearson(self): known = { (1, 5): 0.9798502, (2, 3): 0.2965401, (1, 4): 0.4383540, (4, 5): 0.25 } found = nbr.Pearson(self.G).predict(weight="weight") assert found == pytest.approx(Scoresheet(known))
def test_cosine(self): known = { (1, 5): 10 / sqrt(104), (2, 3): 7 / sqrt(150), (1, 4): 7 / sqrt(130), (4, 5): 2 / sqrt(20) } found = Cosine(self.G).predict(weight='weight') assert_dict_almost_equal(found, Scoresheet(known))
def test_weighted_alpha(self): from math import sqrt known = {(0, 1): 1, (0, 2): sqrt(3), (1, 2): 1, (1, 3): sqrt(2), (2, 4): 1, (0, 3): 1 / (1 + 1 / sqrt(2)), (0, 4): 1 / (1 + 1 / sqrt(3)), (1, 4): 0.5, (2, 3): 1 / (1 + 1 / sqrt(2)), (3, 4): 1 / (2 + 1 / sqrt(2))} known = Scoresheet(known) graph_distance = GraphDistance(self.G).predict(alpha=0.5) assert_dict_almost_equal(graph_distance, known)
def test_unweighted(self): known = {(0, 1): 1, (0, 2): 1, (1, 2): 1, (1, 3): 1, (2, 4): 1, (0, 3): 0.5, (0, 4): 0.5, (1, 4): 0.5, (2, 3): 0.5, (3, 4): 1 / 3} known = Scoresheet(known) graph_distance = GraphDistance(self.G).predict(weight=None) assert_dict_almost_equal(graph_distance, known) graph_distance = GraphDistance(self.G).predict(alpha=0) assert_dict_almost_equal(graph_distance, known)
def test_degree_product(self): known = { (1, 2): 130, (1, 3): 780, (1, 4): 130, (1, 5): 104, (2, 3): 150, (2, 4): 25, (2, 5): 20, (3, 4): 150, (3, 5): 120, (4, 5): 20 } found = DegreeProduct(self.G).predict(weight='weight') assert_dict_almost_equal(found, Scoresheet(known))
def test_degree_product(self): known = { (1, 2): 4, (1, 3): 6, (1, 4): 4, (1, 5): 2, (2, 3): 6, (2, 4): 4, (2, 5): 2, (3, 4): 6, (3, 5): 3, (4, 5): 2 } found = DegreeProduct(self.G).predict() assert_dict_almost_equal(found, Scoresheet(known))
def test_degree_product(self): known = { (1, 2): 4, (1, 3): 6, (1, 4): 4, (1, 5): 2, (2, 3): 6, (2, 4): 4, (2, 5): 2, (3, 4): 6, (3, 5): 3, (4, 5): 2, } found = nbr.DegreeProduct(self.G).predict() assert found == pytest.approx(Scoresheet(known))
def test_degree_product(self): known = { (1, 2): 130, (1, 3): 780, (1, 4): 130, (1, 5): 104, (2, 3): 150, (2, 4): 25, (2, 5): 20, (3, 4): 150, (3, 5): 120, (4, 5): 20, } found = nbr.DegreeProduct(self.G).predict(weight="weight") assert found == pytest.approx(Scoresheet(known))
def test_weighted(self): known = { (0, 1): 1, (0, 2): 3, (1, 2): 1, (1, 3): 2, (2, 4): 1, (0, 3): 2 / 3, (0, 4): 0.75, (1, 4): 0.5, (2, 3): 2 / 3, (3, 4): 0.4, } known = Scoresheet(known) graph_distance = GraphDistance(self.G).predict() assert graph_distance == pytest.approx(known)
def test_unweighted(self): known = { (0, 1): 1, (0, 2): 1, (1, 2): 1, (1, 3): 1, (2, 4): 1, (0, 3): 0.5, (0, 4): 0.5, (1, 4): 0.5, (2, 3): 0.5, (3, 4): 1 / 3, } known = Scoresheet(known) graph_distance = GraphDistance(self.G).predict(weight=None) assert graph_distance == pytest.approx(known) graph_distance = GraphDistance(self.G).predict(alpha=0) assert graph_distance == pytest.approx(known)
def test_resource_allocation(self): known = {(1, 5): 1 / 3, (2, 3): 1, (1, 4): 5 / 6, (4, 5): 1 / 3} found = nbr.ResourceAllocation(self.G).predict() assert found == pytest.approx(Scoresheet(known))