How to Model and Deploy Knowledge Graphs with Neo4j and Python
This article explains the fundamentals of knowledge graph representation, including entities, concepts, relationships, and triple structures, and provides step‑by‑step instructions for installing Neo4j, configuring Python with py2neo, and importing CSV‑based triples into a graph database for querying and reasoning.
Knowledge Graph Representation
Recall that a knowledge graph is a graph‑structured representation of real‑world concepts, entities, events and their relationships, serving both as a visual container of knowledge and a platform for semantic understanding and reasoning.
Structurally, a knowledge graph is a semantic network composed of nodes (entities or concepts) and edges (semantic relations). For example, the statement "The current US president is Trump" can be abstracted as the triple (United States, president, Trump).
This basic structure is the common triple representation in knowledge graphs.
Nodes: Entities and Concepts
1. Entities
An entity is a uniquely identifiable object that can be a concrete object (e.g., "Newton", "Beijing", "iPhone 15"), an abstract attribute value (e.g., "July 2023", "20°C", "red"), or a spatial/temporal dimension (e.g., "Middle‑lower Yangtze", "2025 College Entrance Exam"). Each entity usually has a unique ID (URI or hash) for cross‑platform identification.
2. Semantic Classes and Concepts
A semantic class groups entities sharing a common attribute, such as "Country" grouping "China", "USA", etc., or "Person" grouping "Xiang Yu", "Li Bai". Semantic classes provide a taxonomy for classification, clustering, and data completion.
Edges: Modeling Attributes and Relations
Edges link nodes and come in two core types: attribute edges and relation edges .
1. Attribute Edges
Attributes describe a feature of an entity, connecting an entity node to an attribute‑value node. Examples:
(China, capital, Beijing) (iPhone 15, weight, 171g) (Yangtze, length, 6300km)2. Relation Edges
Relations are semantic links between two entities, such as social roles, spatial, or causal relations. Examples:
(Beijing, belongs to, China) (Rain, causes, Flood)Relations emphasize semantic strength and context, crucial for reasoning.
Triples and RDF Representation
1. Triple Definition
The core unit of a knowledge graph is the triple (subject, predicate, object). The subject denotes an entity, the predicate denotes the relationship, and the object can be another entity or an attribute value. This is the RDF (Resource Description Framework) model.
2. Triple Classification
Two main types:
Entity‑Relation triples : (USA, president, Trump) (China, capital, Beijing)
Attribute‑Value triples : (iPhone15, weight, 171g) (Yellow River, source, Qinghai‑Tibet Plateau)
3. Formal Representation
A knowledge graph can be formalized as a set of triples, comprising an entity set, a relation set, and the triple set itself.
Code and Software Practice
Next, we introduce how to work with the graph database Neo4j and import data using Python.
1. Install and Set Up Neo4j
Download Neo4j from https://neo4j.com/download/ , install Neo4j Desktop, start the service, and access the browser at http://localhost:7474 with default credentials neo4j / neo4j (change the password on first login).
2. Python Environment Configuration
Install the py2neo library to interact with Neo4j:
pip install py2neo3. Python Code to Interact with Neo4j
The following script reads triples from multiple CSV files and inserts them into Neo4j, creating start and end nodes and a relationship for each triple. The g.merge method ensures nodes are not duplicated.
import csv
from py2neo import Graph, Node, Relationship
# Connect to Neo4j
g = Graph("http://localhost:7474", auth=("neo4j", "your_password"))
csv_files = ["data1.csv", "data2.csv"]
for csv_file in csv_files:
with open(csv_file, 'r', encoding='utf-8') as f:
reader = csv.reader(f)
next(reader) # skip header
for item in reader:
start_node = Node("Entity", name=item[0])
end_node = Node("Entity", name=item[2])
relation = Relationship(start_node, item[1], end_node)
g.merge(start_node, "Entity", "name")
g.merge(end_node, "Entity", "name")
g.merge(relation, "Entity", "name")
print("All files have been successfully added to Neo4j!")The code uses py2neo to connect to Neo4j, iterate over CSV files, create nodes and relationships, and merge them into the graph.
4. Visualizing the Graph in Neo4j
After insertion, use Neo4j Browser to explore the graph. A basic Cypher query to view the first 25 nodes:
MATCH (n) RETURN n LIMIT 255. Graph Queries and Reasoning
Using Cypher, you can query and infer knowledge. Example to find all knowledge related to "derivative":
MATCH (n:Entity)-[r]->(m:Entity) WHERE n.name = "导数" RETURN n, r, mBy chaining relationships, Neo4j can infer indirect connections, e.g., from (A, father, B) and (B, father, C) derive (A, grandfather, C).
MATCH (a:Entity)-[:父亲]->(b:Entity), (b)-[:父亲]->(c:Entity) RETURN a.name, b.name, c.nameComplex pattern matching can discover higher‑level relationships, such as finding connections between "derivative" and "representation":
MATCH (theorem:Entity)-[:表示]->(application:Entity) WHERE theorem.name CONTAINS "导数" RETURN theorem.name, application.nameThis article covered knowledge‑graph representation, its basic structures, and practical implementation in Neo4j using Python. By importing CSV‑based triples and leveraging Cypher queries, you can efficiently query and reason over graph‑structured knowledge.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Model Perspective
Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
