(This page has no text content)
Graph Machine Learning Second Edition Learn about the latest advancements in graph data to build robust machine learning models Aldo Marzullo Enrico Deusebio Claudio Stamile
Graph Machine Learning Second Edition Copyright © 2025 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors nor Packt Publishing or its dealers and distributors will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Portfolio Director: Sunith Shetty Relationship Lead: Sanjana Gupta Project Manager: Shashank Desai Content Engineer: Tiksha Abhimanyu Lad Technical Editor: Gaurav Gavas Copy Editor: Safis Editing Indexer: Pratik Shirodkar Proofreader: Safis Editing and Tiksha Lad Production Designer: Ganesh Bhadwalkar and Salma Patel Growth Lead: Bhavesh Amin First published: May 2021 Second edition: June 2025 Production reference: 2150725 Published by Packt Publishing Ltd. Grosvenor House 11 St Paul’s Square Birmingham B3 1RB, UK. ISBN 978-1-80324-806-6 www.packtpub.com
Contributors About the authors Aldo Marzullo received an M.Sc. degree in computer science from the University of Calabria (Cosenza, Italy) in September 2016. During his studies, he developed a solid background in several areas, including algorithm design, graph theory, and machine learning. In January 2020, he received his joint Ph.D. from the University of Calabria and Université Claude Bernard Lyon 1 (Lyon, France), with a thesis titled Deep Learning and Graph Theory for Brain Connectivity Analysis in Multiple Sclerosis. He is currently a postdoctoral researcher and collaborates with several international institutions. For those I hold close, and those I carry quietly with me. Aldo Marzullo Enrico Deusebio is currently working as engineering manager at Canonical, the publisher of Ubuntu, to promote open source technologies in the data and AI space and to make them more accessible to everyone. He has been working with data and distributed computing for over 15 years, both in an academic and industrial context, helping organizations implement data-driven strategies and build AI-powered solutions. He has collaborated and worked with top-tier universities, such as the University of Cambridge, University of Turin, and the Royal Institute of Technology (KTH) in Stockholm, where he obtained a Ph.D. in 2014. He holds a B.Sc. and an M.Sc. degree in aerospace engineering from Politecnico di Torino. To Lili and Pepe, for always reminding me, with your learning process, how wonderful the human brain and life are. Enrico Deusebio
Claudio Stamile received an M.Sc. degree in computer science from the University of Calabria (Cosenza, Italy) in September 2013 and, in September 2017, he received his joint Ph.D. from KU Leuven (Leuven, Belgium) and Université Claude Bernard Lyon 1 (Lyon, France). During his career, he developed a solid background in AI, graph theory and machine learning with a focus on the biomedical field. A Enea, che ha dato senso a molti silenzi. To Enea, who gave meaning to many silences. Claudio Stamile
About the reviewer Nathan Smith is a principal consultant in the professional services division of Neo4j. As a data scientist, he works with companies to apply graph algorithms for machine learning and analytics. He is an organizer of the Data Science Kansas City Meetup, and he enjoys engaging with the data science community through blog posts in Towards Data Science, Medium, and the Neo4j Developer Blog.
(This page has no text content)
Table of Contents Preface xvii Part I: Introduction to Graph Machine Learning 1 Chapter 1: Getting Started with Graphs 3 Practical exercises ��������������������������������������������������������������������������������������������������������������� 4 Conventions ������������������������������������������������������������������������������������������������������������������������ 5 Technical requirements ������������������������������������������������������������������������������������������������������ 5 Introduction to graphs with networkx ������������������������������������������������������������������������������� 5 Types of graphs • 9 Digraphs • 9 Multigraph • 11 Weighted graphs • 13 Multipartite graphs • 14 Connected graphs • 16 Disconnected graphs • 17 Complete graphs • 17 Graph representations • 17 Adjacency matrix • 18 Edge list • 19 Plotting graphs ������������������������������������������������������������������������������������������������������������������ 21 NetworkX • 21
Table of Contentsviii Gephi • 24 Graph properties ��������������������������������������������������������������������������������������������������������������� 29 Integration metrics • 30 Distance, path, and shortest path • 30 Characteristic path length • 31 Global and local efficiency • 32 Segregation metrics • 34 Clustering coefficient • 34 Modularity • 35 Centrality metrics • 36 Degree centrality • 36 Closeness centrality • 37 Betweenness centrality • 37 Resilience metrics • 38 Assortativity coefficient • 38 Hands-on examples ���������������������������������������������������������������������������������������������������������� 40 Simple graphs • 40 Generative graph models • 42 Watts and Strogatz (1998) • 42 Barabási-Albert (1999) • 43 Data resources for network analysis ��������������������������������������������������������������������������������� 45 Network Repository • 45 Stanford Large Network Dataset Collection • 50 Open Graph Benchmark • 51 Dealing with large graphs �������������������������������������������������������������������������������������������������� 51 Summary �������������������������������������������������������������������������������������������������������������������������� 53 Chapter 2: Graph Machine Learning 55 Technical requirements ���������������������������������������������������������������������������������������������������� 56 Understanding machine learning on graphs ��������������������������������������������������������������������� 56 Basic principles of machine learning • 56
Table of Contents ix The benefit of machine learning on graphs • 59 The generalized graph embedding problem ���������������������������������������������������������������������� 61 The taxonomy of graph embedding machine learning algorithms ������������������������������������ 68 Summary ��������������������������������������������������������������������������������������������������������������������������� 71 Chapter 3: Neural Networks and Graphs 73 Technical requirements ���������������������������������������������������������������������������������������������������� 74 Introduction to ANNs �������������������������������������������������������������������������������������������������������� 74 Training neural networks ������������������������������������������������������������������������������������������������� 76 Computational frameworks for ANNs ������������������������������������������������������������������������������� 77 TensorFlow • 78 A simple classification example • 80 PyTorch • 83 A simple classification example • 84 Classification beyond fully connected layers • 87 Introduction to GNNs �������������������������������������������������������������������������������������������������������� 88 Variants of GNNs ��������������������������������������������������������������������������������������������������������������� 90 Frameworks for deep learning on graphs �������������������������������������������������������������������������� 90 Graph representation • 92 Data loading • 92 Model definition • 93 Training loop • 94 PyG • 95 StellarGraph • 97 DGL • 101 Summary ������������������������������������������������������������������������������������������������������������������������ 104
Table of Contentsx Part II: Machine Learning on Graphs 105 Chapter 4: Unsupervised Graph Learning 107 Technical requirements ��������������������������������������������������������������������������������������������������� 107 The unsupervised graph embedding roadmap ���������������������������������������������������������������� 108 Shallow embedding methods ������������������������������������������������������������������������������������������ 109 Matrix factorization • 109 Graph factorization • 111 Higher-order proximity preserved embedding • 113 Graph representation with global structure information • 115 Skip-gram • 117 DeepWalk • 119 Node2Vec • 121 Edge2Vec • 123 Graph2Vec • 126 Autoencoders ������������������������������������������������������������������������������������������������������������������ 128 Our first autoencoder • 130 Denoising autoencoders • 134 Graph autoencoders • 137 Graph neural networks ���������������������������������������������������������������������������������������������������� 139 Spectral graph convolution • 140 Spatial graph convolution • 143 Graph convolution in practice • 144 Summary ������������������������������������������������������������������������������������������������������������������������� 147 Chapter 5: Supervised Graph Learning 149 Technical requirements �������������������������������������������������������������������������������������������������� 150 The supervised graph embedding roadmap �������������������������������������������������������������������� 150 Feature-based methods ��������������������������������������������������������������������������������������������������� 151 Shallow embedding methods ������������������������������������������������������������������������������������������� 154
Table of Contents xi Label propagation algorithm • 155 Label spreading algorithm • 160 Graph regularization methods ���������������������������������������������������������������������������������������� 164 Manifold regularization and semi-supervised embedding • 165 Neural graph learning • 167 Planetoid • 177 Graph CNNs ��������������������������������������������������������������������������������������������������������������������� 179 Graph classification using GCNs • 179 Node classification using GraphSAGE • 182 Summary ������������������������������������������������������������������������������������������������������������������������� 183 Chapter 6: Solving Common Graph-Based Machine Learning Problems 185 Technical requirements �������������������������������������������������������������������������������������������������� 186 Predicting missing links in a graph ��������������������������������������������������������������������������������� 186 Similarity-based methods ������������������������������������������������������������������������������������������������ 187 Index-based methods • 187 Resource allocation index • 187 Jaccard coefficient • 188 Community-based methods • 189 Community common neighbor • 189 Community resource allocation • 190 Embedding-based methods ��������������������������������������������������������������������������������������������� 191 Detecting meaningful structures such as communities �������������������������������������������������� 196 Embedding-based community detection • 197 Spectral methods and matrix factorization • 198 Probability models • 199 Cost function minimization • 200 Detecting graph similarities and graph matching ����������������������������������������������������������� 202 Graph embedding-based methods • 203 Graph kernel-based methods • 206 GNN-based methods • 208
Table of Contentsxii Applications • 208 Summary ������������������������������������������������������������������������������������������������������������������������ 210 Part III: Practical Applications of Graph Machine Learning 211 Chapter 7: Social Network Graphs 213 Technical requirements ��������������������������������������������������������������������������������������������������� 214 Overview of the dataset ��������������������������������������������������������������������������������������������������� 214 Dataset download • 214 Loading the dataset using networkx • 215 Analyzing the graph structure ������������������������������������������������������������������������������������������ 217 Topology overview • 218 Node centrality • 219 Community detection • 222 Embedding for supervised and unsupervised tasks �������������������������������������������������������� 224 Task preparation • 225 Node2Vec-based link prediction • 226 GraphSAGE-based link prediction • 227 Featureless approach • 228 Introducing node features • 231 Hand-crafted features for link prediction • 233 Summarizing the results • 235 Summary ������������������������������������������������������������������������������������������������������������������������ 236 Chapter 8: Text Analytics and Natural Language Processing Using Graphs 237 Technical requirements �������������������������������������������������������������������������������������������������� 238 Providing a quick overview of a dataset �������������������������������������������������������������������������� 238 Understanding the main concepts and tools used in NLP ������������������������������������������������ 240 Creating graphs from a corpus of documents ������������������������������������������������������������������ 245 Knowledge graphs • 245 Bipartite document/entity graphs • 248
Table of Contents xiii Entity-entity graph • 252 Filtering the graph: Be aware of dimensions • 253 Analyzing the graph • 256 Document-document graph • 261 Building a document topic classifier ������������������������������������������������������������������������������� 268 Shallow-learning methods • 270 Graph neural network • 274 Summary ������������������������������������������������������������������������������������������������������������������������ 284 Chapter 9: Graph Analysis for Credit Card Transactions 287 Technical requirements �������������������������������������������������������������������������������������������������� 288 Building graphs from credit card transactions ���������������������������������������������������������������� 288 Overview of the dataset • 288 Loading the dataset • 289 Building the graphs using networkx • 290 Network topology and community detection ����������������������������������������������������������������� 295 Network topology • 295 Community detection • 299 Applying supervised and unsupervised fraud approaches to fraud detection ������������������ 304 Dataset resampling • 305 Node feature generation • 306 Training and evaluating the model • 306 Hyperparameter tuning • 307 Unsupervised approach to fraudulent transaction identification • 309 Additional resources • 311 Summary ������������������������������������������������������������������������������������������������������������������������� 312
Table of Contentsxiv Chapter 10: Building a Data-Driven Graph-Powered Application 315 Technical requirements ��������������������������������������������������������������������������������������������������� 316 Overview of Lambda architecture ������������������������������������������������������������������������������������ 316 Lambda architectures for graph-powered applications ��������������������������������������������������� 319 Graph querying engine ��������������������������������������������������������������������������������������������������� 320 Neo4j • 320 JanusGraph – a graph database to scale out to very large datasets • 323 Graph processing engines ����������������������������������������������������������������������������������������������� 327 Selecting the right technology ����������������������������������������������������������������������������������������� 331 Summary ������������������������������������������������������������������������������������������������������������������������� 331 Part IV: Advanced topics in Graph Machine Learning 333 Chapter 11: Temporal Graph Machine Learning 335 Technical requirements �������������������������������������������������������������������������������������������������� 335 What are dynamic graphs? ���������������������������������������������������������������������������������������������� 336 Common problems with temporal graphs ���������������������������������������������������������������������� 336 Representing dynamic graphs • 337 Embedding dynamic graphs ������������������������������������������������������������������������������������������� 338 Factorization-based methods • 340 Random walk-based methods • 341 Graph kernel-based methods • 341 Temporal point process methods • 342 Deep learning-based methods • 342 Agnostic methods • 342 Hands-on temporal graphs ��������������������������������������������������������������������������������������������� 343 Temporal matrix factorization • 343 Temporal random walk • 347 TGNNs • 348 Summary ������������������������������������������������������������������������������������������������������������������������ 352
Table of Contents xv Further reading ��������������������������������������������������������������������������������������������������������������� 352 Chapter 12: GraphML and LLMs 353 Technical requirements �������������������������������������������������������������������������������������������������� 353 LLMs: an overview ���������������������������������������������������������������������������������������������������������� 354 Why combine GraphML with LLMs? ������������������������������������������������������������������������������� 355 State-of-the-art trends and challenges • 356 LLMs as predictors • 357 LLMs as encoders • 358 LLMs as aligners • 359 Hands-on GraphML with LLMs • 360 LLM as predictor • 361 LLM as encoder • 363 LLM as aligner • 366 Building knowledge graphs from text • 371 Real-world scenarios: GraphRAG • 373 Challenges and future directions ������������������������������������������������������������������������������������ 378 Summary ������������������������������������������������������������������������������������������������������������������������ 378 Further reading ��������������������������������������������������������������������������������������������������������������� 379 Chapter 13: Novel Trends on Graphs 381 Technical requirements �������������������������������������������������������������������������������������������������� 382 Data augmentation for graphs ���������������������������������������������������������������������������������������� 382 Sampling strategies • 382 Exploring data augmentation techniques • 383 Learning about topological data analysis ������������������������������������������������������������������������ 384 Topological machine learning • 386 Applying graph theory in new domains �������������������������������������������������������������������������� 387 Graph machine learning and neuroscience • 387 Graph theory and chemistry and biology • 389 Graph machine learning and computer vision • 389
Table of Contentsxvi Image classification and scene understanding • 389 Shape analysis • 390 Recommendation systems • 390 Graph machine learning and NLP • 390 Summary ������������������������������������������������������������������������������������������������������������������������� 391 Index 393 Other Books You May Enjoy 407
Preface This updated and expanded second edition brings several significant improvements to help you stay ahead in the evolving field of graph machine learning. Compared to the previous version, this edition features refined chapters for improved clarity and flow, new examples utilizing both legacy tools and modern frameworks such as PyTorch and DGL, and entirely new chapters covering cutting-edge topics such as temporal graph machine learning and the integration of large language models (LLMs). Graph Machine Learning provides a powerful toolkit for processing network-structured data and leveraging the relationships between entities for predictive modeling, analytics, and more. You’ll begin with a concise introduction to graph theory, graph machine learning, and neural networks, building a foundational understanding of their principles and applications. As you progress, you’ll dive into the core machine learning models for graph representation learning, exploring their goals, inner workings, and practical implementation across various supervised and unsupervised tasks. You’ll develop an end-to-end machine learning pipeline, from data preprocessing to training and prediction, to fully harness the potential of graph data. Throughout the book, you’ll find real-world scenarios such as social network analysis, natural language processing with graphs, and financial transaction systems. The later chapters take you through the creation of scalable, data-intensive applications for storing, querying, and processing graph data and introduce you to the recent breakthroughs and emerging trends in the domain, some of which are the interaction between graphs and LLMs used in the context of generative AI and retrieval-augmented generation (RAG) systems. By the end of this book, you will have understood the key concepts of graph theory and machine learning algorithms, allowing you to develop impactful graph-based machine learning solutions.
Prefacexviii Who this book is for This book is for data analysts, graph developers, graph analysts, and graph professionals who want to leverage the information embedded in the connections and relations between data points, unravel hidden structures, and exploit topological information to boost their analysis and models’ performance. The book will also be useful for data scientists and machine learning developers who want to build machine learning-driven graph databases. What this book covers Chapter 1, Getting Started with Graphs, introduces the basic concepts of graph theory using the NetworkX Python library. Chapter 2, Graph Machine Learning, introduces the main concepts of graph machine learning and graph embedding techniques. Chapter 3, Neural Networks and Graphs, introduces Graph Neural Networks (GNNs) and the leading libraries for graph-based deep learning. Chapter 4, Unsupervised Graph Learning, covers recent unsupervised graph embedding methods. Chapter 5, Supervised Graph Learning, covers recent supervised graph embedding methods. Chapter 6, Solving Common Graph-Based Machine Learning Problems, introduces the most common machine learning tasks on graphs. Chapter 7, Social Network Graphs, shows an application of machine learning algorithms on social network data. Chapter 8, Text Analytics and Natural Language Processing Using Graphs, shows an application of machine learning algorithms on a natural language processing task. Chapter 9, Graphs Analysis for Credit Card Transactions, shows an application of machine learning algorithms in credit card fraud detection. Chapter 10, Building a Data-Driven Graph-Powered Application, introduces some technologies and techniques useful to deal with large graphs. Chapter 11, Temporal Graph Machine Learning, focuses on techniques to model and learn from dynamic, time-evolving graph data. Chapter 12, GraphML and LLMs, explores how graph structures can enhance LLMs and how LLMs can be used for graph-based tasks.
Preface xix Chapter 13, Novel Trends on Graphs, introduces some novel trends (algorithms and applications) of graph machine learning. To get the most out of this book We recommend that you use Docker to have a reproducible environment and stable dependency sets. The provided Docker images – one for each chapter – ship with a Jupyter installation and a Python kernel with the dependencies pre-installed, which you can use to run all the examples. For some chapters, Neo4j, JanusGraph, and Gephi are also needed. Software/hardware covered in the book OS requirements Python Windows, macOS, and Linux (any) Neo4j Windows, macOS, and Linux (any) Gephi Windows, macOS, and Linux (any) Docker Windows, macOS, and Linux (any) A beginner-level understanding of graph databases and graph data is required. Intermediate-level working knowledge of Python programming and machine learning is also expected to make the most of this book. The authors acknowledge the use of cutting-edge AI, such as ChatGPT, with the sole aim of enhancing the language and clarity within the book, thereby ensuring a smooth reading experience for readers. It’s important to note that the content itself has been crafted by the authors and edited by a professional publishing team. Download the example code files The code bundle for the book is hosted on GitHub at https://github.com/PacktPublishing/ Graph-Machine-Learning. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing. Check them out! Conventions used There are a number of text conventions used throughout this book. CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and X/Twitter handles. For example: “For this exercise, we will be using a GraphSAGE encoder with three layers of 32, 32, and 16 dimensions, respectively.”
Comments 0
Loading comments...
Reply to Comment
Edit Comment