运行 GDS 算法
学习执行 GDS 库中的各种图算法,包括将图加载到内存中以及配置算法
运行 GDS 算法 是 CoddyKit 上的免费 Neo4j Graph Database Fundamentals 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Neo4j Graph Database Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Neo4j Graph Database Fundamentals 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
GDS Algorithm Workflow
The Graph Data Science (GDS) library in Neo4j helps us run powerful graph algorithms. To use GDS, you generally follow a three-step process:
- Project: Load a subset of your graph into GDS's optimized in-memory store.
- Execute: Run an algorithm on this in-memory graph.
- Write Back: Optionally, write the results back to your Neo4j database.
Projecting Your Graph
Before running an algorithm, GDS needs to know which part of your database graph to analyze. This process is called graph projection.
You define which nodes and relationships, based on their labels and types, should be copied into GDS's fast in-memory representation. This makes algorithm execution much quicker.
Projecting Nodes Only
Let's project a simple graph containing only nodes with the label Person. We give our projected graph a name, like myGraph.
The gds.graph.project procedure creates this in-memory graph.
CALL gds.graph.project(
'myGraph',
'Person',
{}
) YIELD graphName, nodeCount, relationshipCount;Projecting Nodes and Relationships
More commonly, you'll project both nodes and relationships. Here, we project Person nodes and their KNOWS relationships.
We specify the node label and the relationship type. You can also add properties to the projection.
CALL gds.graph.project(
'socialGraph',
'Person',
'KNOWS'
) YIELD graphName, nodeCount, relationshipCount;Executing GDS Algorithms
Once your graph is projected, you can run various algorithms. GDS algorithms often have different execution modes:
- Stream: Returns results directly as a stream of rows. Great for immediate analysis.
- Stats: Returns only summary statistics about the algorithm's run.
- Write: Writes the results back to your Neo4j database as new properties or relationships.
PageRank Stream Example
Let's run the PageRank algorithm on our socialGraph in stream mode. PageRank helps identify influential nodes.
We use gds.pageRank.stream and pass the name of our projected graph.
CALL gds.pageRank.stream('socialGraph')
YIELD nodeId, score
RETURN gds.util.asNode(nodeId).name AS person, score
ORDER BY score DESC
LIMIT 5;Customizing Algorithm Settings
GDS algorithms are highly configurable. You can pass a map of parameters to fine-tune their behavior.
Common parameters include maxIterations, dampingFactor (for PageRank), or concurrency. Always check the GDS documentation for specific algorithm parameters.
PageRank with Custom Settings
Here, we run PageRank with a custom dampingFactor and limit the maxIterations. This gives you more control over the algorithm's execution and convergence.
CALL gds.pageRank.stream(
'socialGraph',
{
maxIterations: 10,
dampingFactor: 0.8
}
)
YIELD nodeId, score
RETURN gds.util.asNode(nodeId).name AS person, score
ORDER BY score DESC
LIMIT 5;Persisting Algorithm Results
Often, you'll want to store the results of an algorithm back into your Neo4j database. This allows you to query them later or use them in further analysis.
The write mode of GDS algorithms adds new properties to nodes or creates new relationships based on the algorithm's output.
Writing PageRank Scores
To write results, use the .write procedure, like gds.pageRank.write. You need to specify the property name where the score will be stored on the original nodes.
After running this, each Person node in your database will have a new property, pageRankScore.
CALL gds.pageRank.write(
'socialGraph',
{
writeProperty: 'pageRankScore'
}
)
YIELD nodeCount, ranIterations, didConverge;GDS Workflow Check
Which of the following are essential steps in the typical GDS algorithm workflow?
Running GDS Algorithms Recap
Great job! You've learned the core steps for running GDS algorithms:
- Projecting your graph into GDS memory using
gds.graph.project. - Executing algorithms in stream, stats, or write modes.
- Configuring algorithms with custom parameters.
- Writing back results to your Neo4j database for persistence.
Next, you'll explore advanced GDS pipelines and integration with machine learning!
常见问题解答
「运行 GDS 算法」课时是免费的吗?
是的 — 「运行 GDS 算法」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Neo4j Graph Database Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Neo4j Graph Database Fundamentals 课程共包含 4 节课。
「运行 GDS 算法」这节课中我会学到什么?
学习执行 GDS 库中的各种图算法,包括将图加载到内存中以及配置算法 你通过在浏览器中直接运行的动手代码来练习 Neo4j Graph Database Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Neo4j Graph Database Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Neo4j Graph Database Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「运行 GDS 算法」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Neo4j Graph Database Fundamentals 课中编写并运行代码吗?
能。每节 Neo4j Graph Database Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- GDS 库简介
- 运行 GDS 算法
- GDS 管道与机器学习
- 使用 GDS 进行图嵌入