Building a Recommendation System with Neo4j and Python
Discover how graph structures and ML models in Python can power advanced recommendations in your app.


In this article, you’ll learn how to build a recommendation system using graph databases and machine learning models in Python. Graph-based recommendation systems leverage relationships between entities such as users, products, or content to generate more relevant and context-aware suggestions.
Why Use Neo4j?
Neo4j is a powerful graph database that makes it easy to model and query relationships. Thanks to the natural structure of graphs, you can quickly find users with similar preferences or products that are frequently recommended together.
Getting Started with Python and Neo4j
- Install the required libraries:
pip install neo4j pandas scikit-learn
- Connect to your Neo4j database:
from neo4j import GraphDatabase
driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))
def get_user_item_data(tx):
query = '''
MATCH (u:User)-[r:RATED]->(m:Movie)
RETURN u.userId AS user, m.movieId AS item, r.rating AS rating
'''
return list(tx.run(query))
with driver.session() as session:
data = session.read_transaction(get_user_item_data)
- Train a recommendation model: Once you have the user-item matrix, you can train a simple model like matrix factorization or a nearest-neighbor-based recommender.
Integrating the Results
The recommended items can be written back into Neo4j or used directly in your frontend. For example, you might create relationships like (:User)-[:SHOULD_WATCH]->(:Movie).
Conclusion
Combining Neo4j with machine learning in Python lets you build powerful recommendation systems that leverage contextual and relational information. This opens the door to smarter and more personalized user experiences.