2016-11-05 178 views
2

Matlab的smooth函數默認情況下使用5點移動平均來平滑數據。在Python中做同樣的事情最好的方法是什麼? 例如,如果這是我的數據MATLAB在NumPy/Python中的平滑實現(n點移動平均)

0 
0.823529411764706 
0.852941176470588 
0.705882352941177 
0.705882352941177 
0.676470588235294 
0.676470588235294 
0.500000000000000 
0.558823529411765 
0.647058823529412 
0.705882352941177 
0.705882352941177 
0.617647058823529 
0.705882352941177 
0.735294117647059 
0.735294117647059 
0.588235294117647 
0.588235294117647 
1 
0.647058823529412 
0.705882352941177 
0.764705882352941 
0.823529411764706 
0.647058823529412 
0.735294117647059 
0.794117647058824 
0.794117647058824 
0.705882352941177 
0.676470588235294 
0.794117647058824 
0.852941176470588 
0.735294117647059 
0.647058823529412 
0.647058823529412 
0.676470588235294 
0.676470588235294 
0.529411764705882 
0.676470588235294 
0.794117647058824 
0.882352941176471 
0.735294117647059 
0.852941176470588 
0.823529411764706 
0.764705882352941 
0.558823529411765 
0.588235294117647 
0.617647058823529 
0.647058823529412 
0.588235294117647 
0.617647058823529 
0.647058823529412 
0.794117647058824 
0.823529411764706 
0.647058823529412 
0.617647058823529 
0.647058823529412 
0.676470588235294 
0.764705882352941 
0.676470588235294 
0.647058823529412 
0.705882352941177 
0.764705882352941 
0.705882352941177 
0.500000000000000 
0.529411764705882 
0.529411764705882 
0.647058823529412 
0.676470588235294 
0.588235294117647 
0.735294117647059 
0.794117647058824 
0.852941176470588 
0.764705882352941 

平滑的數據應該是

0 
0.558823529411765 
0.617647058823530 
0.752941176470588 
0.723529411764706 
0.652941176470588 
0.623529411764706 
0.611764705882353 
0.617647058823530 
0.623529411764706 
0.647058823529412 
0.676470588235294 
0.694117647058824 
0.700000000000000 
0.676470588235294 
0.670588235294118 
0.729411764705882 
0.711764705882353 
0.705882352941177 
0.741176470588235 
0.788235294117647 
0.717647058823529 
0.735294117647059 
0.752941176470588 
0.758823529411765 
0.735294117647059 
0.741176470588235 
0.752941176470588 
0.764705882352941 
0.752941176470588 
0.741176470588235 
0.735294117647059 
0.711764705882353 
0.676470588235294 
0.635294117647059 
0.641176470588236 
0.670588235294118 
0.711764705882353 
0.723529411764706 
0.788235294117647 
0.817647058823530 
0.811764705882353 
0.747058823529412 
0.717647058823530 
0.670588235294118 
0.635294117647059 
0.600000000000000 
0.611764705882353 
0.623529411764706 
0.658823529411765 
0.694117647058824 
0.705882352941176 
0.705882352941176 
0.705882352941176 
0.682352941176471 
0.670588235294118 
0.676470588235294 
0.682352941176471 
0.694117647058824 
0.711764705882353 
0.700000000000000 
0.664705882352941 
0.641176470588236 
0.605882352941177 
0.582352941176471 
0.576470588235294 
0.594117647058824 
0.635294117647059 
0.688235294117647 
0.729411764705882 
0.747058823529412 
0.803921568627451 
0.764705882352941 

在Matlab的語法來獲得,這是

smooth(data) 

我想要做相同的蟒蛇,但我無法找到任何可以做到這一點的功能。

+0

這個問題是不是一樣http://stackoverflow.com/questions/13728392/moving-average-or-磨合是什麼意思? –

+0

@BillBell我不這麼認爲 – rsnaveen

+0

這裏有一些其他的例子:http://scipy-cookbook.readthedocs.io/items/SignalSmooth.html – Moritz

回答

9

MATLAB的smoooth func基本上與滑動窗口長度爲5的平均值相同,除了它在兩端處理2個元素的方式。由於每個鏈接的文檔,這些邊界情況計算這些公式 -

yy = smooth(y) smooths the data in the column vector y .. 
The first few elements of yy are given by 

yy(1) = y(1) 
yy(2) = (y(1) + y(2) + y(3))/3 
yy(3) = (y(1) + y(2) + y(3) + y(4) + y(5))/5 
yy(4) = (y(2) + y(3) + y(4) + y(5) + y(6))/5 
... 

所以,要複製上與NumPy/Python的相同的實現,我們可以用NumPy's 1D convolution用於獲取滑動窗口求和,並通過窗口長度將它們劃分給我們的平均結果。然後,只需附加邊界元素的特殊情況處理值即可。

因此,我們將不得不處理通用的窗口大小,像這樣的實現 -

def smooth(a,WSZ): 
    # a: NumPy 1-D array containing the data to be smoothed 
    # WSZ: smoothing window size needs, which must be odd number, 
    # as in the original MATLAB implementation 
    out0 = np.convolve(a,np.ones(WSZ,dtype=int),'valid')/WSZ  
    r = np.arange(1,WSZ-1,2) 
    start = np.cumsum(a[:WSZ-1])[::2]/r 
    stop = (np.cumsum(a[:-WSZ:-1])[::2]/r)[::-1] 
    return np.concatenate(( start , out0, stop )) 
+0

非常感謝@Divakar。但是當我嘗試使用函數'smooth'時出現以下錯誤:'AttributeError:'list'object has no attribute'cumsum'' – rsnaveen

+0

@rsnaveen我假設'a'是一個NumPy數組。修正它來處理數組和列表。 – Divakar

+0

非常感謝@Divakar。 – rsnaveen