Library to perform random walks on complex networks.
Project description
CXRandomWalk
Fast library written in C for python to generate sentences based on random walks from networks. Can be used to generate input data for word2vec and other embedding techniques.
Install
Requires python headers and a C11 compatible compiler, such as gcc or clang.
To install it, simply run:
pip install cxrandomwalk
or clone this repository and install it from master by running:
pip install git+git://github.com/filipinascimento/cxrandomwalk.git
Usage
The walking algorithm uses regular random walks and biased random walks according to node2vec ([https://snap.stanford.edu/node2vec/]).
Step 1: Import the libraries
import numpy as np
import cxrandomwalk as rw
Step 2: Convert network to a edgelist and a list of weights (optional)
vertexCount = 10;
edges = np.random.randint(0,vertexCount-1,(vertexCount*2, 2))
weights = np.random.random(size=vertexCount*2);
Step 3: Load the network data in an agent object
agent = rw.Agent(vertexCount,edges,False,weights)
To generate the sequences use:
sequences = agent.generateWalks(p=2,q=3,verbose=True)
The output is a list of lists with the walks.
API Documentation
cxrandomwalk.Agent(vertexCount,edges,directed=False,weights=None)
vertexCount
- number of vertices in the networkedges
- list of edgesdirected
- directed or notweights
- list containing the weights of the edges (use the same order as edges)returns a rw.Agent instance
cxrandomwalk.Agent.generateWalks(p=1.0,q=1.0,windowSize=80,walksPerNode=10,verbose=False,filename=None,callback=None,updateInterval=1000)
p
- for p in node2vec walks (1.0 for unbiased)q
- for q in node2vec walks (1.0 for unbiased)windowSize
- length of each walkwalksPerNode
- number of sentences to be generatedverbose
- enable verbose modefilename
- save sentences to filename instead of returning the sequencescallback
- callback function that runsupdateInterval
- set the interval between iterations so that python interruptions work
returns a list of lists containing the sequences. If filename is provided, instead of returning a list, the method creates the file and fill with the walks, with words separated by spaces and sentences separated by new lines. It can be used directly on gensim.
TODO
- Include other kinds of walks
- Better documentation and CLI help
Example with tqdm callback
import numpy as np
import cxrandomwalk as rw
from tqdm.auto import tqdm
vertexCount = 10000;
edges = np.random.randint(0,vertexCount-1,(vertexCount*2, 2))
weights = np.random.random(size=vertexCount*2);
agent = rw.Agent(vertexCount,edges,False,weights)
def make_pbar():
pbar = None
def inner(current,total):
nonlocal pbar
if(pbar is None):
pbar= tqdm(total=total);
pbar.update(current - pbar.n)
return inner
print(len(agent.generateWalks(q=1.0,p=1.0,verbose=False,updateInterval=1000,callback=make_pbar())))
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for cxrandomwalk-0.3.0-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c788bb9bed5d3c89cf8856e494c3c8fc54dd14e46f2c8d1089baa5aff2c02610 |
|
MD5 | 41d045c9681617de42aac131c5647c10 |
|
BLAKE2b-256 | d57ed78b8fe1b27bf1ec6566e28144cf1289f214cdfbb0127ae4c2050dc06715 |
Hashes for cxrandomwalk-0.3.0-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ebb6dda6eee8a769646c6a7a6b47514e7acd820a90a494758501f379c6bcbbf1 |
|
MD5 | 19d8b8ccc7103ee899fbea0b97da9045 |
|
BLAKE2b-256 | 05f460b6560df5926a71b2252b20b2e8f0e62ba4802be21e53121bc8c68d2834 |
Hashes for cxrandomwalk-0.3.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d42c3707d30a9c333705c4df5bd5496eecf8d8ae1351788d85465cfb22dc3ba |
|
MD5 | f47592b7cfe5b7a617116e84b500cd78 |
|
BLAKE2b-256 | 7c6fd7d2dcd56001a553ce25797e035a474e5bc471e34fb908bdd55cad5114f1 |
Hashes for cxrandomwalk-0.3.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54e4a2d7279b87d17153ad1892d81d789951521829cc5025edc1bd20c1aabf21 |
|
MD5 | 29e63a1a0727ed780be523be9606feb9 |
|
BLAKE2b-256 | 494744040c918ee8e847fbb8a10dc2fa74a778dcdff930196982b99a47d06939 |
Hashes for cxrandomwalk-0.3.0-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cce14a14f45d9553b9dec88d759b2432841a11342a23acd98741607bf50c93ed |
|
MD5 | 3b576d2343b22d4cb016011e5c7b1b1b |
|
BLAKE2b-256 | 496c382026fb775c3a49c39914ae92331a15aede232ca089333888b8df3074f6 |
Hashes for cxrandomwalk-0.3.0-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99705cdccc088e06572e4bd168eea2b5e73fad51ff892ea7820ef166c9f60460 |
|
MD5 | d7ec2e0a2b2946233a399c6fcfffc9e5 |
|
BLAKE2b-256 | 0aea781366cbaae159f6aeda3221fd8ae8186a6e4fd14ac2ef749d732549e3b9 |
Hashes for cxrandomwalk-0.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | db759bda6fbb202064abefbb08dca3cb4c0b15a053d92b6ceb60cfe65daa1961 |
|
MD5 | 4cb8d2918e0cb3d21917ea1929d0d9b5 |
|
BLAKE2b-256 | 847b80fda8f244a1baf0b6b84ab20382ae512edbb2c72775ed338e28604f3f4b |
Hashes for cxrandomwalk-0.3.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 519c375e41fd9cda5bde6bb7829633c9ea9a66cf3dded6fc1e82abcd38ce3a7c |
|
MD5 | a6944e914c225c457340fdbdb5fdd2b8 |
|
BLAKE2b-256 | 4cf94f58f7b8dc7a4d8d5c89a6a3e3314a48d1536e7741e3e70d61be0ee1f6b2 |
Hashes for cxrandomwalk-0.3.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9f65079b55c76cb0f117e90c30ae8de704207dbf30d51d9b4d9de81e6e68adf |
|
MD5 | f4abbf6c04a02cf3fe353c8ac254219d |
|
BLAKE2b-256 | ee8e9eb680315d30dc5624c843ed276754575587a8e16a99fec87c2d26d821c5 |
Hashes for cxrandomwalk-0.3.0-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 798c3745992bb521b71b9b33e8956fea0993cba78ad28065e297bc1d9b1084a8 |
|
MD5 | 4a42250204aba08f5be4515503df20c0 |
|
BLAKE2b-256 | 968eef8f26066cd4b128434a20ded7fc6e6fcd7e285e93eb920ba27d178a3569 |
Hashes for cxrandomwalk-0.3.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dee84ec8ff9cb7d591a9b089556d6124fef4bb909dce99a5b643fa8961991fdb |
|
MD5 | 43dff5a63360fd779ae25aa482d5d30a |
|
BLAKE2b-256 | 11566eaac3036e98ce7545c32f42f6d9af8ebc0b476837a530a675da701bbb80 |
Hashes for cxrandomwalk-0.3.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54ce88f41b2d90a8dc1c7d4fa09d529657beddb3afe90bdb1829ea1dcbf8df78 |
|
MD5 | 550b7e114200943ff1781b0615629439 |
|
BLAKE2b-256 | deec25ad0a29c13c321d34ca2dcf39f9974e3543ed7a7b4f8a822ab4f5ba82a6 |