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.mzph.cn/news/388496.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!