Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Highlighting Trends and Anomalies | Section
Mastering Time Series Fundamentals
Sektion 1. Kapitel 10
single

single

bookHighlighting Trends and Anomalies

Stryg for at vise menuen

1234567891011121314151617181920212223242526272829
import 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()
copy

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.

Opgave

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 series and store it in a variable called trend.
  • Plot the original series as a line chart.
  • Plot the trend as 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().

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 10
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt