, 당신은 scipy.signal.lfilter
를 사용할 수 있습니다.
import numpy as np
from scipy.signal import lfilter
np.random.seed(123)
A = np.random.randn(10)
a = 2.0
b = 3.0
# Compute the recursion using lfilter.
# [a] and [1, -b] are the coefficients of the numerator and
# denominator, resp., of the filter's transfer function.
B = lfilter([a], [1, -b], A)
print B
# Compare to a simple loop.
B2 = np.empty(len(A))
for k in range(0, len(B2)):
if k == 0:
B2[k] = a*A[k]
else:
B2[k] = a*A[k] + b*B2[k-1]
print B2
print "max difference:", np.max(np.abs(B2 - B))
스크립트의 출력은 다음과 같습니다 :
[ -2.17126121e+00 -4.51909273e+00 -1.29913212e+01 -4.19865530e+01
-1.27116859e+02 -3.78047705e+02 -1.13899647e+03 -3.41784725e+03
-1.02510099e+04 -3.07547631e+04]
[ -2.17126121e+00 -4.51909273e+00 -1.29913212e+01 -4.19865530e+01
-1.27116859e+02 -3.78047705e+02 -1.13899647e+03 -3.41784725e+03
-1.02510099e+04 -3.07547631e+04]
max difference: 0.0
는
B = lfilter([a], [1.0, -b], A)
여기에 전체 스크립트입니다 :이 경우, 당신이 필요로하는 모든입니다 (A
를 가정하고 한 차원 NumPy와 배열)
또 다른 예는 IPython에서 numpy 배열 대신 pandas DataFrame을 사용하는 것입니다.
당신은
In [12]: df = pd.DataFrame([1, 7, 9, 5], columns=['A'])
In [13]: df
Out[13]:
A
0 1
1 7
2 9
3 5
을 가지고 있고 (K < 0 B[k] == 0
포함) B[k] = A[k] + 2*B[k-1]
, 당신은 여기
In [14]: df['B'] = lfilter([1], [1, -2], df['A'].astype(float))
In [15]: df
Out[15]:
A B
0 1 1
1 7 9
2 9 27
3 5 59
개방 문제에 쓸 수 있도록 새 열,
B
를 만들려면 그것을 cythonize하려면 : https://github.com/pydata/pandas/issues/4567, 그러나 일부 링크도 마찬가지입니다. – Jeff'scipy.signal.lfilter'를 사용할 수 있습니다. 예제는 http://stackoverflow.com/questions/21336794/python- recursive-vectorization-with-timeseries를 참조하십시오. –