Once our model is ready and produces predictions, we can save them back into Neo4j for future use (see Chapter 11, Using Neo4j in Your Web Application). Regarding the link prediction problem, we will save a new relationship type, FUTURE_LINK, for each pair of nodes we identify as more likely to be connected in the future.
Let's start by writing the parametrized Cypher query that will perform this operation:
cypher = """
MATCH (u:Node {id: $u_id})
MATCH (v:Node {id: $v_id})
CREATE (u)-[:FUTURE_LINK {score: $score}]->(v)
"""
We merge all the required information into a single DataFrame:
df_test = df.loc[X_test.index]
df_test["score"] = pred
We can then iterate over this data and create one relationship per row having label=True:
with driver.session() as session:
for t in df_test.itertuples():
if t.label:
session.run(cypher, parameters={
"u_id": t.u_id,
...