简体   繁体   中英

Built-in function scipy.signal.savgol_filter returns error

I am trying to filter a data set using the scipy.signal.savgol_filter function, but I get the error

TypeError: expected x and y to have same length

when I try to assign the data in a new list.

What is stranger is that just before my last computation everything worked as intended, but now I get this. I have tried to create an empty list the size of the filtered one, but I get the same error.

Here is that part of my code:

import scipy as sc
import scipy.signal
Cf = sc.signal.savgol_filter(x=C, window_length=299, polyorder=3)

where C is a well defined list of float type numbers.

I get the error that you reported if the window_length is greater than the length of C (eg savgol_filter([3, 1, 4, 1, 5, 9], window_length=7, polyorder=3) ).

In SciPy 1.0.0, the cryptic error message has been replaced with a more informative message:

ValueError: If mode is 'interp', window_length must be less than
or equal to the size of x.

The error message is confusing. In my case the problem was with the input array. It was in the following form:

x = np.array([[3], 
              [1],
              [4],
              [1], 
              [5], 
              [9]])

Supplying it flattened by np.ravel solved the problem:

>>> np.ravel(x)
array([3, 1, 4, 1, 5, 9])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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