Chapter 10: Advanced Topics & Projects / Lesson 47

Time Series Analysis

What is Time Series Analysis?

Time series analysis involves analyzing data points collected over time to identify patterns, trends, and make predictions. Unlike traditional ML where data points are independent, time series data points are dependent on previous values, making temporal relationships crucial.

Time series data appears in many domains: stock prices, weather patterns, sales data, sensor readings, and more. Understanding how to analyze and forecast time series is essential for many real-world applications.

Time Series Data Structure
import pandas as pd import numpy as np # Create time series data dates = pd.date_range('2024-01-01', periods=100, freq='D') values = 100 + np.cumsum(np.random.randn(100)) # Trend + noise ts_data = pd.Series(values, index=dates) print("Time Series Data:") print(ts_data.head()) print(f"\\nData points: {len(ts_data)}") print(f"Date range: {ts_data.index[0]} to {ts_data.index[-1]}")

Key Components of Time Series

Time series can be decomposed into several components:

  • Trend: Long-term increase or decrease in the data
  • Seasonality: Regular patterns that repeat at fixed intervals (daily, weekly, yearly)
  • Cyclical: Irregular cycles without fixed periods
  • Noise/Random: Random variation that cannot be explained
Time Series Visualization
import matplotlib.pyplot as plt import pandas as pd import numpy as np # Generate sample time series with trend and seasonality dates = pd.date_range('2024-01-01', periods=365, freq='D') trend = np.linspace(100, 150, 365) seasonality = 10 * np.sin(2 * np.pi * np.arange(365) / 365.25 * 4) # 4 cycles per year noise = np.random.randn(365) * 5 values = trend + seasonality + noise ts = pd.Series(values, index=dates) print(f"Mean: {ts.mean():.2f}") print(f"Std: {ts.std():.2f}") print("Time series has trend, seasonality, and noise")

Time Series Forecasting Models

Various models are used for time series forecasting:

  • ARIMA (AutoRegressive Integrated Moving Average): Classic statistical model for stationary time series
  • LSTM/RNN: Deep learning models that capture long-term dependencies
  • Prophet: Facebook's forecasting tool that handles seasonality automatically
  • Exponential Smoothing: Weighted averages of past observations
Simple Time Series Forecasting with LSTM
from tensorflow import keras from tensorflow.keras import layers import numpy as np # Prepare time series data for LSTM def create_sequences(data, seq_length): X, y = [], [] for i in range(len(data) - seq_length): X.append(data[i:i+seq_length]) y.append(data[i+seq_length]) return np.array(X), np.array(y) # Sample time series data = np.sin(np.arange(0, 100, 0.1)) + np.random.randn(1000) * 0.1 seq_length = 10 X, y = create_sequences(data, seq_length) print(f"Input sequences shape: {X.shape}") print(f"Target values shape: {y.shape}") print("Ready for LSTM training!")

Preprocessing Time Series

Important preprocessing steps for time series:

  • Stationarity: Ensure data is stationary (constant mean and variance) using differencing
  • Normalization: Scale data to help model training
  • Handling Missing Values: Forward fill, interpolation, or removal
  • Feature Engineering: Create lag features, rolling statistics, time-based features

💡 Stationarity Importance

Many time series models assume stationarity. Use differencing (subtracting previous value) or log transformations to make non-stationary series stationary before modeling!

Practical Applications

Time series analysis is crucial for:

  • Financial Forecasting: Stock prices, currency exchange rates, economic indicators
  • Sales & Demand Forecasting: Retail sales, inventory management, supply chain
  • Weather Prediction: Temperature, rainfall, climate modeling
  • IoT & Sensors: Monitoring equipment, predictive maintenance
  • Web Analytics: Website traffic, user behavior patterns

Exercise: Time Series Forecasting

In the exercise on the right, you'll prepare time series data for forecasting, create sequences for LSTM training, and build a simple forecasting model. You'll learn how to structure temporal data for machine learning.

This hands-on exercise will help you understand the fundamental steps in time series forecasting.

🎉

Lesson Complete!

Great work! Continue to the next lesson.

main.py
📤 Output
Click "Run" to execute...