Home >Backend Development >Python Tutorial >How to Effectively Smoothen Noisy Data Curves?
Optimally Smoothing Noisy Curves
Consider a dataset approximated by:
import numpy as np x = np.linspace(0, 2*np.pi, 100) y = np.sin(x) + np.random.random(100) * 0.2
This includes 20% variation. Approaches like UnivariateSpline and moving averages present limitations.
Savitzky-Golay Filter
An effective solution is the Savitzky-Golay filter, available in scipy. It uses least squares regression to estimate the value at the center of a small window using a polynomial. The window then shifts to repeat the process, resulting in optimized adjustment of each point.
import numpy as np import matplotlib.pyplot as plt from scipy.signal import savgol_filter x = np.linspace(0, 2*np.pi, 100) y = np.sin(x) + np.random.random(100) * 0.2 yhat = savgol_filter(y, 51, 3) # window size 51, polynomial order 3 plt.plot(x,y) plt.plot(x,yhat, color='red') plt.show()
The above is the detailed content of How to Effectively Smoothen Noisy Data Curves?. For more information, please follow other related articles on the PHP Chinese website!