Simple Moving Average
ã¡ãã¥ãŒã衚瀺ããã«ã¯ã¹ã¯ã€ãããŠãã ãã
Mathematically, the simple moving average model is represented as follows:
In this equation, the prediction SMA consists of variables, where k is the window size (how many past values we will take to calculate the next value), and p is the value taken.
How does this model work? In fact, the simple moving average at each prediction moment calculates the average of several past values - the resulting number is the next prediction. You can imagine a graph on the path of which a window of size n moves (you can take 2 values, you can take 3, etc.). The smaller the window, the smoother the predictions. Let's demonstrate it below:
In the first image, the window size for which the average value is calculated is 8, while on the second graph, n = 3. The smaller the window, the fewer peaks it can capture.
Python allows you to implement this model like this:
import matplotlib.pyplot as plt
import pandas as pd
# Calculate predictions
t_average = df["value"].rolling(window=8).mean()
# Plot results
plt.figure(figsize=(10, 5))
plt.plot(df["value"][:50], "k-", label="Original")
plt.plot(t_average[:50], "r-", label="Running average")
plt.ylabel("NO2")
plt.xlabel("Date")
plt.grid(linestyle=":")
plt.legend(loc="upper left")
plt.show()
rolling() - a function used to calculate the moving average.
The simple moving average is one of the simplest models, so it is enough to use the pandas library to implement it.
ã¹ã¯ã€ãããŠã³ãŒãã£ã³ã°ãéå§
Make predictions for dataset pr_air_quality.csv with window size 5.
- Convert the
"date.utc"column todatetimetype. - Calculate moving averages using moving windows with the size of
5. - Compare the results on a plot: visualize the first 50 values of the
"value"column of thedfwithin the first call of the.plot()function and the first 50 values of thepredwithin the second call. - Display the legend and the plot.
è§£ç
ãã£ãŒãããã¯ããããšãããããŸãïŒ
single
AIã«è³ªåãã
AIã«è³ªåãã
äœã§ã質åããããææ¡ããã質åã®1ã€ã詊ããŠãã£ãããå§ããŠãã ãã