-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpreprocessing.py
More file actions
108 lines (90 loc) · 3.94 KB
/
preprocessing.py
File metadata and controls
108 lines (90 loc) · 3.94 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Load and preprocess the Cora dataset for GCN
import numpy as np
import scipy.sparse as sp
import torch
import pandas as pd
import os
# ================================================================
# One-hot Encoding Function
# Converts paper categories into one-hot encoded vectors
# ================================================================
def encode_onehot(labels):
"""
Convert paper categories (string labels) into one-hot encoded vectors.
Example:
['Neural_Networks', 'Genetic_Algorithms', 'Neural_Networks']
→ [[1,0,0,...], [0,1,0,...], [1,0,0,...]]
"""
classes = set(labels)
classes_dict = {c: np.identity(len(classes))[i, :] for i, c in enumerate(classes)}
labels_onehot = np.array(list(map(classes_dict.get, labels)), dtype=np.int32)
return labels_onehot
# ================================================================
# Normalization Function
# Row-normalizes a sparse matrix (each row sums to 1)
# ================================================================
def normalize(mx):
"""Row-normalize sparse matrix"""
rowsum = np.array(mx.sum(1))
r_inv = np.power(rowsum, -1).flatten()
r_inv[np.isinf(r_inv)] = 0.
r_mat_inv = sp.diags(r_inv)
mx = r_mat_inv.dot(mx)
return mx
# ================================================================
# Convert Scipy Sparse Matrix → PyTorch Sparse Tensor
# ================================================================
def sparse_mx_to_torch_sparse_tensor(sparse_mx):
"""
Convert a scipy sparse matrix to a torch sparse tensor.
"""
sparse_mx = sparse_mx.tocoo().astype(np.float32)
indices = torch.from_numpy(
np.vstack((sparse_mx.row, sparse_mx.col)).astype(np.int64))
values = torch.from_numpy(sparse_mx.data)
shape = torch.Size(sparse_mx.shape)
return torch.sparse_coo_tensor(indices, values, shape)
# ================================================================
# Load Cora Dataset
# ================================================================
def load_data(path="./data/", dataset="cora"):
"""Load citation network dataset (Cora only for now)."""
print(f"Loading {dataset} dataset...")
# Load cora.content
idx_features_labels = np.genfromtxt(f"{path}{dataset}.content", dtype=np.dtype(str))
features = sp.csr_matrix(idx_features_labels[:, 1:-1], dtype=np.float32)
labels = encode_onehot(idx_features_labels[:, -1])
# Load cora.cites
idx = np.array(idx_features_labels[:, 0], dtype=np.int32)
idx_map = {j: i for i, j in enumerate(idx)}
edges_unordered = np.genfromtxt(f"{path}{dataset}.cites", dtype=np.int32)
edges = np.array(list(map(idx_map.get, edges_unordered.flatten())),
dtype=np.int32).reshape(edges_unordered.shape)
adj = sp.coo_matrix((np.ones(edges.shape[0]), (edges[:, 0], edges[:, 1])),
shape=(labels.shape[0], labels.shape[0]),
dtype=np.float32)
# Make the graph symmetric
adj = adj + adj.T.multiply(adj.T > adj) - adj.multiply(adj.T > adj)
# Normalize features and adjacency
features = normalize(features)
adj = normalize(adj + sp.eye(adj.shape[0]))
# Split indices
idx_train = range(140)
idx_val = range(200, 500)
idx_test = range(500, 1500)
# Convert to tensors
features = torch.FloatTensor(np.array(features.todense()))
labels = torch.LongTensor(np.where(labels)[1])
adj = sparse_mx_to_torch_sparse_tensor(adj)
idx_train = torch.LongTensor(idx_train)
idx_val = torch.LongTensor(idx_val)
idx_test = torch.LongTensor(idx_test)
# Return tensors
print("-> Dataset loaded successfully!")
print("Adjacency shape:", adj.shape)
print("Feature shape:", features.shape)
print("Number of classes:", labels.max().item() + 1)
print("Train/Val/Test split:", len(idx_train), len(idx_val), len(idx_test))
return adj, features, labels, idx_train, idx_val, idx_test
if __name__ == "__main__":
load_data()