Phân Tích Chuỗi ThờI Gian Trong Python: Bộ Công Cụ Đầy Đủ Với Prophet, sktime, ARIMA và Darts
Hướng dẫn sử dụng Prophet, sktime, statsmodels ARIMA và Darts để phân tích chuỗI thờI gian trong Python. So sánh thư viện và xây dựng pipeline dự báo.
- MIT
- Cập nhật 2026-05-18
{</* resource-info */>}
Phân tích chuỗI thờI gian là một trong những lĩnh vực quan trọng nhất trong khoa học dữ liệu, với ứng dụng từ dự báo doanh thu, dự đoán giá chứng khoán, đến quản lý chuỗI cung ứng. Hệ sinh thái Python cho phân tích chuỗI thờI gian đã phát triển rất mạnh mẽ trong những năm gần đây, với các thư viện chuyên dụng như Prophet, sktime, statsmodels, và Darts.
Bài viết này cung cấp hướng dẫn toàn diện về các công cụ phân tích chuỗI thờI gian trong Python, từ các phương pháp thống kê cổ điển đến deep learning hiện đại, giúp bạn chọn đúng công cụ cho từng loại bài toán.
Bức Tranh Toàn Cảnh Phân Tích ChuỗI ThờI Gian Năm 2024 #
Phân tích chuỗI thờI gian có thể được chia thành ba trường phái chính:
- Phương pháp thống kê cổ điển: ARIMA, Exponential Smoothing, State Space Models — dựa trên lý thuyết thống kê vững chắc, dễ diễn giải
- Machine Learning: Random Forest, Gradient Boosting, SVM áp dụng cho chuỗI thờI gian — linh hoạt, xử lý được nhiều đặc trưng
- Deep Learning: LSTM, Transformer, N-BEATS, N-HiTS — mạnh mẽ với patterns phức tạp, cần nhiều dữ liệu
Các tác vụ chính trong phân tích chuỗI thờI gian bao gồm:
- Forecasting (dự báo): Dự đoán giá trị tương lai
- Classification (phân loại): Gán nhãn cho toàn bộ chuỗI
- Anomaly detection (phát hiện bất thường): Tìm điểm khác biệt trong chuỗI
- Clustering (phân nhóm): Nhóm các chuỗI tương tự nhau
Prophet: Công Cụ Dự Báo CủA Facebook #
Prophet là thư viện dự báo chuỗI thờI gian mã nguồn mở do Facebook (Meta) phát triển, ra mắt năm 2017. Prophet sử dụng mô hình hồi quy cộng dồn (additive regression model) với ba thành phần chính:
y(t) = g(t) + s(t) + h(t) + ε(t)
Trong đó:
- g(t): Trend (xu hướng tuyến tính hoặc logistic)
- s(t): Seasonality (tính mùa vụ hàng năm, hàng tuần, hàng ngày)
- h(t): Holidays (tác động của các ngày lễ)
- ε(t): Nhiễu (error term)
Cách Sử Dụng Prophet #
from prophet import Prophet
import pandas as pd
# Chuẩn bị dữ liệu (bắt buộc có cột 'ds' và 'y')
df = pd.read_csv('sales.csv')
df['ds'] = pd.to_datetime(df['date'])
df['y'] = df['revenue']
# Khởi tạo và huấn luyện
model = Prophet(
yearly_seasonality=True,
weekly_seasonality=True,
daily_seasonality=False
)
model.fit(df)
# Tạo dữ liệu tương lai
future = model.make_future_dataframe(periods=365)
forecast = model.predict(future)
# Vẽ kết quả
fig = model.plot(forecast)
fig_components = model.plot_components(forecast)
Cấu Hình Nâng Cao #
Prophet cho phép tùy chỉnh sâu:
- Custom seasonality: Thêm tính mùa vụ tùy chỉnh (ví dụ: mùa vụ hàng quý)
- Holiday effects: Định nghĩa các ngày lễ đặc biệt ảnh hưởng đến dữ liệu
- Changepoint detection: Tự động phát hiện các điểm thay đổi xu hướng
- Multiplicative mode: Chuyển sang mô hình nhân thay vì cộng khi tính mùa vụ tăng theo trend
# Thêm ngày lễ tùy chỉnh
holidays = pd.DataFrame({
'holiday': 'tet_holiday',
'ds': pd.to_datetime(['2024-02-10', '2025-01-29', '2026-02-17']),
'lower_window': -2,
'upper_window': 3,
})
model = Prophet(holidays=holidays, seasonality_mode='multiplicative')
Khi Nào Chọn Prophet? #
- Dự báo doanh thu, traffic với tính mùa vụ rõ ràng
- Dữ liệu có missing values và outliers (Prophet rất robust)
- Cần kết quả nhanh với ít parameter tuning
- Non-programmers có thể sử dụng qua R hoặc Python API đơn giản
sktime: Framework Thống Nhất Cho ChuỗI ThờI Gian #
sktime là thư viện cung cấp API tương thích scikit-learn cho tất cả các tác vụ chuỗI thờI gian. Được phát triển bởi cộng đồng academic, sktime cho phép xây dựng pipelines phức tạp với cùng patterns quen thuộc từ scikit-learn.
Điểm Mạnh CủA sktime #
- Unified interface: Cùng một API cho forecasting, classification, regression, và clustering
- Composable pipelines: Kết hợp các bước giống như scikit-learn Pipeline
- Transformers chuyên dụng: Hàng trăm transformer cho chuỗI thờI gian
- Extensive model zoo: Tích hợp nhiều mô hình từ các thư viện khác nhau
from sktime.forecasting.model_selection import temporal_train_test_split
from sktime.forecasting.ardl import ARDL
from sktime.forecasting.compose import TransformedTargetForecaster
from sktime.transformations.series.detrend import Deseasonalizer
from sktime.forecasting.base import ForecastingHorizon
# Chia dữ liệu theo thờI gian (không shuffle!)
y_train, y_test = temporal_train_test_split(y, test_size=24)
# Tạo forecasting pipeline
forecaster = TransformedTargetForecaster([
("deseasonalize", Deseasonalizer(model="multiplicative")),
("forecast", ARDL(lags=12)),
])
# Huấn luyện
forecaster.fit(y_train)
# Dự báo
fh = ForecastingHorizon(y_test.index, is_relative=False)
y_pred = forecaster.predict(fh)
sktime Pipelines Và Model Composition #
sktime cung cấp nhiều strategies để biến đổi bài toán forecasting:
- Direct reduction: Huấn luyện một mô hình riêng cho mỗi forecasting horizon
- Recursive reduction: Dùng dự báo tại t-1 làm input cho t
- Multioutput reduction: Huấn luyện một mô hình dự báo nhiều bước cùng lúc
from sktime.forecasting.compose import make_reduction
from sklearn.ensemble import GradientBoostingRegressor
# Biến ML regressor thành forecaster
regressor = GradientBoostingRegressor()
forecaster = make_reduction(
regressor,
strategy="recursive", # Hoặc "direct", "multioutput"
window_length=12
)
Khi Nào Chọn sktime? #
- Đã quen thuộc với scikit-learn
- Cần xây dựng pipelines phức tạp
- Bài toán không chỉ forecasting (classification, clustering)
- Ứng dụng nghiên cứu, học thuật
statsmodels: Nền Tảng Thống Kê Cổ Điển #
statsmodels là thư viện cốt lõi cho thống kê trong Python, cung cấp các mô hình chuỗI thờI gian kinh điển với nền tảng lý thuyết vững chắc.
Các Mô Hình Chính #
from statsmodels.tsa.arima.model import ARIMA
from statsmodels.tsa.statespace.sarimax import SARIMAX
from statsmodels.tsa.holtwinters import ExponentialSmoothing
from statsmodels.tsa.seasonal import seasonal_decompose
from statsmodels.tsa.stattools import adfuller, acf, pacf
# Kiểm định stationarity (ADF test)
adf_result = adfuller(time_series)
print(f'ADF Statistic: {adf_result[0]:.4f}')
print(f'p-value: {adf_result[1]:.4f}')
# Phân rã mùa vụ
decomposition = seasonal_decompose(time_series, model='multiplicative', period=12)
# ARIMA(p=1, d=1, q=1)
model = ARIMA(time_series, order=(1, 1, 1))
result = model.fit()
print(result.summary())
# Dự báo
forecast = result.forecast(steps=12)
# SARIMA với tính mùa vụ
model = SARIMAX(time_series, order=(1, 1, 1), seasonal_order=(1, 1, 1, 12))
result = model.fit()
Các Công Cụ Phân Tích #
- ADF test: Kiểm định Augmented Dickey-Fuller cho stationarity
- KPSS test: Kiểm định stationarity bổ sung
- ACF/PACF: Autocorrelation và Partial Autocorrelation Function plots
- VAR: Vector Autoregression cho multivariate time series
Khi Nào Chọn statsmodels? #
- Cần nền tảng lý thuyết thống kê vững chắc
- Mô hình cần interpretable (diễn giải được)
- Nghiên cứu học thuật yêu cầu citations thống kê
- Dữ liệu không quá lớn (statsmodels không tối ưu cho big data)
Darts: Deep Learning Hiện Đại Cho ChuỗI ThờI Gian #
Darts là thư viện do Unit8 phát triển, mang các mô hình deep learning hiện đại vào phân tích chuỗI thờI gian. Darts hỗ trợ cả mô hình cổ điển và state-of-the-art deep learning.
Các Mô Hình Deep Learning Trong Darts #
| Mô hình | Mô tả | Phù hợp cho |
|---|---|---|
| N-BEATS | Neural Basis Expansion | Dữ liệu univariate, pattern đơn giản |
| N-HiTS | Neural Hierarchical Interpolation | Multivariate, nhiều tần suất |
| TFT | Temporal Fusion Transformer | Explanable forecasting, nhiều covariates |
| DeepAR | Autoregressive RNN | Probabilistic forecasting |
| TiDE | Time series Dense Encoder | Long-horizon forecasting |
| N-HiTS | Hierarchical Interpolation | Multivariate với nhiều seasonality |
from darts.models import NBEATSModel, TFTModel
from darts import TimeSeries
from darts.metrics import mape, rmse
# Chuẩn bị dữ liệu Darts
series = TimeSeries.from_dataframe(df, 'date', 'value')
train, test = series.split_before(0.8)
# N-BEATS
model = NBEATSModel(
input_chunk_length=24,
output_chunk_length=12,
n_epochs=100,
batch_size=32,
model_name="nbeats_model"
)
model.fit(train, verbose=True)
prediction = model.predict(n=12)
# TFT (Temporal Fusion Transformer) — probabilistic forecasting
model = TFTModel(
input_chunk_length=24,
output_chunk_length=12,
add_relative_index=True,
likelihood="quantile",
random_state=42
)
model.fit(train, future_covariates=covariates, verbose=True)
prediction = model.predict(n=12, num_samples=100) # Probabilistic
Probabilistic Forecasting Và Backtesting #
Darts nổi bật với khả năng probabilistic forecasting — dự báo không chỉ điểm mà cả khoảng tin cậy. Điều này cực kỳ quan trọng trong quyết định kinh doanh.
from darts.backtesting import backtest
# Backtesting với historical forecasts
historical_forecasts = model.historical_forecasts(
series,
start=0.5,
forecast_horizon=12,
stride=1,
retrain=False
)
# Tính metrics
error = rmse(test, prediction)
print(f"RMSE: {error:.2f}")
Khi Nào Chọn Darts? #
- Patterns phức tạp cần deep learning
- Dự báo multivariate (nhiều chuỗI liên quan)
- Cần probabilistic forecasting (khoảng tin cậy)
- Có GPU để tăng tốc training
Feature Engineering Cho ChuỗI ThờI Gian #
Feature engineering là bước quan trọng nhất trong phân tích chuỗI thờI gian. Các đặc trưng phổ biến bao gồm:
Các Loại Features #
- Lag features: Giá trị tại các thờI điểm trước đó (t-1, t-7, t-30)
- Rolling statistics: Mean, std, min, max trong cửa sổ trượt
- Datetime features: Giờ, ngày trong tuần, tháng, quý, năm
- Fourier terms: Sin/cos biến đổi cho tính mùa vụ
import pandas as pd
import numpy as np
def create_time_features(df, target_col):
"""Tạo features cho time series"""
df = df.copy()
# Lag features
for lag in [1, 7, 14, 30]:
df[f'lag_{lag}'] = df[target_col].shift(lag)
# Rolling statistics
for window in [7, 14, 30]:
df[f'rolling_mean_{window}'] = df[target_col].shift(1).rolling(window).mean()
df[f'rolling_std_{window}'] = df[target_col].shift(1).rolling(window).std()
# Datetime features
df['dayofweek'] = df.index.dayofweek
df['month'] = df.index.month
df['quarter'] = df.index.quarter
df['year'] = df.index.year
df['dayofyear'] = df.index.dayofyear
# Fourier terms for seasonality (annual)
df['sin_365'] = np.sin(2 * np.pi * df.index.dayofyear / 365.25)
df['cos_365'] = np.cos(2 * np.pi * df.index.dayofyear / 365.25)
return df
Tích Hợp tsfresh #
tsfresh là thư viện tự động trích xuất hàng trăm features từ chuỗI thờI gian:
from tsfresh import extract_features
from tsfresh.utilities.dataframe_functions import impute
features = extract_features(
df,
column_id='series_id',
column_sort='timestamp',
column_value='value',
default_fc_parameters='efficient' # Hoặc 'comprehensive'
)
features = impute(features) # Xử lý NaN
Tránh Data Leakage #
Điểm quan trọng nhất trong feature engineering chuỗI thờI gian là không sử dụng future information để tạo features. Luôn đảm bảo:
- Chỉ sử dụng dữ liệu quá khứ để tạo features cho dự báo hiện tại
- Sử dụng
shift(1)trước khi tính rolling statistics - Chia train/test theo thờI gian, không bao giờ shuffle
Bảng So Sánh Toàn Diện #
| Tiêu chí | Prophet | sktime | statsmodels | Darts |
|---|---|---|---|---|
| Loại mô hình | Additive regression | Unified framework | Classical statistical | Deep learning + classical |
| Dễ sử dụng | Rất cao | Trung bình | Trung bình | Trung bình |
| Scalability | Tốt | Tốt | Hạn chế | Tốt (GPU support) |
| Probabilistic output | Có (MCMC) | Phụ thuộc model | Có | Có (sampling) |
| Multivariate | Không | Có | VAR model | Có (native) |
| GPU support | Không | Không | Không | Có (PyTorch) |
| Interpretability | Cao | Phụ thuộc model | Rất cao | Thấp-Trung bình |
| Production ready | Cao | Trung bình | Cao | Cao |
| Seasonality handling | Xuất sắc | Tốt | Tốt | Phụ thuộc model |
| Best for | Business forecasting | Pipeline building | Statistical rigor | Complex patterns |
Xây Dựng Pipeline Dự Báo End-to-End #
Workflow Hoàn Chỉnh #
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ Data │──▶│ Preprocessing │──▶│ Feature │
│ Exploration │ │ & Cleaning │ │ Engineering │
└─────────────────┘ └──────────────────┘ └────────┬────────┘
│
┌─────────────────┐ ┌──────────────────┐ ┌───────▼─────────┐
│ Deployment │◀──│ Model Selection │◀──│ Model Training │
│ & Monitoring │ │ & Evaluation │ │ & Tuning │
└─────────────────┘ └──────────────────┘ └─────────────────┘
Code Pipeline Hoàn Chỉnh #
import pandas as pd
import numpy as np
from prophet import Prophet
from sklearn.metrics import mean_absolute_error, mean_squared_error
# 1. Load và khám phá dữ liệu
df = pd.read_csv('daily_sales.csv', parse_dates=['date'], index_col='date')
print(df.describe())
print(df.isnull().sum())
# 2. Preprocessing
df = df.fillna(method='ffill') # Forward fill missing values
df = df.asfreq('D') # Đảm bảo tần suất hàng ngày
# 3. Feature engineering (tùy chọn, Prophet tự động xử lý nhiều)
# Prophet không cần nhiều feature engineering thủ công
# 4. Chia train/test (theo thờI gian!)
train = df.iloc[:-365] # Trừ 1 năm cuối
test = df.iloc[-365:]
# 5. Huấn luyện model
model = Prophet(
yearly_seasonality=True,
weekly_seasonality=True,
changepoint_prior_scale=0.05
)
model.fit(train.reset_index().rename(columns={'date': 'ds', 'sales': 'y'}))
# 6. Dự báo
future = model.make_future_dataframe(periods=365)
forecast = model.predict(future)
predictions = forecast[['ds', 'yhat']].tail(365).set_index('ds')
# 7. Đánh giá
mae = mean_absolute_error(test['sales'], predictions['yhat'])
rmse = np.sqrt(mean_squared_error(test['sales'], predictions['yhat']))
mape = np.mean(np.abs((test['sales'] - predictions['yhat']) / test['sales'])) * 100
print(f"MAE: {mae:.2f}")
print(f"RMSE: {rmse:.2f}")
print(f"MAPE: {mape:.2f}%")
# 8. Backtesting
# Sử dụng cross-validation của Prophet
from prophet.diagnostics import cross_validation, performance_metrics
df_cv = cross_validation(model, initial='730 days', period='180 days', horizon='365 days')
df_p = performance_metrics(df_cv)
print(df_p.head())
Các Sai Lầm Thường Gặp Và Best Practices #
Tránh Data Leakage #
Sai lầm phổ biến nhất là sử dụng future information trong features. Các dấu hiệu:
- Train score cao bất thường so với test score
- Sử dụng
StandardScaler.fit_transform()trên toàn bộ dữ liệu trước khi chia train/test - Shuffling dữ liệu chuỗI thờI gian
# ❌ SAI: Fit trên toàn bộ dữ liệu
scaler = StandardScaler()
X = scaler.fit_transform(X) # Data leakage!
train, test = train_test_split(X) # SAI: Shuffle time series
# ✅ ĐÚNG: Fit chỉ trên train data
train, test = temporal_train_test_split(X, test_size=0.2)
scaler = StandardScaler()
train_scaled = scaler.fit_transform(train)
test_scaled = scaler.transform(test) # Chỉ transform, không fit
Cross-Validation Cho ChuỗI ThờI Gian #
Không sử dụng K-fold cross-validation thông thường. Sử dụng walk-forward validation hoặc expanding window:
from sklearn.model_selection import TimeSeriesSplit
tscv = TimeSeriesSplit(n_splits=5)
for train_idx, test_idx in tscv.split(X):
X_train, X_test = X[train_idx], X[test_idx]
# Huấn luyện và đánh giá
Xử Lý Timestamps Không Đều #
- Resampling:
df.resample('D').mean()để đảm bảo tần suất đều - Interpolation:
df.interpolate(method='linear')cho missing values - Irregular data: Sử dụng Darts hoặc sktime có hỗ trợ timestamps không đều
Chọn Forecast Horizon Phù Hợp #
- Short-term (1-7 ngày): Các mô hình đơn giản, ARIMA, Exponential Smoothing
- Medium-term (1-12 tháng): Prophet, Gradient Boosting với features tốt
- Long-term (1-5 năm): Prophet, TFT, hoặc kết hợp nhiều mô hình
FAQ #
Nên Dùng Prophet Hay ARIMA Cho Dự Báo? #
Prophet phù hợp hơn cho hầu hết các trường hợp thực tế vì:
- Không cần kiểm tra stationarity
- Xử lý tự động missing values và outliers
- Dễ dàng thêm holidays và custom seasonality
- API đơn giản, ít parameter tuning
ARIMA phù hợp khi:
- Cần nền tảng lý thuyết vững chắc để diễn giải
- Dữ liệu đã stationary hoặc dễ dàng làm stationary
- Cần mô hình lightweight, chạy nhanh
sktime Có Xử Lý Multivariate Time Series Không? #
Có. sktime hỗ trợ multivariate forecasting thông qua:
- VAR (Vector Autoregression): Mô hình thống kê cho multivariate
- DirectMultiVariateForecaster: Wrapper cho scikit-learn estimators
- Hierarchical forecasting: Dự báo theo cấu trúc phân cấp
Darts Có Tốt Hơn Prophet Cho Deep Learning? #
Darts và Prophet phục vụ mục đích khác nhau:
- Prophet là mô hình statistical cho dữ liệu có tính mùa vụ rõ ràng
- Darts là framework deep learning cho patterns phức tạp
Nếu dữ liệu có nhiều covariates, patterns phi tuyến phức tạp, và đủ lớn — Darts với TFT hoặc N-HiTS thường cho kết quả tốt hơn. Nếu dữ liệu có tính mùa vụ rõ ràng, ít noise — Prophet có thể đủ tốt với ít effort hơn.
Làm Thế Nào Tránh Data Leakage Trong Time Series? #
Ba nguyên tắc vàng:
- Không bao giờ shuffle — Luôn chia theo thờI gian
- Fit transformers trên train only — StandardScaler, PCA, v.v.
- Lag features phải shift đủ — Đảm bảo không dùng future information
# Kiểm tra: Nếu correlation giữa features và target quá cao (>0.95),
# có thể đã có data leakage
correlation = X_train.corrwith(y_train)
print(correlation[correlation.abs() > 0.95])
Thư Viện Nào Tốt Nhất Cho Real-time Forecasting? #
- statsmodels ExponentialSmoothing: Nhanh, lightweight, phù hợp microservices
- Prophet: Chậm hơn nhưng chính xác, phù hợp batch predictions
- Darts N-BEATS/N-HiTS: Cần GPU, phù hợp nếu đã có infrastructure
- River: Thư viện chuyên dụng cho online learning và real-time forecasting
Kết Luận #
Hệ sinh thái Python cho phân tích chuỗI thờI gian cung cấp đầy đủ công cụ cho mọi nhu cầu:
- Prophet: Bắt đầu với dự báo kinh doanh, tính mùa vụ, holidays
- sktime: Xây dựng pipelines phức tạp, tương thích scikit-learn
- statsmodels: Nền tảng thống kê vững chắc, interpretable
- Darts: Deep learning cho patterns phức tạp, probabilistic forecasting
Chiến lược phù hợp nhất là bắt đầu đơn giản — Prophet cho dự báo cơ bản, sau đó chuyển sang Darts hoặc sktime khi cần xử lý phức tạp hơn. Luôn nhớ rằng feature engineering tốt quan trọng hơn chọn model fancy — “garbage in, garbage out” vẫn đúng trong thờI đại deep learning.
Tài Liệu Tham Khảo #
- Prophet Documentation — Tài liệu chính thức Prophet
- sktime Documentation — Hướng dẫn và API reference
- Darts GitHub Repository — Mã nguồn và examples
- statsmodels Documentation — Thư viện thống kê Python
- Pandas Documentation — Xử lý dữ liệu chuỗI thờI gian
Hạ Tầng Đề Xuất #
Để chạy các công cụ trên 24/7 ổn định, lựa chọn hạ tầng rất quan trọng:
- DigitalOcean — $200 tín dụng miễn phí 60 ngày, 14+ region toàn cầu.
- HTStack — VPS Hong Kong, độ trễ thấp. dibi8.com cũng host ở đây.
- Hostinger — VPS giá tốt cho thị trường Việt Nam.
Đây là affiliate link, không tăng chi phí của bạn nhưng giúp dibi8.com duy trì hoạt động.
💬 Bình luận & Thảo luận