1
0
Fork 0

Added precision and recall

master
Luca Moretti 4 years ago
parent 3165928ebd
commit 8ce2d6ee04
  1. 32
      main_package/classes/estimators/structure_score_based_estimator.py
  2. 2
      main_package/tests/estimators/test_structure_score_based_estimator.py

@ -62,7 +62,7 @@ class StructureScoreBasedEstimator(se.StructureEstimator):
""" """
'Save the true edges structure in tuples' 'Save the true edges structure in tuples'
true_edges = copy.deepcopy(self.sample_path.structure.edges) true_edges = copy.deepcopy(self.sample_path.structure.edges)
true_edges = list(map(tuple, true_edges)) true_edges = set(map(tuple, true_edges))
'Remove all the edges from the structure' 'Remove all the edges from the structure'
self.sample_path.structure.clean_structure_edges() self.sample_path.structure.clean_structure_edges()
@ -91,23 +91,39 @@ class StructureScoreBasedEstimator(se.StructureEstimator):
print('-------------------------') print('-------------------------')
'TODO: Pensare a un modo migliore -- set difference sembra non funzionare ' 'TODO: Pensare a un modo migliore -- set difference sembra non funzionare '
n_missing_edges = 0 n_missing_edges = 0
n_added_fake_edges = 0 n_added_fake_edges = 0
for estimate_edge in list_edges: set_list_edges = set(list_edges)
if not estimate_edge in true_edges:
n_added_fake_edges += 1 n_added_fake_edges = len(set_list_edges.difference(true_edges))
n_missing_edges = len(true_edges.difference(set_list_edges))
n_true_positive = len(true_edges) - n_missing_edges
precision = n_true_positive / (n_true_positive + n_added_fake_edges)
recall = n_true_positive / (n_true_positive + n_missing_edges)
# for estimate_edge in list_edges:
# if not estimate_edge in true_edges:
# n_added_fake_edges += 1
for true_edge in true_edges: # for true_edge in true_edges:
if not true_edge in list_edges: # if not true_edge in list_edges:
n_missing_edges += 1 # n_missing_edges += 1
print(true_edge) # print(true_edge)
print(f"n archi reali non trovati: {n_missing_edges}") print(f"n archi reali non trovati: {n_missing_edges}")
print(f"n archi non reali aggiunti: {n_added_fake_edges}") print(f"n archi non reali aggiunti: {n_added_fake_edges}")
print(true_edges) print(true_edges)
print(list_edges) print(list_edges)
print(f"precision: {precision} ")
print(f"recall: {recall} ")
def estimate_parents(self,node_id:str, max_parents:int = None, iterations_number:int= 40, patience:int = 10 ): def estimate_parents(self,node_id:str, max_parents:int = None, iterations_number:int= 40, patience:int = 10 ):

@ -32,7 +32,7 @@ class TestStructureScoreBasedEstimator(unittest.TestCase):
def test_esecuzione(self): def test_esecuzione(self):
se1 = se.StructureScoreBasedEstimator(self.s1) se1 = se.StructureScoreBasedEstimator(self.s1)
se1.estimate_structure( se1.estimate_structure(
max_parents = 6, max_parents = 3,
iterations_number = 80, iterations_number = 80,
patience = None patience = None
) )