Book Image

Graph Data Modeling in Python

By : Gary Hutson, Matt Jackson
Book Image

Graph Data Modeling in Python

By: Gary Hutson, Matt Jackson

Overview of this book

Graphs have become increasingly integral to powering the products and services we use in our daily lives, driving social media, online shopping recommendations, and even fraud detection. With this book, you’ll see how a good graph data model can help enhance efficiency and unlock hidden insights through complex network analysis. Graph Data Modeling in Python will guide you through designing, implementing, and harnessing a variety of graph data models using the popular open source Python libraries NetworkX and igraph. Following practical use cases and examples, you’ll find out how to design optimal graph models capable of supporting a wide range of queries and features. Moreover, you’ll seamlessly transition from traditional relational databases and tabular data to the dynamic world of graph data structures that allow powerful, path-based analyses. As well as learning how to manage a persistent graph database using Neo4j, you’ll also get to grips with adapting your network model to evolving data requirements. By the end of this book, you’ll be able to transform tabular data into powerful graph data models. In essence, you’ll build your knowledge from beginner to advanced-level practitioner in no time.
Table of Contents (16 chapters)
1
Part 1: Getting Started with Graph Data Modeling
4
Part 2: Making the Graph Transition
7
Part 3: Storing and Productionizing Graphs
11
Part 4: Graphing Like a Pro

Implementing the model in Python

In the following step, we will load in the data that we are going to be working with.

To begin creating this graph in Python, we can import the nodes from musae_facebook_target.csv, using the standard Python csv library:

import csv
with open('./data/facebook_large/musae_facebook_target.csv', 'r', encoding='utf-8') as csv_file:
reader = csv.reader(csv_file)
data = [line for line in reader]
print(data[:10])
print(len(data))

Here, we open the CSV file with utf-8 encoding, as some node name strings contain non-standard characters. We use csv.reader to read the file, and convert this into a list of lists with a list comprehension (a special construct to encapsulate a loop inside a list to return a new list based on the loop logic, in essence to create a list from another list). Finally, we confirm that the CSV file is loaded correctly by examining the first few lines and checking the length of the imported list, which...