← All posts
01 Feb 2020Matías Aguirre

Getting Started with Machine Learning and Neo4j

Learn how to integrate Python-based machine learning workflows with Neo4j's graph database for smarter insights.

machine learningneo4jpythongraph database
Graph with machine learning
Graph with machine learning

Introduction

Neo4j, a popular graph database, offers unique advantages when working with connected data. When we combine these graph structures with the power of Python’s machine learning libraries like Scikit-learn, we can gain deeper insights and build smarter models.

In this article, you’ll learn how to get started with a machine learning workflow that pulls data from Neo4j, processes it in Python, and makes predictions or classifications using popular ML models.

Step 1: Retrieving data from Neo4j

Use the official neo4j Python driver to connect to your Neo4j database and retrieve data. For example, we can run a query to fetch nodes and their properties:

from neo4j import GraphDatabase

driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))

def get_data(tx):
    query = "MATCH (p:Person)-[:HAS_RELATION]->(a:Activity) RETURN p.name AS name, a.type AS activity"
    return list(tx.run(query))

with driver.session() as session:
    results = session.read_transaction(get_data)

Step 2: Preparing the data

Once the data is available, it must be transformed into a suitable format for Scikit-learn. This often means converting categorical variables to numeric via one-hot encoding and filling or removing missing values.

import pandas as pd
df = pd.DataFrame(results)
df = pd.get_dummies(df)

Step 3: Training the ML model

With the prepared data, we can train a model. For example, using a Random Forest Classifier:

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

X = df.drop("activity_sport", axis=1)
y = df["activity_sport"]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

clf = RandomForestClassifier()
clf.fit(X_train, y_train)

Step 4: Interpreting the results

Once the model is trained, we can evaluate its performance with a classification report or a ROC curve. These insights help refine the model or better understand the data.

Conclusion

Combining Neo4j and Python’s machine learning tools offers powerful opportunities to leverage connected data for prediction and analysis. By transforming graph structures into features, you can use traditional ML models to extract valuable insights.

Want to dive deeper? Check out our other articles on graph ML and embedding techniques with Neo4j.

Share
Next step

Ready to Start Your Project?

Let's discuss how technology can transform your organization. We offer technical consultations and custom solutions tailored to institutional requirements. Schedule a complimentary strategic session.

Get in touch
Free consultationNo commitmentFast response