Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function which is similar to numpy's diff

I was wondering if there exists a function, which would compute at the same time moving average and combine it with np.diff?

If you have an array and you would compute a mean for a moving window (moving average) and compute the difference between that mean and next 1 element.

Example:

a = [1, 3, 4, 5, 15, 14, 16, 13]
b = np.diff(a)
#np.diff makes something like this: `[n] - [n-1]`
#I want something like this: `[n] - np.mean([n-m : n])`

#I would like to have a function, where I could vary `m`:

m = 2
d = [2, 1.5, 10.5, 4, 1.5, -2]

How would I implement it, so that time computation wouldn't be so long, since I would like to use it for an array of 26000 elements and higher m?

like image 808
Mapa Avatar asked Oct 19 '22 17:10

Mapa


1 Answers

Edit 1: OP has updated his question after I had given my first answer. The updated answer can be found below after EDIT2.

Not sure what exactly you try to do but in this case you could simply do the following in order to obtain diff:

import numpy as np
diff = np.array(array[n-1:]) -  np.array(average[:-n+2])

Then diff will be the desired output:

array([  2. ,   1.5,  10.5,   4. ,   1.5,  -2. ])

So you first slice your lists using the parameter n, then convert your lists to arrays and subtract them from each other. The above line of code would be even simpler if a) your lists had the same length, b) n was your index and not the element you want to start with and c) if you used numpy arrays instead of lists:

import numpy as np

# add one additional value so that the arrays have the same length
myArray = np.array([1, 3, 4, 5, 15, 14, 16, 13, 17]) 

# choose the starting index rather than the element
n = 2

myAverage = np.array([2, 3.5, 4.5, 10, 14.5, 15, 14.5])

diffAr = myArray[n:] - myAverage

Then diffAr looks like this (one element more than in your case since I added one element to myArray):

array([  2. ,   1.5,  10.5,   4. ,   1.5,  -2. ,   2.5])

Just a general comment: Please don't use array and diff as variable names.

EDIT2:

You changed your question; here is now an updated answer. The only thing one has to add to the answer above is a way to calculate the running mean given the window size m. After that, one can do exactly what I did above:

import numpy as np

def runningMean(ar, m):

    return np.convolve(ar, np.ones((m,))/m)[(m-1):]

a = np.array([1, 3, 4, 5, 15, 14, 16, 13])
m = 2
av = runningMean(a, m)

d = a[m:] - av[:-m]

In this case d contains the desired output:

array([  2. ,   1.5,  10.5,   4. ,   1.5,  -2. ])
like image 129
Cleb Avatar answered Oct 31 '22 12:10

Cleb