如果我有一個數字串列,我將用簡單的例子進行解釋,然后深入探討
t_original = [180,174,168,166,162,94,70,80,128,131,160,180]
如果我們繪制它,它會從 180 下降到 70,然后它會再次上升到 180
但如果我們突然將第四個值 (166) 更改為 450,那么串列將是
t = [180,174,168,700,162,94,70,80,128,131,160,180]
圖中沒有意義
我想將第四個值(700)視為錯誤值我想用相對值替換它,即使不是原始值而是相對于前兩個元素(168,174) 我想對整個串列做同樣的事情如果另一個再次出現錯誤值
我們可以稱之為[填補數字串列之間的空白]
所以我嘗試做同樣的想法,但更大的例子
我試過的方法
我將與輸出共享我的代碼,過濾意味著應用填充間隙功能
我的代碼
def preprocFN(*U):
prePlst=[] # after preprocessing list
#preprocessing Fc =| 2*LF1 prev by 1 - LF2 prev by 2 |
c0 = -2 #(previous) by 2
c1 =-1 #(previous)
c2 =0 #(current)
c3 = 1 #(next)
preP = U[0] # original list
if c2 == 0:
prePlst.append(preP[0])
prePlst.append(preP[1])
c1 =2
c2 =2
c0 =2
oldlen = len(preP)
while oldlen > c2:
Equ = abs(2*preP[c1] - preP[c0]) #fn of preprocessing #removed abs()
formatted_float = "{:.2f}".format(Equ) #with .2 number only
equu = float(formatted_float) #from string float to float
prePlst.insert(c2,equu) # insert the preprocessed value to the List
c1 =1
c2 =1
c0 =1
return prePlst
我的輸入:
我的代碼:
def outLiersFN(*U):
outliers=[] # after preprocessing list
#preprocessing Fc =| 2*LF1 prev by 1 - LF2 prev by 2 |
c0 = -2 #(previous) by 2 #from original
c1 =-1 #(previous) #from original
c2 =0 #(current) #from original
c3 = 1 #(next) #from original
preP = U[0] # original list
if c2 == 0:
outliers.append(preP[0])
c1 =1
c2 =1
c0 =1
c3 =1
oldlen = len(preP)
M_RangeOfMotion = 90
while oldlen > c2 :
if c3 == oldlen:
outliers.insert(c2, preP[c2]) #preP[c2] >> last element in old list
break
if (preP[c2] > M_RangeOfMotion and preP[c2] < (preP[c1] preP[c3])/2) or (preP[c2] < M_RangeOfMotion and preP[c2] > (preP[c1] preP[c3])/2): #Check Paper 3.3.1
Equ = (preP[c1] preP[c3])/2 #fn of preprocessing # From third index # ==== inserting current frame
formatted_float = "{:.2f}".format(Equ) #with .2 number only
equu = float(formatted_float) #from string float to float
outliers.insert(c2,equu) # insert the preprocessed value to the List
c1 =1
c2 =1
c0 =1
c3 =1
else :
Equ = preP[c2] # fn of preprocessing #put same element (do nothing)
formatted_float = "{:.2f}".format(Equ) # with .2 number only
equu = float(formatted_float) # from string float to float
outliers.insert(c2, equu) # insert the preprocessed value to the List
c1 = 1
c2 = 1
c0 = 1
c3 = 1
return outliers
uj5u.com熱心網友回復:
我建議以下演算法:
t[i]
如果資料點偏離平均值t[i-2], t[i-1], t[i], t[i 1], t[i 2]
超過這 5 個元素的標準偏差,則該資料點被視為例外值。- 例外值被它們周圍的兩個元素的平均值替換。
import matplotlib.pyplot as plt
from statistics import mean, stdev
t = [180,174,168,700,162,94,70,80,128,131,160,180]
def smooth(t):
new_t = []
for i, x in enumerate(t):
neighbourhood = t[max(i-2,0): i 3]
m = mean(neighbourhood)
s = stdev(neighbourhood, xbar=m)
if abs(x - m) > s:
x = ( t[i - 1 (i==0)*2] t[i 1 - (i 1==len(t))*2] ) / 2
new_t.append(x)
return new_t
new_t = smooth(t)
plt.plot(t)
plt.plot(new_t)
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/480074.html
下一篇:二維陣列python的乘積之和