numpy - Smoothing in Python -
this question has answer here:
- pandas: rolling mean time interval 7 answers
i use time averaged view data less noisy when plot it. example, if data taken every 1 minute, have 2 arrays, ts
, ys
. created fs
local averaging of 60 nearest points in ys
. convolution myself calculating average of 60 nearest points, don't use modules numpy
or else.
i have new data ts
bit more sparse. is, miss datapoints , can't take average of 60 nearest points. if independent variable, ts
, in minutes, how calculate hourly average of dependent variable, ys
, create hourly average function, fs
, in python
?
if independent variable, ts, in minutes, how calculate hourly average of dependent variable, ys, create hourly average function, fs, in python?
this complex problem , possible answers vary depending on mean "hourly average".
one approach dealing irregularly-spaced data resample it. resampling done interpolation, , resulting resampled data usable whatever filter method like.
import numpy np import matplotlib.pyplot plt scipy.signal import savgol_filter %matplotlib inline def y(t): # function simulate data return np.sin(t/20.) + 0.05*np.random.randn(len(t)) four_hours = np.arange(240) random_time_points = np.sort(np.random.choice(four_hours, size=30, replace=false)) simulated_data = y(random_time_points) resampled_data = np.interp(four_hours, random_time_points, simulated_data) # here smooth savitzky-golay filter, # use moving avg or else # window-length=61 means smooth on 1-hour (60 minute) window smoothed_data = savgol_filter(resampled_data, window_length=61, polyorder=0) # plot results plt.plot(random_time_points, simulated_data, '.k', four_hours, smoothed_data, '--b', four_hours, y(four_hours), '-g') # save plot plt.savefig('so35038933.png')
the plot shows original "sparse" data (black points), original "true" data (green curve), , smoothed data (blue dotted curve).
Comments
Post a Comment