Weighted smoothing of a 1D array - Python
Question: I am quite new to Python and I have an array of some parameter detections,
some of the values were detected incorrectly and (like 4555555):

    
    
    array = [1, 20, 55, 33, 4555555, 1]
    

And I want to somehow smooth it. Right now I'm doing that with a weighted
mean:

    
    
    def smoothify(array):
        for i in range(1, len(array) - 2):
            array[i] = 0.7 * array[i] + 0.15 * (array[i - 1] + array[i + 1])
        return array
    

But it works pretty bad, of course, we can take a weighted mean of more than 3
elements, but it results in copypasting... I tried to find some native
functions for that, but I failed.

Could you please help me with that?

P.S. Sorry if it's a noob question :(

Thanks for your time, Best regards, Anna


Answer: Would suggest
[numpy.average](http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.average.html)
to help you with this. the trick is getting the weights calculated - below I
zip up the three lists - one the same as the original array, the next one step
ahead, the next one step behind. Once we have the weights, we feed them into
the `np.average` function

    
    
    import numpy as np
    array = [1, 20, 55, 33, 4555555, 1]
    arrayCompare = zip(array, array[1:] + [0], [0] + array)
    
    weights = [.7 * x + .15 * (y + z) for x, y, z in arrayCompare]
    
    avg = np.average(array, weights=weights)
    

