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.4.0-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58a2738fcc7d23ecc5f61b4e9bae8ab089b1895a71a0e167c2ad9d2b73333a06 |
|
MD5 | 3826d2a44e55abafdd48dbb12c1890ea |
|
BLAKE2b-256 | ca99e78abe2d0a9f7697c86826d4af9461f31952c54a561a411d5b012c14e6ec |
Hashes for cxrandomwalk-0.4.0-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50b9322c0244980e058585ab7c11fa9e4c72a9424e1f355b9624c05c217e6876 |
|
MD5 | 8b93328d5a8d5f1c977c177740094b68 |
|
BLAKE2b-256 | f22a29cb19f467501d48b26e101dfb8b2e4d41a989e8a90ed4c3db69442eb309 |
Hashes for cxrandomwalk-0.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 694f7d5c7ab423ab0cac92252608c6c8a4aabbf59759b6f484c2ce8155e4f0e7 |
|
MD5 | c9af9f2509ce48f5f046b601e8f09f0e |
|
BLAKE2b-256 | 967e3819b1c534492bdd12af25fe05de2a8262055ee732c32d62b0dc6ccc7116 |
Hashes for cxrandomwalk-0.4.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 69d3a505847a9352ca4a825313ac448909aff2804dc87e93a94dc27708dd48ba |
|
MD5 | 6f52dd24eb563f5dd1f085c9e3b068fc |
|
BLAKE2b-256 | de9f9a041738544c0fa2d892010df44b00e12350dfce18601963db24ad38c2ff |
Hashes for cxrandomwalk-0.4.0-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8eba66119be4d191e02167eaf8c7cc9283c42a4e2b56ffbea26005b051fca8a0 |
|
MD5 | b9d634e0c60ca785fc1b89553c585a1e |
|
BLAKE2b-256 | 1ae4fb4693ed5ba4c7cac845fd52186a565c3fbb57cc59f01bc0bfb7e2acfbea |
Hashes for cxrandomwalk-0.4.0-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b2b6cd80f8757c7d006a5c6a2c8a80d44cb011a94157949f31fcaef193715f5 |
|
MD5 | 2245ef9875018ccb3bfa89d5c80c8cb8 |
|
BLAKE2b-256 | 7bc461ede86fe0daa835b01dec8ba2055d8969c71e3bbcdbf4db98f444dde3c5 |
Hashes for cxrandomwalk-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c54883b9d643378ccd35d45cd489dfdd72005b7bc2f518206f789086559ed79 |
|
MD5 | 4a9dec8985c5f8e9bb25393ce616a379 |
|
BLAKE2b-256 | fbb7e8ec6abc44739ecc9071acf35c323c60a5d90719d67964c1ae6c48756e6d |
Hashes for cxrandomwalk-0.4.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c311df411a67bf89cf2e77ebe3e4eaaf029faacfa3c7a4cde1fec853d379f1e |
|
MD5 | 475e8a0cd98ad9746c37bdafd50eb345 |
|
BLAKE2b-256 | 4117e14ce8194903210ee42a106e285cc7320d10d495dfbcc4a5ebe52e72815d |
Hashes for cxrandomwalk-0.4.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 49ba397b4b8a00af1986b15817f84cae6ce5bf19a54427c2a65a35240cf0d348 |
|
MD5 | bfdc03042a2da1d600e54241c16285a8 |
|
BLAKE2b-256 | 38c00b5c6a7a604108ee08aa0c325e35ecbc399948caea94ec46bc4063ab806b |
Hashes for cxrandomwalk-0.4.0-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 521c6d3a485e18033c570e7a0be79ca4ea2a9cce3f8785107ddd367a2643aa6f |
|
MD5 | a9fa9c45c5f4209e742f63239735d044 |
|
BLAKE2b-256 | 68c3659bf4bb5c425ed7b4e985b94090a7740493144861fbc30014ee337c9550 |
Hashes for cxrandomwalk-0.4.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a1cde4100ef6fdb189a8b055faec77a988a05715ad50b84b5510a2b06d30835 |
|
MD5 | 18eac5708331535d9a24621eb2855454 |
|
BLAKE2b-256 | 2c5849f79cea45334b02b2b28990579dd1225b411cd276d0572673215593bf42 |
Hashes for cxrandomwalk-0.4.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f448e90942e9a0276a373f496a18862cac3177fb06fc0747ac4ded4ec9ae885b |
|
MD5 | 4ea3538914ff5fa2e8dee9bc0a50c4a5 |
|
BLAKE2b-256 | 047965bf5337b8755848832cf39ca398c660edf1d2ec441eb67808d8dfe757c2 |