Skip to content

Commit

Permalink
Add internal density metric
Browse files Browse the repository at this point in the history
  • Loading branch information
Joaquim2805 committed May 23, 2024
1 parent df58b3c commit 588b2ff
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/algorithms/Algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ def evaluate(self) -> list[(str, float)]:

conductance : float = self._get_conductance()
silouhette : float = self. _get_modularity()
internal_density : float = self._get_internal_density()
metrics.append(("Conductance",conductance))
metrics.append(("Modularity",silouhette))
metrics.append(("Internal density",internal_density))


return metrics
Expand Down Expand Up @@ -85,14 +87,13 @@ def _get_conductance(self)->float :
:rtype: float
"""
G = self.graph.nx_graph

# A revoir (se repète)
clusters = defaultdict(set)
for node, cluster_id in enumerate(self.clusters):
clusters[cluster_id].add(node)
partition = list(clusters.values())

conductances = []
print(partition)
for cluster in partition:
cut_size = nx.cut_size(G, cluster)

Expand All @@ -116,23 +117,41 @@ def _get_modularity(self)->float:
:return: Modularity of the clustering
:rtype: float
"""

G = self.graph.nx_graph

clusters = defaultdict(set)
for node, cluster_id in enumerate(self.clusters):
clusters[cluster_id].add(node)
partition = list(clusters.values())
modularity_score = modularity(G, partition)

return modularity_score

def _get_internal_density(self) -> float:
"""Calculates the average internal density .
Returns:
float: The average internal density of each cluster.
"""

G = self.graph.nx_graph

clusters = defaultdict(set)
for node, cluster_id in enumerate(self.clusters):
clusters[cluster_id].add(node)
partition = list(clusters.values())
modularity_score = modularity(G, partition)

return modularity_score

densities = []

for cluster in partition:
subgraph = self.graph.nx_graph.subgraph(cluster)

density = nx.density(subgraph)

densities.append(density)

return np.mean(densities)

def _get_nmi(self) -> float:
"""Returns the Normalized Mutual Information of the clustering
Expand Down

0 comments on commit 588b2ff

Please sign in to comment.