single
Highlighting Trends and Anomalies
Veeg om het menu te tonen
1234567891011121314151617181920212223242526272829import pandas as pd import numpy as np import matplotlib.pyplot as plt # Generate example time series data np.random.seed(0) dates = pd.date_range(start="2022-01-01", periods=60, freq="D") values = np.cumsum(np.random.normal(loc=0.5, scale=2, size=60)) series = pd.Series(values, index=dates) # Calculate a trend line using rolling mean trend = series.rolling(window=7, center=True).mean() # Identify anomalies: points that deviate from the trend by more than 1.5 standard deviations deviation = np.abs(series - trend) threshold = 1.5 * deviation.std() anomalies = series[deviation > threshold] # Plot the time series, trend line, and highlight anomalies plt.figure(figsize=(12, 6)) plt.plot(series.index, series.values, label="Original Series") plt.plot(trend.index, trend.values, color="orange", linewidth=2, label="Trend (7-day MA)") plt.scatter(anomalies.index, anomalies.values, color="red", label="Anomalies", zorder=5) plt.title("Time Series with Trend Line and Anomalies Highlighted") plt.xlabel("Date") plt.ylabel("Value") plt.legend() plt.tight_layout() plt.show()
When analyzing time series data, visual techniques are crucial for understanding the underlying structure and for detecting unusual patterns. One common approach to reveal the overall direction of a time series is to add a trend line. A trend line, such as a moving average, smooths out short-term fluctuations and highlights longer-term patterns or shifts in the data. This can help you quickly assess whether values are generally increasing, decreasing, or remaining stable over time.
Detecting anomalies—points that deviate significantly from the expected pattern—is another important aspect of time series analysis. Anomalies may indicate errors, rare events, or important changes in the process being measured. Visually, anomalies can be highlighted on a plot by marking points that differ from the trend by more than a certain threshold, such as a multiple of the standard deviation. This makes it easier to spot outliers and investigate their causes.
By combining trend lines and anomaly highlighting in your visualizations, you gain deeper insights into the behavior of your time series data, making it easier to identify patterns, forecast future movements, and detect unexpected events.
Swipe to start coding
Plotting trends is a key step in time series analysis. Your task is to plot a time series and overlay a trend line using a moving average.
- Calculate a 5-day centered moving average for the input
seriesand store it in a variable calledtrend. - Plot the original
seriesas a line chart. - Plot the
trendas a line chart on the same figure. - Add an x-axis label, a y-axis label, a title, and a legend.
- Ensure the plot is displayed using
plt.show().
Oplossing
Bedankt voor je feedback!
single
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.