简体   繁体   English

如何在Python中计算时间序列的均值和最大值增加,减少

[英]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: 我正在尝试计算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). 您可以使用np.diff来计算数组中连续元素之间的差异,然后使用np.diff选择正值(对应于增加)或负值(对应于减少)。 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可以处理大数据,并且实际上会更容易,只需将“ a”设置为numpy.array即可:

numpy.max(c-b)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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