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.5.7-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 20e97ad87b2e9731f11133a008dfb2f4752b9adda7e29be811dd793b294b651e |
|
MD5 | a0b96ca8cbe512cadea964b63fd18fc7 |
|
BLAKE2b-256 | cc653cfd50a9976357455b6bf2cacadf520309df915442d019fe1837d5fc3c87 |
Hashes for cxrandomwalk-0.5.7-cp312-cp312-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a068ab2a1de1a841721f7dd19f3c3b32369f4011683f77ddc2add4787e48c06 |
|
MD5 | 2ae67a1b9f49cecca0496850d0e1097f |
|
BLAKE2b-256 | e2cb01d5267d6dc5e7641e867f6c3983c4cba62948fbf7f771977f4a7fb3ac28 |
Hashes for cxrandomwalk-0.5.7-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc6e754e24341b783ae8ecc7f1b238ee28b2d54b1c8501c9eba93517bbb268c9 |
|
MD5 | fad020321061769d3985a691316259f7 |
|
BLAKE2b-256 | 488151c3bd34dc6e462554e35c71c72c659327dc273c60062d69fbdeb7226c00 |
Hashes for cxrandomwalk-0.5.7-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b80e9989e040d4034e7fdd7d0f07ae58eddaa85de086ec37c1c4896cf249a3b |
|
MD5 | 90656303c3a77c98f1ec6180b8b7e036 |
|
BLAKE2b-256 | 5ff1e41c15321bf8dae67765e78b5aedfcedde2d95cbf554739a68c568ecb48a |
Hashes for cxrandomwalk-0.5.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ec9dbdbfbc87afc1ed435b88fd9dc3347ec1e131351f2fa9d3d408ad900cc13 |
|
MD5 | adc5a182e4e322ca93c011901f2dc010 |
|
BLAKE2b-256 | 85d8119cdcea00d3e0d93b4b48fbbb5dbe368a8b71fd19bdd80daf6de2f91e66 |
Hashes for cxrandomwalk-0.5.7-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8402f2e7dd1e860a84965d82c6bca54377b8ca9624e6ad03f19e39ff86f69417 |
|
MD5 | 68a0a02b9905ac8a33a675b00f761042 |
|
BLAKE2b-256 | b3b92578a2b92bcc942267b95ca04e609a0d6a413fd5c67cdcca45451ebeb1c1 |
Hashes for cxrandomwalk-0.5.7-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 103d83e152bbf674776ac7c9c1c884726c2ff620c435de28e03cb77e1ddfe74d |
|
MD5 | 87320057d6531ffe1824bcf4b229c5ec |
|
BLAKE2b-256 | c09173229864f073b13dc98bd2c71727235f4cb43ac7b840e264efac8ba6728f |
Hashes for cxrandomwalk-0.5.7-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9d4cb54bdacc9d28aebecd66aa1285571bd0c7c7f55fdb00a7e43c801b558b7 |
|
MD5 | e4942dffaf343e141ca08d011dd9ac7f |
|
BLAKE2b-256 | 13ba62e7b72050e1cba6b4fdca8551772ae83bcb132dc4b2ad0eb44175196192 |
Hashes for cxrandomwalk-0.5.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d3bcc89fa479b64acee3f2954f373c7833a3c6fbb17db1c09769e9120676438 |
|
MD5 | f988046773608ad559dba7c0d1f2df18 |
|
BLAKE2b-256 | 98be6c383c4790e3800b61fdc716a370269557627068bd74447ced33c5505622 |
Hashes for cxrandomwalk-0.5.7-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 417ed23229cee23848be5496359e92ce642a3e81688cef87cc14841cb96cb91a |
|
MD5 | de886e1962f21d1e3b37c58b27895160 |
|
BLAKE2b-256 | 05fd1326b1c2983a100162e4b5ddacf54513dbc93fb3477fdc6a8ba70581a5f8 |
Hashes for cxrandomwalk-0.5.7-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 46cef36bcd2f627176c921a3d9e2afcc2b41023c1e781b56ff2acc7c4531c67e |
|
MD5 | 0d551206a5f76f5631f48aedfb7fd39f |
|
BLAKE2b-256 | c89f97d7ec7e0df8569a3b520884dbc13de49016c4c033adfbd69a3a44293c24 |
Hashes for cxrandomwalk-0.5.7-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8cc6ca0a5a36a8484ad3e1b2e9255eee86e8f767f665bebf6c486060bd0108ec |
|
MD5 | 7414efb387b9d51d0482853f5b779b09 |
|
BLAKE2b-256 | 481d348fbe92a6c8235b544761b70d4691b48ff2fd7dd3e6e5d0baff266ccc46 |
Hashes for cxrandomwalk-0.5.7-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 63f111cc18c670c55c920dfb3d866d08b967543b397407b2c623c2f9964764d1 |
|
MD5 | 517e9c008c8742bd520d373549ab87f2 |
|
BLAKE2b-256 | 17756c139349c452bc802d13639478966d16d6a37b2cfcdfa97781c6550ad225 |
Hashes for cxrandomwalk-0.5.7-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f630a783e52765507aa267f2c5b2af95a614001aa403a8aea89386e90a9e9cc |
|
MD5 | 0d679f4a570dd9d526107336336174d5 |
|
BLAKE2b-256 | ce3a8a4b55b49c9f8d1e1e43721b0bb757982a07c00c142fbad3c40ae5b8fa19 |
Hashes for cxrandomwalk-0.5.7-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c51051dbb889a8d47f928ad111ea3414ab63d462a1f03c54eb4bdaaeed7c9996 |
|
MD5 | 7bda1703204bad65ce8d2dd931210b7a |
|
BLAKE2b-256 | b438ace03286fbf1f31ec5760fb0640d24f9ea031372d7e8802140fc1732e816 |