[英]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.