scipy.signal.savgol_filter的False ValueError

时间:2017-11-07 06:38:02

标签: python pandas scipy filtering

我很困惑。我有21个文件由同一个进程生成,我使用相同参数的savitzky-golay过滤器对它们进行过滤。 它适用于某些文件,但在某些时候,我会收到ValueError: array must not contain infs or NaNs。问题是,我检查了文件,没有任何infs或NaN!

print "nan", df.isnull().sum()
print "inf", np.isinf(df).sum()

给出

nan x        0
T          0
std_T      0
sterr_T    0
dtype: int64
inf x        0
T          0
std_T      0
sterr_T    0
dtype: int64

所以问题可能在于过滤器的实现?这可能是由于例如相对于数据的长度或步长选择窗口长度或多重顺序吗?

完成追溯:

Traceback (most recent call last):

  File "<ipython-input-7-40b33049ef41>", line 1, in <module>
    runfile('D:/data/scripts/DailyProfiles_Gradients.py', wdir='D:/data/DFDP2/DFDP2B/DTS/DTS_scripts')

  File "C:\Users\me\AppData\Local\Continuum\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
    execfile(filename, namespace)

  File "C:\Users\me\AppData\Local\Continuum\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "D:/data/scripts/DailyProfiles_Gradients.py", line 142, in <module>
    grad = gradient(y, x, scale,PO)

  File "D:/data/scripts/DailyProfiles_Gradients.py", line 76, in Tgradient
    smoothed=savgol_filter(list(x), scale, PO, deriv=1, delta=dy[0])

  File "C:\Users\me\AppData\Local\Continuum\Anaconda2\lib\site-packages\scipy\signal\_savitzky_golay.py", line 337, in savgol_filter
    coeffs = savgol_coeffs(window_length, polyorder, deriv=deriv, delta=delta)

  File "C:\Users\me\AppData\Local\Continuum\Anaconda2\lib\site-packages\scipy\signal\_savitzky_golay.py", line 140, in savgol_coeffs
    coeffs, _, _, _ = lstsq(A, y)

  File "C:\Users\me\AppData\Local\Continuum\Anaconda2\lib\site-packages\scipy\linalg\basic.py", line 822, in lstsq
    b1 = _asarray_validated(b, check_finite=check_finite)

  File "C:\Users\me\AppData\Local\Continuum\Anaconda2\lib\site-packages\scipy\_lib\_util.py", line 187, in _asarray_validated
    a = toarray(a)

  File "C:\Users\me\AppData\Local\Continuum\Anaconda2\lib\site-packages\numpy\lib\function_base.py", line 1033, in asarray_chkfinite
    "array must not contain infs or NaNs")

ValueError: array must not contain infs or NaNs

这个问题对数据和方法非常具体,我无法生成最小的可重复工作示例。我不是要求修复我的代码,我只是要求一些头脑风暴:我还没有检查哪些方面可能导致错误?函数参数应该是什么样的,除了窗口长度必须是比polyorder大的奇数?

我很感激已经出现的讨论,最终有所帮助。

1 个答案:

答案 0 :(得分:1)

如果ValueError: array must not contain infs or NaNs非常小(例如delta),我可以重现错误delta=1e-310。检查您的代码和数据,以确保您为delta传递的值是合理的。