N2V, UMAP
In figure 2.2, books are shaded based on their political alignment—darker shade for liberal, lighter shade for conservative, and striped for neutral. The categories were assigned by Mark Newman through a qualitative analysis of book descriptions and
reviews posted on Amazon. This dataset, compiled by Valdis Krebs and available through the GNN in Action repository (https://mng.bz/qxnE) or the Carnegie Mellon University website (https://mng.bz/mG8M), contains 105 books (nodes) and 441 edges (co-purchases). If you want to learn more about the background of this dataset, Krebs has written an article with this information [4].
Kamada-Kawai algorithm
NetworkX, GML
Failed to install node2vec in Python 3.13.3. Successfully installed node2vec in Python 3.12.9:
zzh@ZZHPC:~$ pip install node2vec Collecting node2vec Downloading node2vec-0.5.0-py3-none-any.whl.metadata (849 bytes) Collecting gensim<5.0.0,>=4.3.0 (from node2vec) Downloading gensim-4.3.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (8.1 kB) Collecting joblib<2.0.0,>=1.4.0 (from node2vec) Downloading joblib-1.4.2-py3-none-any.whl.metadata (5.4 kB) Collecting networkx<4.0.0,>=3.1.0 (from node2vec) Downloading networkx-3.4.2-py3-none-any.whl.metadata (6.3 kB) Collecting numpy<2.0.0,>=1.24.0 (from node2vec) Downloading numpy-1.26.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (61 kB) Collecting tqdm<5.0.0,>=4.66.1 (from node2vec) Downloading tqdm-4.67.1-py3-none-any.whl.metadata (57 kB) Collecting scipy<1.14.0,>=1.7.0 (from gensim<5.0.0,>=4.3.0->node2vec) Downloading scipy-1.13.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (60 kB) Collecting smart-open>=1.8.1 (from gensim<5.0.0,>=4.3.0->node2vec) Downloading smart_open-7.1.0-py3-none-any.whl.metadata (24 kB) Collecting wrapt (from smart-open>=1.8.1->gensim<5.0.0,>=4.3.0->node2vec) Downloading wrapt-1.17.2-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (6.4 kB) Downloading node2vec-0.5.0-py3-none-any.whl (7.2 kB) Downloading gensim-4.3.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (26.6 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 26.6/26.6 MB 712.2 kB/s eta 0:00:00 Downloading joblib-1.4.2-py3-none-any.whl (301 kB) Downloading networkx-3.4.2-py3-none-any.whl (1.7 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.7/1.7 MB 1.4 MB/s eta 0:00:00 Downloading numpy-1.26.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.0 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 18.0/18.0 MB 1.1 MB/s eta 0:00:00 Downloading tqdm-4.67.1-py3-none-any.whl (78 kB) Downloading scipy-1.13.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (38.2 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 38.2/38.2 MB 1.5 MB/s eta 0:00:00 Downloading smart_open-7.1.0-py3-none-any.whl (61 kB) Downloading wrapt-1.17.2-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (89 kB) Installing collected packages: wrapt, tqdm, numpy, networkx, joblib, smart-open, scipy, gensim, node2vec Successfully installed gensim-4.3.3 joblib-1.4.2 networkx-3.4.2 node2vec-0.5.0 numpy-1.26.4 scipy-1.13.1 smart-open-7.1.0 tqdm-4.67.1 wrapt-1.17.2
In Colab:
import networkx as nx # Load the GML file gml_graph = nx.read_gml('/content/drive/MyDrive/Colab Notebooks/polbooks.gml') print(len(gml_graph.nodes), len(gml_graph.edges)) # 105 441 from collections import defaultdict # Initialize a dictionary to store counts value_counts = defaultdict(int) for _, attr in gml_graph.nodes(data=True): value = attr['value'] value_counts[value] += 1 # Print the counts for each 'value' print(value_counts) defaultdict(<class 'int'>, {'n': 13, 'c': 49, 'l': 43})
import matplotlib.pyplot as plt # Increase figure size plt.figure(figsize=(15, 15)) # Change layout pos = nx.kamada_kawai_layout(gml_graph) # Adjust node size (optional, if you have a measure like degree to scale by) # node_sizes = [gml_graph.degree[node] * 100 for node in gml_graph.nodes()] # Adjust font size font_size = 8 # Define your color mapping color_map = {'l': 'green', 'c': 'orange', 'n': 'grey'} node_colors = [color_map[attr['value']] for _, attr in gml_graph.nodes(data=True)] # Draw nodes nx.draw_networkx_nodes(gml_graph, pos, node_color=node_colors, alpha=0.7) # Draw edges nx.draw_networkx_edges(gml_graph, pos, alpha=0.1) # Draw labels nx.draw_networkx_labels(gml_graph, pos, font_size=font_size) # Remove the axes plt.axis('off') # Show the plot plt.show()
(zpy312) zzh@ZZHPC:/zdata/Github/gnn_in_action$ pip install torch
Collecting torch
Using cached torch-2.7.0-cp312-cp312-manylinux_2_28_x86_64.whl.metadata (29 kB)
Collecting filelock (from torch)
Using cached filelock-3.18.0-py3-none-any.whl.metadata (2.9 kB)
Collecting typing-extensions>=4.10.0 (from torch)
Using cached typing_extensions-4.13.2-py3-none-any.whl.metadata (3.0 kB)
Collecting setuptools (from torch)
Using cached setuptools-79.0.1-py3-none-any.whl.metadata (6.5 kB)
Collecting sympy>=1.13.3 (from torch)
Using cached sympy-1.13.3-py3-none-any.whl.metadata (12 kB)
Requirement already satisfied: networkx in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch) (3.4.2)
Collecting jinja2 (from torch)
Using cached jinja2-3.1.6-py3-none-any.whl.metadata (2.9 kB)
Collecting fsspec (from torch)
Using cached fsspec-2025.3.2-py3-none-any.whl.metadata (11 kB)
Collecting nvidia-cuda-nvrtc-cu12==12.6.77 (from torch)
Using cached nvidia_cuda_nvrtc_cu12-12.6.77-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)
Collecting nvidia-cuda-runtime-cu12==12.6.77 (from torch)
Using cached nvidia_cuda_runtime_cu12-12.6.77-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.metadata (1.5 kB)
Collecting nvidia-cuda-cupti-cu12==12.6.80 (from torch)
Using cached nvidia_cuda_cupti_cu12-12.6.80-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.metadata (1.6 kB)
Collecting nvidia-cudnn-cu12==9.5.1.17 (from torch)
Using cached nvidia_cudnn_cu12-9.5.1.17-py3-none-manylinux_2_28_x86_64.whl.metadata (1.6 kB)
Collecting nvidia-cublas-cu12==12.6.4.1 (from torch)
Using cached nvidia_cublas_cu12-12.6.4.1-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.metadata (1.5 kB)
Collecting nvidia-cufft-cu12==11.3.0.4 (from torch)
Using cached nvidia_cufft_cu12-11.3.0.4-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.metadata (1.5 kB)
Collecting nvidia-curand-cu12==10.3.7.77 (from torch)
Using cached nvidia_curand_cu12-10.3.7.77-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.metadata (1.5 kB)
Collecting nvidia-cusolver-cu12==11.7.1.2 (from torch)
Using cached nvidia_cusolver_cu12-11.7.1.2-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.metadata (1.6 kB)
Collecting nvidia-cusparse-cu12==12.5.4.2 (from torch)
Using cached nvidia_cusparse_cu12-12.5.4.2-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.metadata (1.6 kB)
Collecting nvidia-cusparselt-cu12==0.6.3 (from torch)
Using cached nvidia_cusparselt_cu12-0.6.3-py3-none-manylinux2014_x86_64.whl.metadata (6.8 kB)
Collecting nvidia-nccl-cu12==2.26.2 (from torch)
Using cached nvidia_nccl_cu12-2.26.2-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.metadata (2.0 kB)
Collecting nvidia-nvtx-cu12==12.6.77 (from torch)
Using cached nvidia_nvtx_cu12-12.6.77-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.metadata (1.6 kB)
Collecting nvidia-nvjitlink-cu12==12.6.85 (from torch)
Using cached nvidia_nvjitlink_cu12-12.6.85-py3-none-manylinux2010_x86_64.manylinux_2_12_x86_64.whl.metadata (1.5 kB)
Collecting nvidia-cufile-cu12==1.11.1.6 (from torch)
Using cached nvidia_cufile_cu12-1.11.1.6-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.metadata (1.5 kB)
Collecting triton==3.3.0 (from torch)
Using cached triton-3.3.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.metadata (1.5 kB)
Collecting mpmath<1.4,>=1.1.0 (from sympy>=1.13.3->torch)
Using cached mpmath-1.3.0-py3-none-any.whl.metadata (8.6 kB)
Collecting MarkupSafe>=2.0 (from jinja2->torch)
Using cached MarkupSafe-3.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (4.0 kB)
Using cached torch-2.7.0-cp312-cp312-manylinux_2_28_x86_64.whl (865.0 MB)
Using cached nvidia_cublas_cu12-12.6.4.1-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (393.1 MB)
Using cached nvidia_cuda_cupti_cu12-12.6.80-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (8.9 MB)
Using cached nvidia_cuda_nvrtc_cu12-12.6.77-py3-none-manylinux2014_x86_64.whl (23.7 MB)
Using cached nvidia_cuda_runtime_cu12-12.6.77-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (897 kB)
Using cached nvidia_cudnn_cu12-9.5.1.17-py3-none-manylinux_2_28_x86_64.whl (571.0 MB)
Downloading nvidia_cufft_cu12-11.3.0.4-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (200.2 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 200.2/200.2 MB 1.7 MB/s eta 0:00:00
Downloading nvidia_cufile_cu12-1.11.1.6-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (1.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.1/1.1 MB 2.4 MB/s eta 0:00:00
Downloading nvidia_curand_cu12-10.3.7.77-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (56.3 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 56.3/56.3 MB 2.6 MB/s eta 0:00:00
Downloading nvidia_cusolver_cu12-11.7.1.2-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (158.2 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 158.2/158.2 MB 2.5 MB/s eta 0:00:00
Downloading nvidia_cusparse_cu12-12.5.4.2-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (216.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 216.6/216.6 MB 3.0 MB/s eta 0:00:00
Downloading nvidia_cusparselt_cu12-0.6.3-py3-none-manylinux2014_x86_64.whl (156.8 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 156.8/156.8 MB 2.7 MB/s eta 0:00:00
Downloading nvidia_nccl_cu12-2.26.2-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (201.3 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 201.3/201.3 MB 1.9 MB/s eta 0:00:00
Downloading nvidia_nvjitlink_cu12-12.6.85-py3-none-manylinux2010_x86_64.manylinux_2_12_x86_64.whl (19.7 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 19.7/19.7 MB 1.7 MB/s eta 0:00:00
Downloading nvidia_nvtx_cu12-12.6.77-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (89 kB)
Downloading triton-3.3.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (156.5 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 156.5/156.5 MB 1.7 MB/s eta 0:00:00
Downloading setuptools-79.0.1-py3-none-any.whl (1.3 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.3/1.3 MB 2.0 MB/s eta 0:00:00
Downloading sympy-1.13.3-py3-none-any.whl (6.2 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.2/6.2 MB 2.0 MB/s eta 0:00:00
Downloading typing_extensions-4.13.2-py3-none-any.whl (45 kB)
Downloading filelock-3.18.0-py3-none-any.whl (16 kB)
Downloading fsspec-2025.3.2-py3-none-any.whl (194 kB)
Downloading jinja2-3.1.6-py3-none-any.whl (134 kB)
Downloading MarkupSafe-3.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (23 kB)
Downloading mpmath-1.3.0-py3-none-any.whl (536 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 536.2/536.2 kB 2.2 MB/s eta 0:00:00
Installing collected packages: nvidia-cusparselt-cu12, mpmath, typing-extensions, sympy, setuptools, nvidia-nvtx-cu12, nvidia-nvjitlink-cu12, nvidia-nccl-cu12, nvidia-curand-cu12, nvidia-cufile-cu12, nvidia-cuda-runtime-cu12, nvidia-cuda-nvrtc-cu12, nvidia-cuda-cupti-cu12, nvidia-cublas-cu12, MarkupSafe, fsspec, filelock, triton, nvidia-cusparse-cu12, nvidia-cufft-cu12, nvidia-cudnn-cu12, jinja2, nvidia-cusolver-cu12, torch
Successfully installed MarkupSafe-3.0.2 filelock-3.18.0 fsspec-2025.3.2 jinja2-3.1.6 mpmath-1.3.0 nvidia-cublas-cu12-12.6.4.1 nvidia-cuda-cupti-cu12-12.6.80 nvidia-cuda-nvrtc-cu12-12.6.77 nvidia-cuda-runtime-cu12-12.6.77 nvidia-cudnn-cu12-9.5.1.17 nvidia-cufft-cu12-11.3.0.4 nvidia-cufile-cu12-1.11.1.6 nvidia-curand-cu12-10.3.7.77 nvidia-cusolver-cu12-11.7.1.2 nvidia-cusparse-cu12-12.5.4.2 nvidia-cusparselt-cu12-0.6.3 nvidia-nccl-cu12-2.26.2 nvidia-nvjitlink-cu12-12.6.85 nvidia-nvtx-cu12-12.6.77 setuptools-79.0.1 sympy-1.13.3 torch-2.7.0 triton-3.3.0 typing-extensions-4.13.2
(zpy312) zzh@ZZHPC:/zdata/Github/gnn_in_action$ pip install torchvision
Collecting torchvision
Downloading torchvision-0.22.0-cp312-cp312-manylinux_2_28_x86_64.whl.metadata (6.1 kB)
Requirement already satisfied: numpy in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torchvision) (1.26.4)
Requirement already satisfied: torch==2.7.0 in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torchvision) (2.7.0)
Collecting pillow!=8.3.*,>=5.3.0 (from torchvision)
Downloading pillow-11.2.1-cp312-cp312-manylinux_2_28_x86_64.whl.metadata (8.9 kB)
Requirement already satisfied: filelock in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (3.18.0)
Requirement already satisfied: typing-extensions>=4.10.0 in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (4.13.2)
Requirement already satisfied: setuptools in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (79.0.1)
Requirement already satisfied: sympy>=1.13.3 in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (1.13.3)
Requirement already satisfied: networkx in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (3.4.2)
Requirement already satisfied: jinja2 in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (3.1.6)
Requirement already satisfied: fsspec in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (2025.3.2)
Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.6.77 in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (12.6.77)
Requirement already satisfied: nvidia-cuda-runtime-cu12==12.6.77 in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (12.6.77)
Requirement already satisfied: nvidia-cuda-cupti-cu12==12.6.80 in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (12.6.80)
Requirement already satisfied: nvidia-cudnn-cu12==9.5.1.17 in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (9.5.1.17)
Requirement already satisfied: nvidia-cublas-cu12==12.6.4.1 in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (12.6.4.1)
Requirement already satisfied: nvidia-cufft-cu12==11.3.0.4 in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (11.3.0.4)
Requirement already satisfied: nvidia-curand-cu12==10.3.7.77 in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (10.3.7.77)
Requirement already satisfied: nvidia-cusolver-cu12==11.7.1.2 in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (11.7.1.2)
Requirement already satisfied: nvidia-cusparse-cu12==12.5.4.2 in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (12.5.4.2)
Requirement already satisfied: nvidia-cusparselt-cu12==0.6.3 in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (0.6.3)
Requirement already satisfied: nvidia-nccl-cu12==2.26.2 in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (2.26.2)
Requirement already satisfied: nvidia-nvtx-cu12==12.6.77 in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (12.6.77)
Requirement already satisfied: nvidia-nvjitlink-cu12==12.6.85 in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (12.6.85)
Requirement already satisfied: nvidia-cufile-cu12==1.11.1.6 in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (1.11.1.6)
Requirement already satisfied: triton==3.3.0 in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from torch==2.7.0->torchvision) (3.3.0)
Requirement already satisfied: mpmath<1.4,>=1.1.0 in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from sympy>=1.13.3->torch==2.7.0->torchvision) (1.3.0)
Requirement already satisfied: MarkupSafe>=2.0 in /home/zzh/venvs/zpy312/lib/python3.12/site-packages (from jinja2->torch==2.7.0->torchvision) (3.0.2)
Downloading torchvision-0.22.0-cp312-cp312-manylinux_2_28_x86_64.whl (7.4 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 7.4/7.4 MB 2.3 MB/s eta 0:00:00
Downloading pillow-11.2.1-cp312-cp312-manylinux_2_28_x86_64.whl (4.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.6/4.6 MB 2.0 MB/s eta 0:00:00
Installing collected packages: pillow, torchvision
Successfully installed pillow-11.2.1 torchvision-0.22.0
Successfully installed torch and torchvision, but failed to install torch-scatter, torch-sparse, torch-cluster, torch-spline-conv, all got below error:
ModuleNotFoundError: No module named 'torch'
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error
Also tried Python 3.11.9, all failed too. Python 3.11.9 and Python 3.10.17 also installed torch-2.7.0.
It looks like torch-2.7.0 is not in the list on page https://data.pyg.org/whl/ .
(zpy312) zzh@ZZHPC:/zdata/Github/gnn_in_action$ pip install torch-geometric
Collecting torch-geometric
Downloading torch_geometric-2.6.1-py3-none-any.whl.metadata (63 kB)
Collecting aiohttp (from torch-geometric)
Downloading aiohttp-3.11.18-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (7.7 kB)
Requirement already satisfied: fsspec in ./venvs/zpy312/lib/python3.12/site-packages (from torch-geometric) (2025.3.2)
Requirement already satisfied: jinja2 in ./venvs/zpy312/lib/python3.12/site-packages (from torch-geometric) (3.1.6)
Requirement already satisfied: numpy in ./venvs/zpy312/lib/python3.12/site-packages (from torch-geometric) (1.26.4)
Collecting psutil>=5.8.0 (from torch-geometric)
Downloading psutil-7.0.0-cp36-abi3-manylinux_2_12_x86_64.manylinux2010_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (22 kB)
Collecting pyparsing (from torch-geometric)
Downloading pyparsing-3.2.3-py3-none-any.whl.metadata (5.0 kB)
Collecting requests (from torch-geometric)
Downloading requests-2.32.3-py3-none-any.whl.metadata (4.6 kB)
Requirement already satisfied: tqdm in ./venvs/zpy312/lib/python3.12/site-packages (from torch-geometric) (4.67.1)
Collecting aiohappyeyeballs>=2.3.0 (from aiohttp->torch-geometric)
Downloading aiohappyeyeballs-2.6.1-py3-none-any.whl.metadata (5.9 kB)
Collecting aiosignal>=1.1.2 (from aiohttp->torch-geometric)
Downloading aiosignal-1.3.2-py2.py3-none-any.whl.metadata (3.8 kB)
Collecting attrs>=17.3.0 (from aiohttp->torch-geometric)
Downloading attrs-25.3.0-py3-none-any.whl.metadata (10 kB)
Collecting frozenlist>=1.1.1 (from aiohttp->torch-geometric)
Downloading frozenlist-1.6.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (16 kB)
Collecting multidict<7.0,>=4.5 (from aiohttp->torch-geometric)
Downloading multidict-6.4.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (5.3 kB)
Collecting propcache>=0.2.0 (from aiohttp->torch-geometric)
Downloading propcache-0.3.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (10 kB)
Collecting yarl<2.0,>=1.17.0 (from aiohttp->torch-geometric)
Downloading yarl-1.20.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (72 kB)
Requirement already satisfied: MarkupSafe>=2.0 in ./venvs/zpy312/lib/python3.12/site-packages (from jinja2->torch-geometric) (3.0.2)
Collecting charset-normalizer<4,>=2 (from requests->torch-geometric)
Downloading charset_normalizer-3.4.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (35 kB)
Collecting idna<4,>=2.5 (from requests->torch-geometric)
Downloading idna-3.10-py3-none-any.whl.metadata (10 kB)
Collecting urllib3<3,>=1.21.1 (from requests->torch-geometric)
Downloading urllib3-2.4.0-py3-none-any.whl.metadata (6.5 kB)
Collecting certifi>=2017.4.17 (from requests->torch-geometric)
Downloading certifi-2025.1.31-py3-none-any.whl.metadata (2.5 kB)
Downloading torch_geometric-2.6.1-py3-none-any.whl (1.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.1/1.1 MB 1.3 MB/s eta 0:00:00
Downloading psutil-7.0.0-cp36-abi3-manylinux_2_12_x86_64.manylinux2010_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (277 kB)
Downloading aiohttp-3.11.18-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.7/1.7 MB 774.1 kB/s eta 0:00:00
Downloading pyparsing-3.2.3-py3-none-any.whl (111 kB)
Downloading requests-2.32.3-py3-none-any.whl (64 kB)
Downloading aiohappyeyeballs-2.6.1-py3-none-any.whl (15 kB)
Downloading aiosignal-1.3.2-py2.py3-none-any.whl (7.6 kB)
Downloading attrs-25.3.0-py3-none-any.whl (63 kB)
Downloading certifi-2025.1.31-py3-none-any.whl (166 kB)
Downloading charset_normalizer-3.4.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (145 kB)
Downloading frozenlist-1.6.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (316 kB)
Downloading idna-3.10-py3-none-any.whl (70 kB)
Downloading multidict-6.4.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (223 kB)
Downloading propcache-0.3.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (245 kB)
Downloading urllib3-2.4.0-py3-none-any.whl (128 kB)
Downloading yarl-1.20.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (349 kB)
Installing collected packages: urllib3, pyparsing, psutil, propcache, multidict, idna, frozenlist, charset-normalizer, certifi, attrs, aiohappyeyeballs, yarl, requests, aiosignal, aiohttp, torch-geometric
Successfully installed aiohappyeyeballs-2.6.1 aiohttp-3.11.18 aiosignal-1.3.2 attrs-25.3.0 certifi-2025.1.31 charset-normalizer-3.4.1 frozenlist-1.6.0 idna-3.10 multidict-6.4.3 propcache-0.3.1 psutil-7.0.0 pyparsing-3.2.3 requests-2.32.3 torch-geometric-2.6.1 urllib3-2.4.0 yarl-1.20.0
In Colab:
!pip install torch torchvision
Requirement already satisfied: torch in /usr/local/lib/python3.11/dist-packages (2.6.0+cu124)
Requirement already satisfied: torchvision in /usr/local/lib/python3.11/dist-packages (0.21.0+cu124)
Requirement already satisfied: filelock in /usr/local/lib/python3.11/dist-packages (from torch) (3.18.0)
Requirement already satisfied: typing-extensions>=4.10.0 in /usr/local/lib/python3.11/dist-packages (from torch) (4.13.2)
Requirement already satisfied: networkx in /usr/local/lib/python3.11/dist-packages (from torch) (3.4.2)
Requirement already satisfied: jinja2 in /usr/local/lib/python3.11/dist-packages (from torch) (3.1.6)
Requirement already satisfied: fsspec in /usr/local/lib/python3.11/dist-packages (from torch) (2025.3.2)
Collecting nvidia-cuda-nvrtc-cu12==12.4.127 (from torch)
Downloading nvidia_cuda_nvrtc_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)
Collecting nvidia-cuda-runtime-cu12==12.4.127 (from torch)
Downloading nvidia_cuda_runtime_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)
Collecting nvidia-cuda-cupti-cu12==12.4.127 (from torch)
Downloading nvidia_cuda_cupti_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl.metadata (1.6 kB)
Collecting nvidia-cudnn-cu12==9.1.0.70 (from torch)
Downloading nvidia_cudnn_cu12-9.1.0.70-py3-none-manylinux2014_x86_64.whl.metadata (1.6 kB)
Collecting nvidia-cublas-cu12==12.4.5.8 (from torch)
Downloading nvidia_cublas_cu12-12.4.5.8-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)
Collecting nvidia-cufft-cu12==11.2.1.3 (from torch)
Downloading nvidia_cufft_cu12-11.2.1.3-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)
Collecting nvidia-curand-cu12==10.3.5.147 (from torch)
Downloading nvidia_curand_cu12-10.3.5.147-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)
Collecting nvidia-cusolver-cu12==11.6.1.9 (from torch)
Downloading nvidia_cusolver_cu12-11.6.1.9-py3-none-manylinux2014_x86_64.whl.metadata (1.6 kB)
Collecting nvidia-cusparse-cu12==12.3.1.170 (from torch)
Downloading nvidia_cusparse_cu12-12.3.1.170-py3-none-manylinux2014_x86_64.whl.metadata (1.6 kB)
Requirement already satisfied: nvidia-cusparselt-cu12==0.6.2 in /usr/local/lib/python3.11/dist-packages (from torch) (0.6.2)
Requirement already satisfied: nvidia-nccl-cu12==2.21.5 in /usr/local/lib/python3.11/dist-packages (from torch) (2.21.5)
Requirement already satisfied: nvidia-nvtx-cu12==12.4.127 in /usr/local/lib/python3.11/dist-packages (from torch) (12.4.127)
Collecting nvidia-nvjitlink-cu12==12.4.127 (from torch)
Downloading nvidia_nvjitlink_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)
Requirement already satisfied: triton==3.2.0 in /usr/local/lib/python3.11/dist-packages (from torch) (3.2.0)
Requirement already satisfied: sympy==1.13.1 in /usr/local/lib/python3.11/dist-packages (from torch) (1.13.1)
Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.11/dist-packages (from sympy==1.13.1->torch) (1.3.0)
Requirement already satisfied: numpy in /usr/local/lib/python3.11/dist-packages (from torchvision) (2.0.2)
Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /usr/local/lib/python3.11/dist-packages (from torchvision) (11.1.0)
Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.11/dist-packages (from jinja2->torch) (3.0.2)
Downloading nvidia_cublas_cu12-12.4.5.8-py3-none-manylinux2014_x86_64.whl (363.4 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 363.4/363.4 MB 4.2 MB/s eta 0:00:00
Downloading nvidia_cuda_cupti_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (13.8 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 13.8/13.8 MB 50.5 MB/s eta 0:00:00
Downloading nvidia_cuda_nvrtc_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (24.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 24.6/24.6 MB 45.3 MB/s eta 0:00:00
Downloading nvidia_cuda_runtime_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (883 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 883.7/883.7 kB 34.9 MB/s eta 0:00:00
Downloading nvidia_cudnn_cu12-9.1.0.70-py3-none-manylinux2014_x86_64.whl (664.8 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 664.8/664.8 MB 2.6 MB/s eta 0:00:00
Downloading nvidia_cufft_cu12-11.2.1.3-py3-none-manylinux2014_x86_64.whl (211.5 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 211.5/211.5 MB 3.8 MB/s eta 0:00:00
Downloading nvidia_curand_cu12-10.3.5.147-py3-none-manylinux2014_x86_64.whl (56.3 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 56.3/56.3 MB 10.0 MB/s eta 0:00:00
Downloading nvidia_cusolver_cu12-11.6.1.9-py3-none-manylinux2014_x86_64.whl (127.9 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 127.9/127.9 MB 7.5 MB/s eta 0:00:00
Downloading nvidia_cusparse_cu12-12.3.1.170-py3-none-manylinux2014_x86_64.whl (207.5 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 207.5/207.5 MB 7.2 MB/s eta 0:00:00
Downloading nvidia_nvjitlink_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (21.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 21.1/21.1 MB 42.0 MB/s eta 0:00:00
Installing collected packages: nvidia-nvjitlink-cu12, nvidia-curand-cu12, nvidia-cufft-cu12, nvidia-cuda-runtime-cu12, nvidia-cuda-nvrtc-cu12, nvidia-cuda-cupti-cu12, nvidia-cublas-cu12, nvidia-cusparse-cu12, nvidia-cudnn-cu12, nvidia-cusolver-cu12
Attempting uninstall: nvidia-nvjitlink-cu12
Found existing installation: nvidia-nvjitlink-cu12 12.5.82
Uninstalling nvidia-nvjitlink-cu12-12.5.82:
Successfully uninstalled nvidia-nvjitlink-cu12-12.5.82
Attempting uninstall: nvidia-curand-cu12
Found existing installation: nvidia-curand-cu12 10.3.6.82
Uninstalling nvidia-curand-cu12-10.3.6.82:
Successfully uninstalled nvidia-curand-cu12-10.3.6.82
Attempting uninstall: nvidia-cufft-cu12
Found existing installation: nvidia-cufft-cu12 11.2.3.61
Uninstalling nvidia-cufft-cu12-11.2.3.61:
Successfully uninstalled nvidia-cufft-cu12-11.2.3.61
Attempting uninstall: nvidia-cuda-runtime-cu12
Found existing installation: nvidia-cuda-runtime-cu12 12.5.82
Uninstalling nvidia-cuda-runtime-cu12-12.5.82:
Successfully uninstalled nvidia-cuda-runtime-cu12-12.5.82
Attempting uninstall: nvidia-cuda-nvrtc-cu12
Found existing installation: nvidia-cuda-nvrtc-cu12 12.5.82
Uninstalling nvidia-cuda-nvrtc-cu12-12.5.82:
Successfully uninstalled nvidia-cuda-nvrtc-cu12-12.5.82
Attempting uninstall: nvidia-cuda-cupti-cu12
Found existing installation: nvidia-cuda-cupti-cu12 12.5.82
Uninstalling nvidia-cuda-cupti-cu12-12.5.82:
Successfully uninstalled nvidia-cuda-cupti-cu12-12.5.82
Attempting uninstall: nvidia-cublas-cu12
Found existing installation: nvidia-cublas-cu12 12.5.3.2
Uninstalling nvidia-cublas-cu12-12.5.3.2:
Successfully uninstalled nvidia-cublas-cu12-12.5.3.2
Attempting uninstall: nvidia-cusparse-cu12
Found existing installation: nvidia-cusparse-cu12 12.5.1.3
Uninstalling nvidia-cusparse-cu12-12.5.1.3:
Successfully uninstalled nvidia-cusparse-cu12-12.5.1.3
Attempting uninstall: nvidia-cudnn-cu12
Found existing installation: nvidia-cudnn-cu12 9.3.0.75
Uninstalling nvidia-cudnn-cu12-9.3.0.75:
Successfully uninstalled nvidia-cudnn-cu12-9.3.0.75
Attempting uninstall: nvidia-cusolver-cu12
Found existing installation: nvidia-cusolver-cu12 11.6.3.83
Uninstalling nvidia-cusolver-cu12-11.6.3.83:
Successfully uninstalled nvidia-cusolver-cu12-11.6.3.83
Successfully installed nvidia-cublas-cu12-12.4.5.8 nvidia-cuda-cupti-cu12-12.4.127 nvidia-cuda-nvrtc-cu12-12.4.127 nvidia-cuda-runtime-cu12-12.4.127 nvidia-cudnn-cu12-9.1.0.70 nvidia-cufft-cu12-11.2.1.3 nvidia-curand-cu12-10.3.5.147 nvidia-cusolver-cu12-11.6.1.9 nvidia-cusparse-cu12-12.3.1.170 nvidia-nvjitlink-cu12-12.4.127
#!pip install torch-scatter torch-sparse torch-cluster torch-spline-conv torch-geometric !pip install torch-geometric Collecting torch-geometric Downloading torch_geometric-2.6.1-py3-none-any.whl.metadata (63 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 63.1/63.1 kB 2.8 MB/s eta 0:00:00 Requirement already satisfied: aiohttp in /usr/local/lib/python3.11/dist-packages (from torch-geometric) (3.11.15) Requirement already satisfied: fsspec in /usr/local/lib/python3.11/dist-packages (from torch-geometric) (2025.3.2) Requirement already satisfied: jinja2 in /usr/local/lib/python3.11/dist-packages (from torch-geometric) (3.1.6) Requirement already satisfied: numpy in /usr/local/lib/python3.11/dist-packages (from torch-geometric) (2.0.2) Requirement already satisfied: psutil>=5.8.0 in /usr/local/lib/python3.11/dist-packages (from torch-geometric) (5.9.5) Requirement already satisfied: pyparsing in /usr/local/lib/python3.11/dist-packages (from torch-geometric) (3.2.3) Requirement already satisfied: requests in /usr/local/lib/python3.11/dist-packages (from torch-geometric) (2.32.3) Requirement already satisfied: tqdm in /usr/local/lib/python3.11/dist-packages (from torch-geometric) (4.67.1) Requirement already satisfied: aiohappyeyeballs>=2.3.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp->torch-geometric) (2.6.1) Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.11/dist-packages (from aiohttp->torch-geometric) (1.3.2) Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp->torch-geometric) (25.3.0) Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.11/dist-packages (from aiohttp->torch-geometric) (1.6.0) Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.11/dist-packages (from aiohttp->torch-geometric) (6.4.3) Requirement already satisfied: propcache>=0.2.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp->torch-geometric) (0.3.1) Requirement already satisfied: yarl<2.0,>=1.17.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp->torch-geometric) (1.20.0) Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.11/dist-packages (from jinja2->torch-geometric) (3.0.2) Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests->torch-geometric) (3.4.1) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.11/dist-packages (from requests->torch-geometric) (3.10) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests->torch-geometric) (2.3.0) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.11/dist-packages (from requests->torch-geometric) (2025.1.31) Downloading torch_geometric-2.6.1-py3-none-any.whl (1.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.1/1.1 MB 16.6 MB/s eta 0:00:00 Installing collected packages: torch-geometric Successfully installed torch-geometric-2.6.1
GCN stands for Graph Convolutional Network.
A GCN is a type of neural network designed specifically to work with graph-structured data — meaning data where the relationships between points (edges) are just as important as the points (nodes) themselves. Unlike traditional neural networks that operate on regular grids (like images or sequences), GCNs can directly model and learn from the complex connectivity patterns of graphs.
At a high level, here's how GCNs work:
-
Each node in the graph has a feature vector.
-
In a GCN, during each layer, a node updates its feature by aggregating (usually summing or averaging) the features of its neighbors (and often itself), followed by applying a learnable transformation (like a weight matrix and non-linear activation).
-
As you stack multiple GCN layers, each node gathers information from farther and farther away in the graph — capturing both local and global structure.
import networkx as nx import torch import torch.nn.functional as F from torch_geometric.nn import GCNConv
class SimpleGNN(torch.nn.Module): def __init__(self, num_features, hidden_channels): super(SimpleGNN, self).__init__() self.conv1 = GCNConv(num_features, hidden_channels) self.conv2 = GCNConv(hidden_channels, hidden_channels) def forward(self, x, edge_index): # First Graph Convolutional Layer x = self.conv1(x, edge_index) x = F.relu(x) x = F.dropout(x, training=self.training) # Second Graph Convolutional Layer x = self.conv2(x, edge_index) return x, F.log_softmax(x, dim=1)
from torch_geometric.utils.convert import from_networkx import torch.nn as nn # Load the GML file gml_graph = nx.read_gml('/content/drive/MyDrive/Colab Notebooks/polbooks.gml') # Convert NetworkX graph to a PyTorch Geometric Data object data = from_networkx(gml_graph) # Initialize node features with Xavier initialization num_features = 64 # Set the desired number of features data.x = torch.empty((data.num_nodes, num_features), dtype=torch.float) # Create an empty tensor nn.init.xavier_uniform_(data.x) # Apply Xavier initialization # Instantiate the model model = SimpleGNN(num_features=data.x.shape[1], hidden_channels=64) # Switch the model to evaluation mode model.eval() # Perform a forward pass with no gradient computation with torch.no_grad(): gnn_embeddings = model(data.x, data.edge_index)
gnn_embeddings (tensor([[-0.0188, -0.0429, -0.0008, ..., 0.0182, -0.0553, 0.0112], [-0.0187, -0.0310, -0.0011, ..., 0.0151, -0.0455, 0.0107], [-0.0172, -0.0482, -0.0019, ..., 0.0187, -0.0419, 0.0134], ..., [ 0.0009, -0.0245, -0.0090, ..., 0.0324, -0.0461, -0.0017], [-0.0030, -0.0256, -0.0153, ..., 0.0265, -0.0297, -0.0150], [-0.0031, -0.0276, -0.0151, ..., 0.0354, -0.0297, -0.0144]]), tensor([[-4.1764, -4.2005, -4.1584, ..., -4.1394, -4.2129, -4.1464], [-4.1767, -4.1890, -4.1590, ..., -4.1429, -4.2035, -4.1473], [-4.1752, -4.2062, -4.1599, ..., -4.1393, -4.1999, -4.1446], ..., [-4.1580, -4.1834, -4.1679, ..., -4.1265, -4.2050, -4.1606], [-4.1625, -4.1852, -4.1748, ..., -4.1331, -4.1892, -4.1745], [-4.1624, -4.1869, -4.1743, ..., -4.1239, -4.1890, -4.1737]]))
umap.UMAP
refers to the main class in the UMAP library, where UMAP stands for Uniform Manifold Approximation and Projection.
It’s a very powerful and popular algorithm for dimensionality reduction, similar to t-SNE but faster, scalable to large datasets, and often better at preserving global structure.
In simple terms:
-
Input: High-dimensional data (e.g., 1000 features).
-
UMAP learns the manifold structure (hidden shape) of the data.
-
Output: Low-dimensional data (e.g., 2D or 3D), keeping important relationships intact.
-
It is unsupervised by default, but can also use labels if you want supervised dimensionality reduction.
Typical usage
import umap import numpy as np # Some random high-dimensional data X = np.random.rand(100, 50) # 100 points, 50 dimensions # Create a UMAP model reducer = umap.UMAP(n_neighbors=15, n_components=2, random_state=42) # Fit and transform X_embedded = reducer.fit_transform(X) print(X_embedded.shape) # (100, 2)
Now X_embedded
is a 2D version of the original data — you can plot it nicely!
Key Parameters in umap.UMAP()
Parameter | Meaning |
---|---|
n_neighbors |
How many neighbors to look at when learning structure (balance between local vs. global structure). Default = 15. |
n_components |
Target dimensionality (e.g., 2 for 2D, 3 for 3D). |
metric |
How distance is measured (e.g., 'euclidean', 'cosine', etc.). |
min_dist |
How tightly UMAP packs points together. Smaller values = tighter clusters. |
random_state |
Seed for randomness (for reproducibility). |
spread |
How much to spread out the points overall. |
Why use UMAP?
-
🔥 Faster than t-SNE on large datasets.
-
🧠 Preserves more global structure (clusters and their relations).
-
🎨 Great for visualizing high-dimensional data (like word embeddings, gene expressions, etc.).
-
🏋️♂️ Scales to millions of points.
-
🛠️ Can be used for clustering, anomaly detection, and preprocessing for supervised learning.
from sklearn.manifold import TSNE import umap import matplotlib.pyplot as plt
# Convert to numpy for t-SNE gnn_embeddings_np = gnn_embeddings[0].detach().cpu().numpy() # Initialize and fit UMAP umap_model = umap.UMAP(n_neighbors=15, min_dist=0.1, n_components=2, random_state=42) umap_features = umap_model.fit_transform(gnn_embeddings_np) # Define your color mapping color_map = {'l': 'green', 'c': 'orange', 'n': 'grey'} node_colors = [color_map[attr['value']] for _, attr in gml_graph.nodes(data=True)] # Plot the result plt.figure(figsize=(10, 10)) plt.scatter(umap_features[:, 0], umap_features[:, 1], c=node_colors, cmap='viridis', s=15) plt.title('Node Embeddings Visualized with UMAP') plt.xlabel('UMAP Feature 0') plt.ylabel('UMAP Feature 1') legend_labels = { 'l': 'Liberal', 'c': 'Conservative', 'n': 'Neutral' } # Create a legend handles = [plt.Line2D([0], [0], marker='o', color='w', label=legend_labels[key], markerfacecolor=color_map[key], markersize=10) for key in legend_labels] plt.legend(handles=handles, title='Node Type') plt.show()
import numpy as np import random # Extract labels and handle neutral values labels = [] for node, data in gml_graph.nodes(data=True): if data['value'] == 'c': labels.append('right') elif data['value'] == 'l': labels.append('left') else: # Handle neutral and missing values labels.append('neutral') labels = np.array(labels) # Random seed for reproducibility random.seed(52) # Indices of all nodes indices = list(range(len(labels))) # Percentage of data to keep as labelled labelled_percentage = 0.2 # Select a subset of indices to remain labelled labelled_indices = random.sample(indices, int(labelled_percentage * len(labels))) # Initialize masks for labelled and unlabelled data labelled_mask = np.zeros(len(labels), dtype=bool) unlabelled_mask = np.ones(len(labels), dtype=bool) # Update masks labelled_mask[labelled_indices] = True unlabelled_mask[labelled_indices] = False # Use masks to split the dataset labelled_labels = labels[labelled_mask] unlabelled_labels = labels[unlabelled_mask] # You won't use these labels during training
from node2vec import Node2Vec # Initialize the N2V model with specified parameters for the input graph n2v = Node2Vec(gml_graph, dimensions=64, walk_length=30, num_walks=200, workers=4) # Train the N2V model model = n2v.fit(window=10, min_count=1, batch_words=4) # Extract and store the node embeddings generated by the N2V model in a dictionary embeddings = {node: model.wv[node] for node in gml_graph.nodes()}
label_mapping = {'left': 0, 'right': 1, 'neutral': 2} numeric_labels = np.array([label_mapping[label] for label in labels]) # For GNN embeddings X_train_gnn, y_train_gnn = gnn_embeddings[0][labelled_mask], numeric_labels[labelled_mask] # For N2V embeddings # Ensure node2vec embeddings are in the same order as labels X_n2v = np.array([embeddings[node] for node in gml_graph.nodes()]) X_train_n2v, y_train_n2v = X_n2v[labelled_mask], numeric_labels[labelled_mask] from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import accuracy_score, f1_score # Classifier for GNN embeddings clf_gnn = RandomForestClassifier() clf_gnn.fit(X_train_gnn, y_train_gnn) # Classifier for N2V embeddings clf_n2v = RandomForestClassifier() clf_n2v.fit(X_train_n2v, y_train_n2v) # Predictions with GNN embeddings y_pred_gnn = clf_gnn.predict(gnn_embeddings[0][unlabelled_mask]) # Evaluate GNN classifier gnn_accuracy = accuracy_score(numeric_labels[unlabelled_mask], y_pred_gnn) gnn_f1_score = f1_score(numeric_labels[unlabelled_mask], y_pred_gnn, average='weighted') # Predictions with N2V embeddings y_pred_n2v = clf_n2v.predict(X_n2v[unlabelled_mask]) # Evaluate N2V classifier n2v_accuracy = accuracy_score(numeric_labels[unlabelled_mask], y_pred_n2v) n2v_f1_score = f1_score(numeric_labels[unlabelled_mask], y_pred_n2v, average='weighted') print(f"GNN Accuracy: {gnn_accuracy:.4f}") print(f"GNN F1 Score: {gnn_f1_score:.4f}") print(f"N2V Accuracy: {n2v_accuracy:.4f}") print(f"N2V F1 Score: {n2v_f1_score:.4f}")
GNN Accuracy: 0.8095 GNN F1 Score: 0.7848 N2V Accuracy: 0.8214 N2V F1 Score: 0.7898