如何在python中计算时间序列的平均值和最大增、减
我试图用Python计算时间序列的以下特性:如何在python中计算时间序列的平均值和最大增、减,python,numpy,feature-extraction,Python,Numpy,Feature Extraction,我试图用Python计算时间序列的以下特性: 平均增减 最大增减量 但我不知道如何快速、简单、正确地完成它。也许是用numpy或scipy 我很高兴能得到任何帮助 我发现了以下特征的数学解释: 非常感谢如果数据在列表中,您可以将其切分。例如: a = [2,6,8,4,5,9] b = a[:-1] c = a[1:] x = np.random.random_integers(0, 10, 20) print(x) # [10 10 5 4 2 10 8 9 10 2 2
- 平均增减
- 最大增减量
非常感谢如果数据在列表中,您可以将其切分。例如:
a = [2,6,8,4,5,9]
b = a[:-1]
c = a[1:]
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
因此,您可以使用
max([j-i for (i,j) in zip(b,c)])
如果使用numpy的数据量很大,而且实际上会更容易,只需将“a”设为numpy.array,您就可以通过以下方式获得最大增量:
numpy.max(c-b)
可以使用计算数组中连续元素之间的差异,然后使用布尔索引选择正值(对应于增加)或负值(对应于减少)。从那里,你可以得到平均值,max等等
例如:
a = [2,6,8,4,5,9]
b = a[:-1]
c = a[1:]
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
这真是一个干净又好的解决方案。非常感谢你