简体   繁体   English

旧数字在numpy.convolve和Python中无法更改

[英]Old numbers can't change in numpy.convolve and Python

I am working with numpy.convolve. 我正在使用numpy.convolve。 To be honest I am not sure if I am using the correct module for the project I am working. 老实说,我不确定我是否为我正在使用的项目使用正确的模块。

I want numpy convolve (or if there is any other module I can implement) not to change previous numbers. 我希望numpy卷积(或者如果可以实现其他模块)不要更改以前的数字。 I don't want the old numbers changed I want them to be fixed. 我不想改变旧的数字,我希望将它们固定下来。 So when I get new data the only number that should be affect is the new one, not the rest. 因此,当我获得新数据时,唯一应该影响的数字是新数字,而不是其余数字。

Here is an example to clarify: 这是一个例子,以澄清:

import numpy 

N = 3
numbers = [11,-5,54,-100,1]
numbers_mean  = numpy.convolve(numbers, numpy.ones((N,))/N)[(N-1):]
print numbers_mean

Output: 输出:

[ 20.         -17.         -15.         -33.           0.33333333]

But when I change the last number to 100 instead of 1 the result changes all 3 numbers. 但是,当我将最后一个数字更改为100而不是1时,结果将更改所有3个数字。

N = 3
numbers = [11,-5,54,-100,100]
numbers_mean  = numpy.convolve(numbers, numpy.ones((N,))/N)[(N-1):]
print numbers_mean

Output: 输出:

[ 20.         -17.          18.           0.          33.33333333]

JUST THE OUTPUTS: 只是输出:

[ 20.         -17.         -15.         -33.           0.33333333]
[ 20.         -17.          18.           0.          33.33333333]

THIS IS WHAT I WANT TO SEE: 这是我想看的:

[ 20.         -17.         -15.         -33.           33.33333333<- this number only should change not the rest]

So as you can see when the number goes from 1 to 100 it changed the -33. 因此,您可以看到当数字从1变为100时,它更改了-33。 to 0. This is not what I want I want all of those numbers to be fixed and none changing the only number that should be able to change is the newest number. 到0。这不是我想要的,我希望所有这些数字都是固定的,并且没有一个改变的唯一数字应该是最新的。 in this case 33.333333 from 0.3333 在这种情况下为0.3333的33.333333

Is there any module or way I can implement this in Python? 有什么模块或方法可以在Python中实现吗?

You want to compute 3-neighborhood means. 您要计算3邻域均值。 So you have a mask=[1./3,1./3,1./3] which size is N . 因此,您有一个mask=[1./3,1./3,1./3] 。/ mask=[1./3,1./3,1./3] ,其大小为N

See what happen on a simpler example , numbers = [0,1,2,3,4] , which size is M>=N : 看看一个简单的例子, numbers = [0,1,2,3,4] ,其大小为M>=N

In [1]: numpy.convolve(numbers,mask)
Out[1]: 
array([ 0.        ,  0.33333333,  1.        ,  2.        ,  3.        ,
        2.33333333,  1.33333333])

This is an array with N+M-1 elements . 这是具有N+M-1元素的数组。 There are only M-2(N-1) valid means, [1,2,3] here, and N-1 overlapping means at the beginning and at the END : You must discard them too. 只有M-2(N-1)有效装置,此处[1,2,3],并且在开头和结尾处有N-1重叠装置:您也必须丢弃它们。

For your case : 对于您的情况:

N = 3
a1 = [11,-5,54,-100,1]
a2 = [11,-5,54,-100,100]

mask=numpy.ones(N)/N
m1= numpy.convolve(a1, mask)[(N-1):-(N-1)]
m2= numpy.convolve(a2, mask)[(N-1):-(N-1)]
print (m1,m2)

# [ 20. -17. -15.] [ 20. -17.  18.]

Only the last term is affected. 仅最后一个学期受到影响。

Edit 编辑

To avoid the N-1 last terms, an other approach is to use scipy.signal.lfilter , which implement differences equations : 为了避免N-1最后项,另一种方法是使用scipy.signal.lfilter ,它实现了差分方程:

Here, mean(n) - mean(n-1) = data(n)/N -data(nN)/N . 在此, mean(n) - mean(n-1) = data(n)/N -data(nN)/N

from scipy.signal import lfilter
N=3
a=np.array([1,-1])
b=np.concatenate(([1],np.zeros(N-1),[-1]))/N
x=np.arange(5)
means=lfilter(b,a,x)
# array([0.,0.3333,1.,2.,3.])

i'm not a numpy expert, so i will propose you a Python solution for that: 我不是一个麻木的专家,所以我将为您提出一个Python解决方案:

In [342]: fixed = np.convolve([11,-5,54,-100,1], np.ones((N,))/N)[(N-1):-1]

In [343]: fixed
Out[343]: array([ 20., -17., -15., -33.])

In [344]: np.append(fixed, np.convolve([11,-5,54,-100,100], np.ones((N,))/N)[-1])
Out[344]: array([ 20.        , -17.        , -15.        , -33.        ,  33.33333333])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM