Graph Data Science With Neo4j: Learn How To Use Neo4j 5 With Graph Data Science Library 2.0 and Its Python Driver For Your Project Scifo
Graph Data Science With Neo4j: Learn How To Use Neo4j 5 With Graph Data Science Library 2.0 and Its Python Driver For Your Project Scifo
com
https://textbookfull.com/product/graph-algorithms-for-data-science-
with-examples-in-neo4j-1st-edition-tomaz-bratanic/
textbookfull.com
https://textbookfull.com/product/graph-algorithms-practical-examples-
in-apache-spark-and-neo4j-1st-edition-mark-needham/
textbookfull.com
https://textbookfull.com/product/macroeconomic-policy-and-a-living-
wage-the-employment-act-as-redistributive-economics-1944-1969-donald-
r-stabile/
textbookfull.com
Women s Entrepreneurship in Iran Role Models of Growth
Oriented Iranian Women Entrepreneurs 1st Edition Leyla
Sarfaraz (Auth.)
https://textbookfull.com/product/women-s-entrepreneurship-in-iran-
role-models-of-growth-oriented-iranian-women-entrepreneurs-1st-
edition-leyla-sarfaraz-auth/
textbookfull.com
https://textbookfull.com/product/applied-helping-skills-transforming-
lives-leah-m-brew/
textbookfull.com
https://textbookfull.com/product/sukhoi-su-25-frogfoot-1st-edition-
alexander-mladenov-2/
textbookfull.com
https://textbookfull.com/product/flip-your-life-how-to-find-
opportunity-in-distress-in-real-estate-business-and-life-1st-edition-
tarek-el-moussa/
textbookfull.com
https://textbookfull.com/product/mechanobiology-handbook-second-
edition-ebong/
textbookfull.com
Hard Rock Hydraulics: An Introduction to Modeling 1st
Edition Fernando Olavo Franciss
https://textbookfull.com/product/hard-rock-hydraulics-an-introduction-
to-modeling-1st-edition-fernando-olavo-franciss/
textbookfull.com
BIRMINGHAM—MUMBAI
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 author, 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.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-80461-274-3
www.packtpub.com
Contributors
Before that, she worked in several fields, starting out with research in particle
physics, during which she worked at CERN on uncovering Higgs boson properties.
She received her PhD in 2014 from the Laboratoire de l’Accélérateur Linéaire
(Orsay, France). Continuing her career in industry, she worked in real estate,
mobility, and logistics for almost 10 years. In the Neo4j community, she is known
as the creator of neomap, a map visualization application for data stored in Neo4j.
She also regularly gives talks at conferences such as NODES and PyCon. Her
domain expertise and deep insight into the perspective of a beginner’s needs make
her an excellent teacher.
There is only one name on the cover, but a book is not the work of one
person. I would like to thank everyone involved in making this book a reality.
Beyond everyone at Packt, the reviewers did an incredible job of suggesting
some very relevant improvements. Thank you, all!
I hope this book will inspire you as much as other books of this genre have
inspired me.
Sean William Grant is a product and analytics professional with over 20 years of
experience in technology and data analysis. His experience ranges from geospatial
intelligence with the United States Marine Corps, product management within the
aviation and autonomy space, to implementing advanced analytics and data
science within organizations. He is a graph data science and network analytics
enthusiast who frequently gives presentations and workshops on connected data.
He has also been a technical advisor to several early-stage start-ups. Sean is
passionate about data and technology, and how it can elevate our understanding
of ourselves.
Jose Ernesto Echeverria has worked with all kinds of databases, from relational
databases in the 1990s to non-SQL databases in the 2010s. He considers graph
databases to be the best fit for solving real-world problems, given their strong
capability for modeling and adaptability to change. As a polyglot programmer, he
has used languages such as Java, Ruby, and R and tools such as Jupyter with
Neo4j in order to solve data management problems for multinational corporations.
A long-time advocate of data science, he expects this long-awaited book to cover
the proper techniques and approach the intersections of this discipline, as well as
help readers to discover the possibilities of graph databases. When not working,
he enjoys spending time with friends and family.
Table of Contents
Preface
Part 1 – Creating Graph Data in Neo4j
Technical requirements
Importing CSV data into Neo4j with Cypher
Discovering the Netflix dataset
Defining the graph schema
Importing data
Introducing the APOC library to deal with JSON
data
Browsing the dataset
Getting to know and installing the APOC plugin
Loading data
Dealing with temporal data
Discovering the Wikidata public knowledge
graph
Data format
Query language – SPARQL
Enriching our graph with Wikidata information
Loading data into Neo4j for one person
Importing data for all people
Dealing with spatial data in Neo4j
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
Importing data in the cloud
Summary
Further reading
Exercises
Part 2 – Exploring and Characterizing Graph Data
with Neo4j
Technical requirements
Digging into the Neo4j GDS library
GDS content
Installing the GDS library with Neo4j Desktop
GDS project workflow
Projecting a graph for use by GDS
Native projections
Cypher projections
Computing a node’s degree with GDS
stream mode
The YIELD keyword
write mode
mutate mode
Algorithm configuration
Other centrality metrics
Understanding a graph’s structure by looking for
communities
Number of components
Modularity and the Louvain algorithm
Summary
Further reading
8
Building a GDS Pipeline for Node Classification Model
Training
Technical requirements
The GDS pipelines
What is a pipeline?
Building and training a pipeline
Creating the pipeline and choosing the features
Setting the pipeline configuration
Training the pipeline
Making predictions
Computing the confusion matrix
Using embedding features
Choosing the graph embedding algorithm to use
Training using Node2Vec
Training using GraphSAGE
Summary
Further reading
Exercise
10
Index
Among the different tools on the market to work with graphs, Neo4j, a graph
database, is popular among developers for its ability to build simple and evolving
data models and query data easily with Cypher. For a few years now, it has also
stood out as a leader in graph analytics, especially since the release of the first
version of its GDS library, allowing you to run graph algorithms from data stored in
Neo4j, even at a large scale.
This book is designed to guide you through the field of GDS, always using Neo4j
and its GDS library as the main tool. By the end of this book, you will be able to
run your own GDS model on a graph dataset you created. By the end of the book,
you will even be able to pass the Neo4j Data Science certification to prove your
new skills to the world.
Who this book is for
This book is for people who are curious about graphs and how this data structure
can be useful in data science. It can serve both data scientists who are learning
about graphs and Neo4j developers who want to get into data science.
The book assumes minimal data science knowledge (classification, training sets,
confusion matrices) and some experience with Python and its related data science
toolkit (pandas, matplotlib, and scikit-learn).
What this book covers
Chapter 1, Introducing and Installing Neo4j, introduces the basic principles of
graph databases and gives instructions on how to set up Neo4j locally, create your
first graph, and write your first Cypher queries.
Chapter 2, Using Existing Data to Build a Knowledge Graph, guides you through
loading data into Neo4j from different formats (CSV, JSON, and an HTTP API). This
is where you will build the dataset that will be used throughout this book.
Chapter 5, Visualizing Graph Data, delves into graph data visualization by drawing
nodes and edges, starting from static representations and moving on to dynamic
ones.
Chapter 6, Building a Machine Learning Model with Graph Features, talks about
machine learning model training using scikit-learn. This is where we will first use
the GDS Python client.
Chapter 9, Predicting Future Edges, gives a short introduction to the topic of link
prediction, a graph-specific machine learning task.
Chapter 10, Writing Your Custom Graph Algorithms with the Pregel API in Java,
covers the exciting topic of building an extension for the GDS plugin.
For the very last chapter, a Java JDK will also be required. The code was tested
with OpenJDK 11.
You will also need to install Neo4j plugins: APOC and GDS. Installation instructions
for Neo4j Desktop are given in the relevant chapters. However, if you are not using
a local Neo4j instance, please refer to the following pages for installation
instructions, especially regarding version compatibilities:
APOC: https://neo4j.com/docs/apoc/current/installation/
GDS: https://neo4j.com/docs/graph-data-science/current/installation/
If you are using the digital version of this book, we advise you to type
the code yourself or access the code from the book’s GitHub repository
(a link is available in the next section). Doing so will help you avoid any
potential errors related to the copying and pasting of code.
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.
Code in text: Indicates code words in text, database table names, folder names,
filenames, file extensions, pathnames, dummy URLs, user input, and Twitter
handles. Here is an example: “Mount the downloaded WebStorm-10*.dmg disk
image file as another disk in your system.”
CREATE (:Movie {
id: line.show_id,
title: line.title,
releaseYear: line.release_year
}
When we wish to draw your attention to a particular part of a code block, the
relevant lines or items are set in bold:
SACRIFICE.
The paper dropped from the hand of the miserable wife. Vincent
sprang to her side. “It is not true!” he exclaimed passionately; “it is
all nonsense and lies!—it is!—oh, say that it is!”
“Leave me, Vincent! leave me!” gasped Clemence; with an
imploring gesture she motioned to the door, and, as soon as her
command had been obeyed, threw herself down upon the floor and
writhed, as if in convulsions of bodily pain! What physical torture
could have equalled the agony of that hour! The anguish caused to a
loving and conscientious spirit by the errors of the being most
beloved, resembles in nature, and is scarcely exceeded in intensity
by that of remorse! To Clemence, her husband’s disgrace was her
disgrace; his transgressions seemed even as her own. So closely was
she joined to him in heart, that the consciousness of personal
blamelessness brought her no comfort—the shadow which had fallen
on him enveloped her also in its blackness!
“What am I called upon to endure!” was a thought ere long
superseded by another: “What am I called upon to do?” A gulf of
misery was yawning before the bankrupt’s wife—could no personal
sacrifice close it? Clemence started to her feet, took the writing
materials which lay on the table, and hastily penned to Mr. Mark a
scarcely legible note, praying him to come to her as soon as was
possible, as she needed his assistance and advice. This done, and
the letter despatched, Clemence could breathe a little more freely.
She declined seeing any one until after his arrival, and as that was
delayed for several hours, the unhappy wife had time to become
more calm, and to revolve in her mind what course of duty lay
before her. Yet the sound of the long waited-for knock at the door
which announced the man of business, was to her much as that of
the hammer-stroke on a scaffold might be to one doomed to suffer
thereon.
Mr. Mark entered with apologies for delay, of which Clemence
understood not one word. With tremulous hand she pointed to the
Times, and could scarcely articulate, “You have seen it?”
Mr. Mark gravely inclined his head.
“And is there any—” Clemence stopped short—she could not
endure to put the question in such a form. “Is it not all cruel
calumny?” she faltered.
Mr. Mark hesitated. “The language is harsh and strong,” was his
guarded reply: it was too well comprehended by the miserable
Clemence.
“When that—that money was settled,” she stammered forth,
without daring to look at her listener, “the house was safe, secure—
there was no prospect of the ruin that followed?”
“I believed so when I followed Mr. Effingham’s directions. I, for
one, had not the slightest doubt at that time of the solvency of the
firm.”
“And he—”
There was a long, painful silence; Clemence heard nothing but the
throbbing of her own heart. When the lady spoke again her tone
was strangely altered; there was in it no more of tremulous
earnestness, but the calm resolution of despair.
“Mr. Mark, let me ask one more question. Is that money entirely at
my own disposal?”
“It is so by the terms of the settlement.”
“Then I request you, acting in my name, to place the whole of it in
the hands of the creditors.”
“My dear madam—”
“My resolution is quite fixed,” said Clemence, compressing her
bloodless lips.
“But consider your position, that of the family—”
“I have resources of my own,” replied Clemence firmly; “and my
step-daughters are already provided for.”
“You have resources?” repeated the lawyer doubtfully; “and the
boy?”
“Shares whatever I have,” answered Clemence.
“Perhaps a partial sacrifice,” began Mr. Mark, but the lady
interrupted him.
“All—all—I will give up all!”
“Not without reflection, dear madam, not on the impulse of the
moment, not without consulting your friends.”
“I consult you, the friend and adviser of my husband. Would not
the act be a just one?”
“Just, perhaps, but—” and he paused.
“I have also consulted another friend, one who has been to me as
a father—the Reverend Mr. Gray of Stoneby.”
“And he advises this step?”
“I have not yet had time to receive his reply.”
“Wait for it then,” said the lawyer; “do nothing without beforehand
weighing the consequences, or it is possible that you may regret
even the noble and generous act, the thought of which does you
honour.”
After some further conversation, it was settled that Clemence
should delay her decision until Mr. Gray’s letter should be received,
and then convey her final decision in writing to the man of business.
Mr. Mark left her with a mingled sentiment of compassion and
respect, which softened his usually abrupt manner to that of almost
paternal tenderness.
“She has much to suffer, but she will suffer bravely,” thought he,
as he stepped into his brougham.
Clemence heaved a deep sigh when she found herself left alone.
The spirit which had supported her through that painful interview
now seemed to fail her. Very repugnant was it to her feelings to
consult any one before her husband, on a point which concerned his
honour so nearly. Could she not learn his will ere making so
momentous a decision? To do so was the instinct of her heart, but
not the judgment of her reason. No; even had she the means of
communicating with Mr. Effingham, how could she seek guidance
from him on the very path from which he had wandered? how ask
him if it were her duty to counteract his own schemes, and clear, as
far as possible, his character from a stain which he had deliberately
contracted? It was, perhaps, better that a cloud of doubt should rest
on what Mr. Effingham’s ultimate wishes might be, and that
Clemence should not behold in actual opposition her obedience to
her husband and her duty to her God.
CHAPTER XX
DECISION.
The man’s face at once lost its smiling expression. “We sometimes
exchange jewels,” said he coldly “but never make purchases in that
way.” Like a fluttered bird, Clemence made her escape out of the
shop.
Must she try another? Yes, that one on the opposite side of the
street. So engaged in her own thoughts was Mrs. Effingham, so
abstracted from all that was passing around her, that as she crossed
the road she narrowly escaped being thrown down by a passing
vehicle. Once more summoning all her resolution, she entered the
shop. Here she was at least attended to without delay. A tall, hard-
visaged man in spectacles, was ready to receive the lady’s
commands. Clemence did not seat herself, but resting her trembling
hand on the counter, told her errand, and produced her jewels. The
man opened the case, and examined one article after the other, as if
mentally calculating its value. That precious guard-ring, first gift of
affection; that chain which loved hands had placed round her neck;
the diamond brooch selected by her husband; the watch, by which
she had counted so many blissful hours,—it seemed to Clemence
almost like desecration to see them in the hand of a stranger! It was
really a relief to her that a sum so much below their actual value
was offered by the jeweller, that she could, without self-reproach,
bear her treasures away from the place.
And yet they must—they must be sold! She must not return to her
home without success! A third time the drooping, heart-sick
Clemence crossed the threshold of a shop, where everything spoke
of luxury and wealth. This visit was the most trying of all! The
dapper little tradesman behind the counter eyed with a quick and
penetrating glance, not only the jewels, but their owner. Clemence
read in his curious look, “How came you possessed of such things as
these?” The bare idea of suspicion covered the pallid countenance of
the youthful lady with a burning glow. It seemed to her as if the first
words from the tradesman might be a question as to her own right
to the property of which she wished to dispose. He spoke, but to
Clemence’s relief it was only to mention terms of purchase.
Clemence, who had been tried almost beyond what she could bear,
hastily closed with his offer, and again had to encounter that curious,
scrutinizing look. Glad, most glad was she to leave the shop and the
street, with their bustle and grandeur, far behind her, though the
sum which she bore with her as the price of her jewels was less than
one-third of what they had originally cost!
“But is the sacrifice sufficient?” Such was the question which
Clemence asked herself as, almost sinking from fatigue, she at
length regained the well-known precincts of Belgrave Square, and
wearily remounted the steps of her magnificent mansion. “Is the
sacrifice sufficient?” she repeated, as, hastily throwing off the cloak,
whose weight even in that wintry day oppressed her, she sank on
the sofa in her own apartment. Could she on so trifling a sum travel
to Cornwall, and support Vincent and herself until she could draw
her interest in June? It was barely possible that, by the severest
economy, she might procure the necessaries of life, but Vincent’s
schooling, small as would be its expense—it would be idle to think of
that! And was he, of whose talents and progress his father had been
so proud, to lose by months of idleness all that he had gained during
years of application? Clemence opened her desk, and drew from it
her most precious possession—the miniature of her husband. Its
diamond setting was even as the admiration and praise of the world
which had once gathered around the original of that portrait, whom
the same world now scorned and condemned. Would the picture be
less precious without it, to her who valued every feature in the
likeness beyond all the jewels in Peru? And yet fast fell the tears of
the unhappy wife, as she removed from its sparkling encirclement
the ivory from which her husband’s eyes seemed to be looking upon
her, calm and bright, as in the first happy days of their love! Could
such a countenance deceive? Could dishonour ever sit on such a
brow? Fervently Clemence pressed to her lips again and again the
lifeless miniature, divested of outward adornment, but to its
possessor even dearer than ever. Dearer, because there was nothing
now but itself to give it value; dearer, because by man it would now
be regarded as a worthless thing!—was it not an emblem of the
beloved one whose image it bore?