简体   繁体   中英

How to calculate mean and max increase, decrease of time series in python

I'm trying to calculate this features of a time series in Python:

  • Mean Increase and Decrease
  • Max Increase and Decrease

But i can't figure out how to do it in a fast, easy and correct way. Maybe with numpy or scipy.

I'm very glad about any help.

I found the following mathematical explanation of the features:

平均最大增加减少量计算

Thank you very much

You can use np.diff to compute the differences between consecutive elements in your array, then use boolean indexing to select either positive values (corresponding to increases) or negative values (corresponding to decreases). From there, you can take the mean, max etc.

For example:

x = np.random.random_integers(0, 10, 20)
print(x)
# [10 10  5  4  2 10  8  9 10  2  2  0  7  3  8  6  4  1  3 10]

dx = np.diff(x)
print(dx)
# [ 0 -5 -1 -2  8 -2  1  1 -8  0 -2  7 -4  5 -2 -2 -3  2  7]

increases = dx[dx > 0]
print(increases)
# [8 1 1 7 5 2 7]

print(increases.mean())
# 4.42857142857

print(increases.max())
# 8

If the data are in a list you can slice it up. For example:

a = [2,6,8,4,5,9]
b = a[:-1]
c = a[1:]

So you can get max increase with

max([j-i for (i,j) in zip(b,c)])

If the data is big using numpy will be the way, and it will actually be easier, just make "a" be a numpy.array then you can get max increase with:

numpy.max(c-b)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM