Getting time frequency of number in array in Python? -


let's have time series represented in numpy array, every 3 seconds, data point. looks (but many more data points):

z = np.array([1, 2, 1, 2.2, 3, 4.4, 1, 1.2, 2, 3, 2.1, 1.2, 5, 0.5]) 

i want find threshold where, on average, every y seconds data point surpass threshold (x).

maybe question easier understand in sense: let's i've gathered data on how many ants leaving mound every 3 seconds. using data, want create threshold (x) in future if number of ants leaving @ 1 time exceeds x, beeper go off. key part - want beeper go off every 4 seconds. i'd use python figure out x should given y amount of time based on array of data i've collected.

is there way in python?

i think easiest first think in terms of statistics. think saying want calculate 100*(1-m/nth) percentile, number such value below 1-m/nth of time, m sampling period , n desired interval. in example, 100*(1-3/4th) percentile, or 25th percentile. is, want value exceeded 75% of time.

so calculate on data, should use scipy.stats.scoreatpercentile. case can like:

>>> z = np.array([1, 2, 1, 2.2, 3, 4.4, 1, 1.2, 2, 3, 2.1, 1.2, 5, 0.5]) >>> m = 3. >>> n = 4. >>> x = scipy.stats.scoreatpercentile(z, 100*(1-m/n)) >>> print(x) 1.05 >>> print((z>x).sum()/len(z))  # test, should 0.75 0.714285714286 

of course if have lot of values estimate better.

edit: had percentile backwards. should 1-m/n, had m/n.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -