![](/img/trans.png)
[英]How do i calculate the percentage increase or decrease in a list of values with python
[英]How to calculate mean and max increase, decrease of time series in python
您可以使用np.diff
来计算数组中连续元素之间的差异,然后使用np.diff
选择正值(对应于增加)或负值(对应于减少)。 从那里,您可以取平均值,最大值等。
例如:
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
如果数据在列表中,则可以对其进行切片。 例如:
a = [2,6,8,4,5,9]
b = a[:-1]
c = a[1:]
因此,您可以获得最大的增长
max([j-i for (i,j) in zip(b,c)])
如果使用numpy可以处理大数据,并且实际上会更容易,只需将“ a”设置为numpy.array即可:
numpy.max(c-b)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.