簡體   English   中英

如何在Python中計算時間序列的均值和最大值增加,減少

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

我正在嘗試計算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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM