Package: graphdatascience
Neo4j Graph Data Science Clientgraphdatascienceis a Python client for operating and working with theNeo4j Graph Data Science (GDS) library.
It enables users to write pure Python code to project graphs, run algorithms, as well as define and use machine learning pipelines in GDS.The API is designed to mimic the GDS Cypher procedure API in Python code.
It abstracts the necessary operations of theNeo4j Python driverto offer a simpler surface.
Additionally, the client-specific graph, model, and pipeline objects offer convenient functions that heavily reduce the need to use Cypher to access and operate these GDS resources.graphdatascienceis only guaranteed to work with GDS versions 2.0+.Please leave any feedback as issues onthe source repository.
Happy coding!InstallationTo install the latest deployed version ofgraphdatascience, simply run:pipinstallgraphdatascienceGetting startedTo use the GDS Python Client, we need to instantiate a GraphDataScience object.
Then, we can project graphs, create pipelines, train models, and run algorithms.fromgraphdatascienceimportGraphDataScience# Configure the driver with AuraDS-recommended settingsgds=GraphDataScience("neo4j+s://my-aura-ds.databases.neo4j.io:7687",auth=("neo4j","my-password"),aura_ds=True)# Import the Cora common dataset to GDSG=gds.graph.load_cora()assertG.node_count()==2708# Run PageRank in mutate mode on Gpagerank_result=gds.pageRank.mutate(G,tolerance=0.5,mutateProperty="pagerank")assertpagerank_result["nodePropertiesWritten"]==G.node_count()# Create a Node Classification pipelinepipeline=gds.nc_pipe("myPipe")assertpipeline.type()=="Node classification training pipeline"# Add a Degree Centrality feature to the pipelinepipeline.addNodeProperty("degree",mutateProperty="rank")pipeline.selectFeatures("rank")features=pipeline.feature_properties()assertlen(features)==1assertfeatures[0]["feature"]=="rank"# Add a training methodpipeline.addLogisticRegression(penalty=(0.1,2))# Train a model on Gmodel,train_result=pipeline.train(G,modelName="myModel",targetProperty="myClass",metrics=["ACCURACY"])assertmodel.metrics()["ACCURACY"]["test"]>0asserttrain_result["trainMillis"]>=0# Compute predictions in stream modepredictions=model.predict_stream(G)assertlen(predictions)==G.node_count()The example here assumes using an AuraDS instance.
For additional examples and extensive documentation of all capabilities, please refer to theGDS Python Client Manual.Full end-to-end examples in Jupyter ready-to-run notebooks can be found in theexamplessource directory:Machine learning pipelines: Node classificationNode Regression with Subgraph and Graph Sample projectionsProduct recommendations with kNN based on FastRP embeddingsSampling, Export and Integration with PyG exampleLoad data to a projected graph via graph constructionHeterogeneous Node Classification with HashGNN and AutotuningPerform inference using pre-trained KGE modelsDocumentationThe primary source for learning everything about the GDS Python Client is the manual, hosted athttps://neo4j.com/docs/graph-data-science-client/current/.
The manual is versioned to cover all GDS Python Client versions, so make sure to use the correct version to get the correct information.Known limitationsOperations known to not yet work withgraphdatascience:Numeric utility functions(will never be supported)Cypher on GDS(might be supported in the future)Projecting graphs using Cypher Aggregation(might be supported in the future)Licensegraphdatascienceis licensed under the Apache Software License version 2.0.
All content is copyright © Neo4j Sweden AB.AcknowledgementsThis work has been inspired by the great work done in the following libraries:pygdsby stellasiagds-pythonby moxious