![]() ![]() You need to test a few values to determine the best balance for your particular use case.Ī good exercise to get a feel for this is to take the code example above and add some noise to the sine wave. The larger the window size, the greater the lags of the peaks and the troughs but the smoother the data. Running the above code produces the following plot in a new window: > plt.plot(x, average_y, 'r.-', label='Running average') > plt.plot(x, y, 'k.-', label='Original data') Now, we can plot the results using matplotlib: Here’s how to add NaNs to the start of the running average to ensure the list has the same length as the original data: To demonstrate this, we can create a sine wave and calculate a running average in Python like we have done earlier: ![]() This is important to keep in mind if you want to identify when a peak in the data has happened and what its magnitude is. The magnitude of the values is also different from the real data. Plotting a Running Average in matplotlibĪs a consequence of this method for smoothing data, the features (e.g., peaks or troughs) in a graph of a moving average lag the real features in the original data. A convenient way to do this is by inserting a NaN at the start of the list using list.insert(). If you want to compare the running average to the original data, you have to align them correctly. Notice the loop is over len(data) – window + 1, which means our smoothed data has only 9 data points. The index then gets advanced with a for loop, and we repeat. Then, we use NumPy to calculate the mean value. Here, we define a window size of 2 data points and use a list slice to get the subset of data we want to average. > for ind in range(len(data) – window + 1): To demonstrate this, let’s define some data and calculate a running average in Python in a for loop: ![]() We define a window, calculate an average in the window, slide the window by one data point, and repeat until we get to the end. This can be any number from 2 to n-1, where n is the number of data points in the time series. To generate a running average, we need to decide on a window size in which to calculate the average values. For more material that builds on top of that, take a look at this data science track. If you’re looking for an introduction to data science, we have a course that provides the foundational skills. This article is aimed at people with a bit of experience in data analysis. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |