-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhomophily_index.py
More file actions
57 lines (36 loc) · 1.38 KB
/
homophily_index.py
File metadata and controls
57 lines (36 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from datasets import datasets
from torch_geometric.utils import to_networkx
import networkx as nx
import pandas as pd
def calculate_node_homophily_pyg(data) -> float:
total_nodes = data.num_nodes
if total_nodes == 0:
return 0
homophily_sum = 0
graph = to_networkx(data)
for node in range(total_nodes):
neighbors = list(graph.neighbors(node))
if len(neighbors) == 0:
continue
same_class_count = 0
node_class = data.y[node].item()
for neighbor in neighbors:
if data.y[neighbor].item() == node_class:
same_class_count += 1
if len(neighbors) > 0:
homophily_ratio = same_class_count / len(neighbors)
else:
homophily_ratio = 0
homophily_sum += homophily_ratio
node_homophily_index = homophily_sum / total_nodes
return node_homophily_index
results = []
for dataset in datasets:
homophily_index = calculate_node_homophily_pyg(dataset[0])
print(f"Node Homophily Index for {dataset.name}: {homophily_index}")
results.append({'Dataset Name': dataset.name, 'Node Homophily Index': homophily_index})
df = pd.DataFrame(results)
df = df.sort_values(by='Node Homophily Index')
csv_file = 'homophily_indices.csv'
df.to_csv(csv_file, index=False)
print(f"Results saved to {csv_file}")