-
Notifications
You must be signed in to change notification settings - Fork 1
/
cora.py
23 lines (16 loc) · 807 Bytes
/
cora.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""
This module provides the Cora dataset.
For background on the dataset see https://www.openicpsr.org/openicpsr/project/100859/version/V1/view.
"""
import os
import networkx as nx
import pandas as pd
def get_graph() -> nx.Graph:
"""Return a NetworkX graph that represents the Cora dataset."""
data_path = os.path.join(os.path.dirname(__file__), "cora_data")
nodes = pd.read_csv(os.path.join(data_path, "cora.content"), sep="\t", header=None, usecols=[0, 1434], names=["node", "subject"])
edges = pd.read_csv(os.path.join(data_path, "cora.cites"), sep="\t", header=None, names=["target", "source"])
G = nx.from_pandas_edgelist(edges)
subject_map = {row["node"]: row["subject"] for i, row in nodes.iterrows()}
nx.set_node_attributes(G, subject_map, "subject")
return G