{"id":13610896,"url":"https://github.com/eliorc/node2vec","last_synced_at":"2025-05-14T16:13:27.355Z","repository":{"id":40656905,"uuid":"113577479","full_name":"eliorc/node2vec","owner":"eliorc","description":"Implementation of the node2vec algorithm.","archived":false,"fork":false,"pushed_at":"2024-08-02T11:14:24.000Z","size":91,"stargazers_count":1263,"open_issues_count":0,"forks_count":252,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-04-03T02:08:18.172Z","etag":null,"topics":["deep-learning","embeddings","machine-learning-algorithms"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eliorc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-12-08T13:30:06.000Z","updated_at":"2025-04-01T08:15:14.000Z","dependencies_parsed_at":"2022-08-02T20:31:03.527Z","dependency_job_id":"4e4d4c68-d6c4-4a6b-b063-3c97a7e619e8","html_url":"https://github.com/eliorc/node2vec","commit_stats":{"total_commits":72,"total_committers":16,"mean_commits":4.5,"dds":"0.41666666666666663","last_synced_commit":"872af473836786688707157d10ad2b7db3c6423d"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eliorc%2Fnode2vec","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eliorc%2Fnode2vec/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eliorc%2Fnode2vec/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eliorc%2Fnode2vec/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eliorc","download_url":"https://codeload.github.com/eliorc/node2vec/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248161242,"owners_count":21057552,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deep-learning","embeddings","machine-learning-algorithms"],"created_at":"2024-08-01T19:01:49.146Z","updated_at":"2025-04-10T04:48:33.274Z","avatar_url":"https://github.com/eliorc.png","language":"Python","readme":"# Node2Vec\n[![Downloads](http://pepy.tech/badge/node2vec)](http://pepy.tech/project/node2vec)\n\nPython3 implementation of the node2vec algorithm Aditya Grover, Jure Leskovec and Vid Kocijan.\n[node2vec: Scalable Feature Learning for Networks. A. Grover, J. Leskovec. ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD), 2016.](https://snap.stanford.edu/node2vec/)\n\n# Maintenance\n\n### I no longer have time to maintain this, if someone wants to pick the baton let me know\n\n## Installation\n\n`pip install node2vec`\n\n## Usage\n```python\nimport networkx as nx\nfrom node2vec import Node2Vec\n\n# Create a graph\ngraph = nx.fast_gnp_random_graph(n=100, p=0.5)\n\n# Precompute probabilities and generate walks - **ON WINDOWS ONLY WORKS WITH workers=1**\nnode2vec = Node2Vec(graph, dimensions=64, walk_length=30, num_walks=200, workers=4)  # Use temp_folder for big graphs\n\n# Embed nodes\nmodel = node2vec.fit(window=10, min_count=1, batch_words=4)  # Any keywords acceptable by gensim.Word2Vec can be passed, `dimensions` and `workers` are automatically passed (from the Node2Vec constructor)\n\n# Look for most similar nodes\nmodel.wv.most_similar('2')  # Output node names are always strings\n\n# Save embeddings for later use\nmodel.wv.save_word2vec_format(EMBEDDING_FILENAME)\n\n# Save model for later use\nmodel.save(EMBEDDING_MODEL_FILENAME)\n\n# Embed edges using Hadamard method\nfrom node2vec.edges import HadamardEmbedder\n\nedges_embs = HadamardEmbedder(keyed_vectors=model.wv)\n\n# Look for embeddings on the fly - here we pass normal tuples\nedges_embs[('1', '2')]\n''' OUTPUT\narray([ 5.75068220e-03, -1.10937878e-02,  3.76693785e-01,  2.69105062e-02,\n       ... ... ....\n       ..................................................................],\n      dtype=float32)\n'''\n\n# Get all edges in a separate KeyedVectors instance - use with caution could be huge for big networks\nedges_kv = edges_embs.as_keyed_vectors()\n\n# Look for most similar edges - this time tuples must be sorted and as str\nedges_kv.most_similar(str(('1', '2')))\n\n# Save embeddings for later use\nedges_kv.save_word2vec_format(EDGES_EMBEDDING_FILENAME)\n\n```\n\n### Parameters\n\n#### `node2vec.Node2vec`\n\n- `Node2Vec` constructor:\n    1. `graph`: The first positional argument has to be a networkx graph. Node names must be all integers or all strings. On the output model they will always be strings.\n    2. `dimensions`: Embedding dimensions (default: 128)\n    3. `walk_length`: Number of nodes in each walk (default: 80)\n    4. `num_walks`: Number of walks per node (default: 10)\n    5. `p`: Return hyper parameter (default: 1)\n    6. `q`: Input parameter (default: 1)\n    7. `weight_key`: On weighted graphs, this is the key for the weight attribute (default: 'weight')\n    8. `workers`: Number of workers for parallel execution (default: 1)\n    9. `sampling_strategy`: Node specific sampling strategies, supports setting node specific 'q', 'p', 'num_walks' and 'walk_length'.\n        Use these keys exactly. If not set, will use the global ones which were passed on the object initialization`\n    10. `quiet`: Boolean controlling the verbosity. (default: False)\n    11. `temp_folder`: String path pointing to folder to save a shared memory copy of the graph - Supply when working on graphs that are too big to fit in memory during algorithm execution.\n    12. `seed`: Seed for the random number generator (default: None). Deterministic results can be obtained if seed is set and `workers=1`.\n\n- `Node2Vec.fit` method:\n    Accepts any key word argument acceptable by gensim.Word2Vec\n\n#### `node2vec.EdgeEmbedder`\n\n`EdgeEmbedder` is an abstract class which all the concrete edge embeddings class inherit from.\nThe classes are `AverageEmbedder`, `HadamardEmbedder`, `WeightedL1Embedder` and `WeightedL2Embedder` which their practical definition could be found in the [paper](https://arxiv.org/pdf/1607.00653.pdf) on table 1\nNotice that edge embeddings are defined for any pair of nodes, connected or not and even node with itself.\n\n- Constructor:\n    1. `keyed_vectors`: A gensim.models.KeyedVectors instance containing the node embeddings\n    2. `quiet`: Boolean controlling the verbosity. (default: False)\n\n- `EdgeEmbedder.__getitem__(item)` method, better known as `EdgeEmbedder[item]`:\n    1. `item` - A tuple consisting of 2 nodes from the `keyed_vectors` passed in the constructor. Will return the embedding of the edge.\n\n- `EdgeEmbedder.as_keyed_vectors` method: Returns a `gensim.models.KeyedVectors` instance with all possible node pairs in a *sorted* manner as string.\n  For example, for nodes ['1', '2', '3'] we will have as keys \"('1', '1')\", \"('1', '2')\", \"('1', '3')\", \"('2', '2')\", \"('2', '3')\" and \"('3', '3')\".\n\n## Caveats\n- Node names in the input graph must be all strings, or all ints\n- Parallel execution not working on Windows (`joblib` known issue). To run non-parallel on Windows pass `workers=1` on the `Node2Vec`'s constructor\n\n## TODO\n- [x] Parallel implementation for walk generation\n- [ ] Parallel implementation for probability precomputation\n","funding_links":[],"categories":["图数据处理","Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feliorc%2Fnode2vec","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feliorc%2Fnode2vec","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feliorc%2Fnode2vec/lists"}