sadrasabouri / pyrandwalk Goto Github PK
View Code? Open in Web Editor NEW:walking:Python Library for Random Walks
License: MIT License
:walking:Python Library for Random Walks
License: MIT License
The current code for simulating a random walk cannot handle data type np.array
as the states vector, given that numpy arrays do not have index
attribute.
With the current code base, the issue can be reproduced using the following:
from pyrandwalk import *
import numpy as np
# Using the same examples given in README for transition matrix
trans = np.array([[1, 0, 0, 0, 0], [0.25, 0, 0.75, 0, 0],[0, 0.25, 0, 0.75, 0], [0, 0, 0.25, 0, 0.75], [0, 0, 0, 1, 0]])
# The only change here is to switch `states` from a List to np.array
states =np.array([0,1,2,3,4])
# This is the check that is run using a utility to ensure the correct data types are used for states
print(isinstance(states, (list, np.ndarray)))
# >>> True
rw = RandomWalk(states, trans)
states, probs = rw.run()
# Error: `AttributeError: 'numpy.ndarray' object has no attribute 'index'`
Linux
Python 3.9.12
1.1
I will try to do a PR later to handle this issue by using np.where
when we have a numpy array.
This bug is occurring because of a bad approach taken for setting the plot title which seems deprecated, now.
So when you use the method like this:
ax = rw.plot_graph()
The graph shows happily.
UNEXPECTED EXCEPTION: AttributeError("'FigureCanvasAgg' object has no attribute 'set_window_title'")
Traceback (most recent call last):
....
AttributeError: 'FigureCanvasAgg' object has no attribute 'set_window_title'
Unbuntu
3.8
pyrandwalk.__version__
)1.1
In some cases, the random walker will reach nodes with no option but to stay in the same state. This situation referred to as the dead ends can be handled by teleportation in these nodes - as mentioned in ML with graphs.
rw = RandomWalk([0, 1], [[1, 0], [0.5, 0.5]], jump_on_deadends=True)
The random walker will no longer be stuck in dead-end situations.
Ubuntu
3.8
pyrandwalk.__version__
)1.1
There are some examples for different uses of random walks on references (like the main reference) this issue is concerning adding these examples to Document/examples.ipynb
There is a major category in random walk which you will be payed off according to given payoff
list by stopping your walk in each state and/or will pay a cost according to given cost
variable. (You may take a factor called discount
into consider which will be manipulate our payoff by multiplying into it. [0<discount
<=1])
>>> rw = RandomWalk([0, 1, 2],
... [[0.5, 0.5, 0], [0.25, 0.5, 0.25], [0, 0, 0]],
... payoff=[1, 4, 0],
... cost=0.5,
... discount=0.9)
>>> rw.best_policy()
This repository has educational potential which can be published as a paper on Journal of Open Source Educational.
For this goal we need to take into consideration some actions which described here.
Related works will be fallowed on branch jose-paper-branch
.
It wold be nice if we have a general refactor before version 1 release.
@sepandhaghighi Can you take it into consider?
networkx
is a huge dependency and I wonder if we should replace it by a more lightweight library.
In pyrandwalk
we just use networkx
for two reasons:
networkx.is_strongly_connected
in is_irreducible
)networkx.strongly_connected_components
in get_typeof_classes
)netwokx.DiGraph
in get_graph
)networkx.draw_circular
in plot_graph
)As a alternative we can use graph-tools.
Until now pyrandwalk just accept numbers(int
s) as its states, it would be nice if we add other types support too.
For example we can take into consider a BiGram simulation like bellow:
>>> lexicon = ['I', 'eat', 'food']
>>> bigram = np.array([[0.1, 0.8, 0.1], [0.3, 0.1, 0.6], [0.6, 0.3, 0.1]])
>>> rw = RandomWalk(lexicon, bigram)
>>> states, probs = rw.run()
>>> states
['I', 'eat', 'food', 'food', 'I', 'food', 'I', 'eat', 'food', 'I', 'eat']
>>> probs
[0.3, 0.8, 0.6, 0.1, 0.6, 0.1, 0.1, 0.8, 0.6, 0.6, 0.8]
Traceback (most recent call last):
...
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
Linux 5.4.0-80-generic #90-Ubuntu SMP Fri Jul 9 22:49:44 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
Python 3.8.10
pyrandwalk.__version__
)1.0
These are the works that should be done before our first release:
final_dist
method for calculating final distirbution of the random walk..ipynb
document and its testsREADME.md
There are some cases in which the random walker would jump to none-adjacent states (Teleport) which you can see one of its use cases here in PageRank Overview by Dan Jurafsky.
>>> rw = RandomWalk([0, 1], [[1, 0], [0.5, 0.5]], teleport_prob=0.9)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.