python 移動平均線_Python中的移動平均線

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個服務器響應時間中的最后幾行。

Image for post
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,一經查實,立即刪除!

相關文章

Ireport制作過程

Ireport制作過程 1、首先要到Option下設置一下ClassPath添加文件夾 2、到預覽->報表字段設置一下將要用到的字段 3、到編輯->查詢報表->寫sql語句,然后把語句查詢的字段結果與上面設置的報表字段的名要對應上 4、Option->選項->Compiler設置一下…

2018.09.16 loj#10243. 移棋子游戲(博弈論)

傳送門 題目中已經給好了sg圖&#xff0c;直接在上面跑出sg函數即可。 最后看給定點的sg值異或和是否等于0就判好了。 代碼&#xff1a; #include<bits/stdc.h> #define N 2005 #define M 6005 using namespace std; int n,m,k,sg[N],first[N],First[N],du[N],cnt0,an…

html5字體的格式轉換,font字體

路由器之家網今天精心準備的是《font字體》&#xff0c;下面是詳解&#xff01;html中的標簽是什么意思HTML提供了文本樣式標記&#xff0c;用來控制網頁中文本的字體、字號和顏色&#xff0c;多種多樣的文字效果可以使網頁變得更加絢麗。其基本語法格式&#xff1a;文本內容fa…

紅星美凱龍牽手新潮傳媒搶奪社區消費市場

瞄準線下流量紅利&#xff0c;紅星美凱龍牽手新潮傳媒搶奪社區消費市場 中新網1月14日電 2019年1月13日&#xff0c;紅星美凱龍和新潮傳媒戰略合作發布會在北京召開&#xff0c;雙方宣布建立全面的戰略合作伙伴關系。未來&#xff0c;新潮傳媒的梯媒產品將入駐紅星美凱龍的全國…

機器學習 啤酒數據集_啤酒數據集上的神經網絡

機器學習 啤酒數據集Artificial neural networks (ANNs), usually simply called neural networks (NNs), are computing systems vaguely inspired by the biological neural networks that constitute animal brains.人工神經網絡(ANN)通常簡稱為神經網絡(NNs)&#xff0c;是…

實例演示oracle注入獲取cmdshell的全過程

以下的演示都是在web上的sql plus執行的&#xff0c;在web注入時 把select SYS.DBMS_EXPORT_EXTENSION.....改成   /xxx.jsp?id1 and 1<>a||(select SYS.DBMS_EXPORT_EXTENSION.....)   的形式即可。(用" a|| "是為了讓語句返回true值)   語句有點長…

html視頻位置控制器,html5中返回音視頻的當前媒體控制器的屬性controller

實例檢測該視頻是否有媒體控制器&#xff1a;myViddocument.getElementById("video1");alert("Controller: " myVid.controller);定義和用法controller 屬性返回音視頻的當前媒體控制器。默認地&#xff0c;音視頻元素不會有媒體控制器。如果規定了媒體控…

ER TO SQL語句

ER TO SQL語句的轉換&#xff0c;在數據庫設計生命周期的位置如下所示。 一、轉換的類別 從ER圖轉化得到關系數據庫中的SQL表&#xff0c;一般可分為3類&#xff1a; 1&#xff09;轉化得到的SQL表與原始實體包含相同信息內容。該類轉化一般適用于&#xff1a; 二元“多對多”關…

dede 5.7 任意用戶重置密碼前臺

返回了重置的鏈接&#xff0c;還要把&amp刪除了&#xff0c;就可以重置密碼了 結果只能改test的密碼&#xff0c;進去過后&#xff0c;這個居然是admin的密碼&#xff0c;有點頭大&#xff0c;感覺這樣就沒有意思了 我是直接上傳的一句話&#xff0c;用菜刀連才有樂趣 OK了…

nasa數據庫cm1數據集_獲取下一個地理項目的NASA數據

nasa數據庫cm1數據集NASA provides an extensive library of data points that they’ve captured over the years from their satellites. These datasets include temperature, precipitation and more. NASA hosts this data on a website where you can search and grab in…

注入代碼oracle

--建立類 select SYS.DBMS_EXPORT_EXTENSION.GET_DOMAIN_INDEX_TABLES(FOO,BAR,DBMS_OUTPUT".PUT(:P1);EXECUTE IMMEDIATE DECLARE PRAGMA AUTONOMOUS_TRANSACTION;BEGIN EXECUTE IMMEDIATE  create or replace and compile java source named "LinxUtil" as …

html5包含inc文件,HTML中include file標簽的用法

參數PathType將 FileName 的路徑類型。路徑可為以下某種類型&#xff1a;路徑類型 含義文件 該文件名是帶有 #include 命令的文檔所在目錄的相對路徑。被包含文件可位于相同目錄或子目錄中&#xff1b;但它不能處于帶有 #include 命令的頁的上層目錄中。虛擬 文件名為 Web 站點…

r語言處理數據集編碼_在強調編碼語言或工具之前,請學習這3個基本數據概念

r語言處理數據集編碼重點 (Top highlight)I got an Instagram DM the other day that really got me thinking. This person explained that they were a data analyst by trade, and had years of experience. But, they also said that they felt that their technical skill…

springboot微服務 java b2b2c電子商務系統(一)服務的注冊與發現(Eureka)

一、spring cloud簡介spring cloud 為開發人員提供了快速構建分布式系統的一些工具&#xff0c;包括配置管理、服務發現、斷路器、路由、微代理、事件總線、全局鎖、決策競選、分布式會話等等。它運行環境簡單&#xff0c;可以在開發人員的電腦上跑。Spring Cloud大型企業分布式…

linux部署服務器常用命令

fdisk -l 查分區硬盤 df -h 查空間硬盤 cd / 進目錄 ls/ll 文件列表 vi tt.txt iinsert 插入 shift: 進命令行 wq 保存%退出 cat tt.txt 內容查看 pwd 當期目錄信息 mkdir tt建目錄 cp tt.txt tt/11.txt 拷貝文件到tt下 mv 11.txt /usr/ 移動 rm -rf tt.txt 刪除不提示 rm t…

HTML和CSS面試問題總結,html和css面試總結

html和cssw3c 規范結構化標準語言樣式標準語言行為標準語言1) 盒模型常見的盒模型有w3c盒模型(又名標準盒模型)box-sizing:content-box和IE盒模型(又名怪異盒模型)box-sizing:border-box。標準盒子模型&#xff1a;寬度內容的寬度(content) border padding margin低版本IE盒子…

css清除浮動float的七種常用方法總結和兼容性處理

在清除浮動前我們要了解兩個重要的定義&#xff1a; 浮動的定義&#xff1a;使元素脫離文檔流&#xff0c;按照指定方向發生移動&#xff0c;遇到父級邊界或者相鄰的浮動元素停了下來。 高度塌陷&#xff1a;浮動元素父元素高度自適應&#xff08;父元素不寫高度時&#xff0c;…

數據遷移測試_自動化數據遷移測試

數據遷移測試Data migrations are notoriously difficult to test. They take a long time to run on large datasets. They often involve heavy, inflexible database engines. And they’re only meant to run once, so people think it’s throw-away code, and therefore …

使用while和FOR循環分布打印字符串S='asdfer' 中的每一個元素

方法1&#xff1a; s asdfer for i in s :print(i)方法2:index 0 while 1:print(s[index])index1if index len(s):break 轉載于:https://www.cnblogs.com/yuhoucaihong/p/10275800.html

山師計算機專業研究生怎么樣,山東師范大學有計算機專業碩士嗎?

山東師范大學位于山東省濟南市&#xff0c;學校是一所綜合性高等師范院校。該院校深受廣大報考專業碩士學員的歡迎&#xff0c;因此很多學員想要知道山東師范大學有沒有計算機專業碩士&#xff1f;山東師范大學是有計算機專業碩士的。下面就和大家介紹一下培養目標有哪些&#…