Extracting Boundaries Of Dense Regions Of 1s In A Huge List Of 1s And 0s
I'm not sure how to word my problem. But here it is... I have a huge list of 1s and 0s [Total length = 53820]. Example of how the list looks like - [0,1,1,1,1,1,1,1,1,0,0,0,1,1,0,
Solution 1:
OK, you need an answer...
First, the imports (we are going to use LineCollections
)
import numpy as np ; import matplotlib.pyplotas plt ;
from matplotlib.collectionsimportLineCollection
Next, definition of constants
N = 1001; np.random.seed(20190515)
and generation of fake data
x = np.linspace(0,1, 1001)
prob = np.where(x<0.4, 0.02, np.where(x<0.7, 0.95, 0.02))
y = np.where(np.random.rand(1001)<prob, 1, 0)
here we create the line collection, sticks
is a N×2×2
array
containing the start and end points of our vertical lines
sticks = np.array(list(zip(zip(x, np.zeros(N)), zip(x, y))))
lc = LineCollection(sticks)
finally, the cumulated sum, here normalized to have the same scale as the vertical lines
cs = (y-0.5).cumsum()
csmin, csmax = min(cs), max(cs)
cs = (cs-csmin)/(csmax-csmin) # normalized to 0 ÷ 1
We have just to plot our results
f, a = plt.subplots()
a.add_collection(lc)
a.plot(x, cs, color='red')
a.grid()
a.autoscale()
Here it is the plot
and here a detail of the stop zone.
You can smooth the cs
data and use something from scipy.optimize
to
spot the position of extremes. Should you have a problem in this last
step please ask another question.
Post a Comment for "Extracting Boundaries Of Dense Regions Of 1s In A Huge List Of 1s And 0s"