python 移動平均線
There are situations, particularly when dealing with real-time data, when a conventional average is of little use because it includes old values which are no longer relevant and merely give a misleading impression of the current situation. The solution to this problem is to use moving averages, ie. the average of the most recent values rather than all values, which I will implement in Python.
在某些情況下,尤其是在處理實時數據時,常規平均值很少使用,因為常規平均值包括不再相關的舊值,只會給當前情況帶來誤導性印象。 解決此問題的方法是使用移動平均值。 我將在Python中實現的最新值而不是所有值的平均值。
To illustrate the problem I will show part of the output of the program I’ll write for this post. It shows the last few rows of a set of 1000 server response times.
為了說明這個問題,我將顯示我將為這篇文章編寫的程序輸出的一部分。 它顯示了一組1000個服務器響應時間中的最后幾行。

Most times in the left hand column are between 10ms and 50ms and can be considered normal but the last few shoot up considerably. The second column shows overall averages which we might use to monitor the server for any problems. However, the large number of normal times included in these averages mean that although the server has slowed to a crawl for the last few requests the averages have hardly risen at all and we wouldn’t realise anything was wrong. The last column shows 4-point moving averages, or the averages of only the last four values. These of course do increase a lot and so alarm bells should start to ring.
左欄中的大多數時間都在10毫秒至50毫秒之間,可以認為是正常的,但最后幾次大幅上升。 第二列顯示總體平均值,我們可以使用總體平均值來監視服務器是否存在任何問題。 但是,這些平均值中包含大量的正常時間,這意味著盡管服務器在最近的幾個請求中已放緩到爬網的速度,但平均值幾乎沒有上升,我們也不會意識到有什么不妥。 最后一列顯示4點移動平均值,或僅顯示最后四個值的平均值。 這些當然會增加很多,因此警鐘應該開始響起。
Having explained both the problem and its solution let’s write some code. This project consists of the following files which you can clone/download from the Github repository.
解釋了問題及其解決方案后,讓我們編寫一些代碼。 該項目包含以下文件,您可以從Github存儲庫中克隆/下載這些文件。
- movingaverageslist.py movingaverageslist.py
- movingaverages_test.py movingaverages_test.py
The movingaverageslist.py
file implements a class which maintains a list of numerical values, and each time a new value is added the overall average and moving average up to that point are also calculated.
movingaverageslist.py
文件實現了一個維護數值列表的類,并且每次添加新值時,也將計算總體平均值和直至該點的移動平均值。
In __init__
we simply create an empty list, and set the points
attribute, ie. the number of values used to calculate the average.
在__init__
我們僅創建一個空列表,并設置points
屬性,即。 用于計算平均值的值的數量。
In the append
method, the overall and moving averages are calculated using separate functions which I’ll come to in a minute. Then a dictionary containing the new value and the two averages is appended to the list.
在append
方法中,總體和移動平均值是使用單獨的函數計算的,我將在稍后介紹。 然后,將包含新值和兩個平均值的字典添加到列表中。
In __calculate_overall_average
we don’t need to add up all the values each time, we can just multiply the previous average by the count and then add the new value. This is then divided by the length + 1, ie. the length the list will be when the new value is added.
在__calculate_overall_average
我們不需要每次都將所有值相加,只需將先前的平均值乘以計數,然后添加新值即可。 然后將其除以長度+ 1,即。 添加新值時列表的長度。
The __calculate_moving_average
function uses a similar technique but is more complex as it has to allow for the list not yet having reached the length of the number of points. In this situation it just calculates the mean of whatever data the list has.
__calculate_moving_average
函數使用類似的技術,但更為復雜,因為它必須允許列表尚未達到點數的長度。 在這種情況下,它只計算列表中任何數據的平均值。
Lastly we implement __str__
which returns the data in a table format suitable for outputting to the console.
最后,我們實現了__str__
,它以適合于輸出到控制臺的表格格式返回數據。
The MovingAveragesList
class is now complete so let’s put together a simple demo.
現在, MovingAveragesList
類已經完成,因此讓我們進行一個簡單的演示。
In main
we call populate_response_times
to get a MovingAveragesList
object with 1000 items, and then print the object. As we implemented __str__
in the class this will be called and therefore we’ll see the table described above.
在main
函數中,我們調用populate_response_times
以獲取包含1000個項目的MovingAveragesList
對象,然后打印該對象。 當我們在類中實現__str__
,它將被調用,因此我們將看到上述表格。
I have also added a line which prints the last item in the list just to show how to access the most recent value and averages. A possible enhancement would be to wrap this in a method to avoid rummaging around in the inner workings of the class.
我還添加了一行,用于打印列表中的最后一項,以顯示如何訪問最新值和平均值。 可能的增強方法是將其包裝在一種方法中,以避免在類的內部工作過程中四處亂搞。
The populate_response_times
function creates a MovingAveragesList
object with a points value of 4. This is probably too low for practical purposes but it does make manual testing easier!
populate_response_times
函數創建一個MovingAveragesList
對象,其點值為4。這對于實際目的來說可能太低了,但是它確實使手動測試變得更加容易!
It then adds a large number of “normal” values to it; remember that each time a value is added new overall and moving averages are also added. Then a few large numbers are added to simulate a server problem before we return the object.
然后為它添加了大量的“正常”值; 請記住,每次添加值時都會添加新的總體和移動平均值。 然后在我們返回對象之前,添加一些大數字來模擬服務器問題。
Now we can run the program like this…
現在我們可以像這樣運行程序了……
python3.8 movingaverages_test.py
python3.8 movingaverages_test.py
I won’t repeat the output but you’ll see 1000 rows of data whizzing up your console.
我不會重復輸出,但是您會看到1000行數據在控制臺上飛馳。
可能的改進 (Possible Improvements)
The MovingAveragesList
class has been tailored to demonstrating the problem it solves and how it does it. In a production environment this are unnecessary and there are a few improvements which could make the class more efficient and useful.
MovingAveragesList
類經過定制,以演示其解決的問題以及如何解決此問題。 在生產環境中,這是不必要的,并且有一些改進可以使類更高效,更有用。
- We could drop the overall averages 我們可以降低總體平均水平
- Only the latest moving average could be kept 只能保留最新的移動平均線
- We could delete the oldest value each time a new one is added, just keeping a restricted number of the latest values 每次添加新值時,我們都可以刪除最舊的值,而只保留有限數量的最新值
- We could forget the list concept entirely and just keep a single moving average, updated from any new values added 我們可能會完全忘記列表概念,而只保留一個移動平均值,并根據添加的任何新值進行更新
- We could include a threshold and function to be called if the threshold is exceeded, for example sending out emails if the server response time slows to an unacceptable level 我們可以包括一個閾值和一個超過該閾值的函數,例如,如果服務器響應時間降至不可接受的水平,則發送電子郵件
翻譯自: https://medium.com/explorations-in-python/moving-averages-in-python-f72a3249cf07
python 移動平均線
本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。 如若轉載,請注明出處:http://www.pswp.cn/news/388496.shtml 繁體地址,請注明出處:http://hk.pswp.cn/news/388496.shtml 英文地址,請注明出處:http://en.pswp.cn/news/388496.shtml
如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!