簡體   English   中英

從 3d 數據元素中刪除異常值

[英]Remove outliers from 3d data elements

我寫了一個 function 從數據集中刪除異常值。 例如,它使用 z-score 並且適用於 1d 的元素;

# usage remove_outliers(data)  
[10 99 12 15 9 2 17 15]---->[10 12 15 9 17 15]

但是,3d 數據是錯誤的,例如,它會分解我的 3d 數據;

# usage remove_outliers(data, thresh=(30,30,30), axis=(0,1))  
[(0, 10, 3) (99, 255, 255) (100, 10, 9) (45, 34, 9)]---->[  0  10   3  99 255 255 100  10   9  45  34   9]

我期待結果是這樣的;

[(0, 10, 3) (100, 10, 9) (45, 34, 9)]

我在 function remove_outliers()中做錯了什么,如何編輯它以處理 3d 元素數據?

def remove_outliers(data, thresh=2.0, axis=None):
    # If a value is > thresh std_deviations from the mean they are an outlier and remove it
    # Eg, thresh = 3, std_dev = 2, mean=18. If value=7, then 7 is an outlier
    d = np.abs(data - np.median(data, axis))
    mdev = np.median(d, axis)
    s = d/mdev if mdev else 0.0
    return data[s<thresh]

您需要結合每個點的坐標條件。 在下面的代碼中,這是由.all(axis=1)完成的

# numpy.median is rather slow, let's build our own instead
def median(x):
    m,n = x.shape
    middle = np.arange((m-1)>>1,(m>>1)+1)
    x = np.partition(x,middle,axis=0)
    return x[middle].mean(axis=0)

# main function
def remove_outliers(data,thresh=2.0):           
    m = median(data)                            
    s = np.abs(data-m)                          
    return data[(s<median(s)*thresh).all(axis=1)]

# small test
remove_outliers(np.array([(0, 10, 3), (99, 255, 255), (100, 10, 9), (45, 34, 9)]))
# array([[100,  10,   9],
#        [ 45,  34,   9]])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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