使用機(jī)器學(xué)習(xí)技術(shù)進(jìn)行時(shí)間序列缺失數(shù)據(jù)填充:基礎(chǔ)方法與入門案例
在時(shí)間序列分析領(lǐng)域中,數(shù)據(jù)缺失是一個(gè)不可避免的挑戰(zhàn)。無論是由于傳感器故障、數(shù)據(jù)傳輸中斷還是設(shè)備維護(hù)等原因,這些缺失都會(huì)對(duì)數(shù)據(jù)分析和預(yù)測(cè)造成顯著影響。傳統(tǒng)的處理方法,如前向填充或簡(jiǎn)單插值,雖然實(shí)現(xiàn)簡(jiǎn)單,但在處理復(fù)雜數(shù)據(jù)時(shí)往往表現(xiàn)不足。
具體來說,當(dāng)時(shí)間序列具有以下特征時(shí),傳統(tǒng)方法的局限性就會(huì)顯現(xiàn):
- 存在復(fù)雜的非線性模式
- 包含多層次的趨勢(shì)變化
- 數(shù)據(jù)波動(dòng)性較大
本文將通過實(shí)際案例,詳細(xì)探討如何運(yùn)用機(jī)器學(xué)習(xí)技術(shù)來解決時(shí)間序列的缺失值問題。
數(shù)據(jù)說明
為了確保研究的可重復(fù)性,我們構(gòu)建了一個(gè)模擬的能源生產(chǎn)數(shù)據(jù)集。這個(gè)數(shù)據(jù)集具有以下特征:
- 時(shí)間范圍:2023年1月1日至2023年3月1日
- 采樣頻率:10分鐘
- 數(shù)據(jù)特點(diǎn):包含真實(shí)的晝夜能源生產(chǎn)周期
- 缺失設(shè)置:隨機(jī)選擇10%的數(shù)據(jù)點(diǎn)作為缺失值
讓我們首先看看如何生成這個(gè)數(shù)據(jù)集:
import pandas as pd
import numpy as np
from datetime import datetime
import matplotlib.pyplot as plt
# 生成模擬能源生產(chǎn)數(shù)據(jù)
start_date = datetime(2023, 1, 1)
end_date = datetime(2023, 3, 1)
datetime_index = pd.date_range(start=start_date, end=end_date, freq='10T')
# 創(chuàng)建具有晝夜周期的能源生產(chǎn)值
np.random.seed(42) # 設(shè)置隨機(jī)種子以確??芍貜?fù)性
base_energy = []
for dt in datetime_index:
hour = dt.hour
if 6 <= hour <= 18: # 白天時(shí)段:較高的能源生產(chǎn)
energy = np.random.normal(loc=300, scale=30)
else: # 夜間時(shí)段:較低的能源生產(chǎn)
energy = np.random.normal(loc=50, scale=15)
base_energy.append(energy)
energy_production = pd.Series(base_energy)
# 隨機(jī)引入缺失值
num_missing = int(0.1 * len(energy_production))
missing_indices = np.random.choice(len(energy_production), num_missing, replace=False)
energy_production.iloc[missing_indices] = np.nan
# 創(chuàng)建數(shù)據(jù)框架
mock_energy_data_with_missing = pd.DataFrame({
'Datetime': datetime_index,
'Energy_Production': energy_production
})
# 添加時(shí)間索引便于后續(xù)分析
data_with_index = mock_energy_data_with_missing.reset_index()
data_with_index['Time_Index'] = np.arange(len(data_with_index))
# 數(shù)據(jù)可視化
plt.figure(figsize=(14, 7))
plt.plot(mock_energy_data_with_missing['Datetime'],
mock_energy_data_with_missing['Energy_Production'],
label='Energy Production (With Missing)', color='blue', alpha=0.7)
plt.scatter(mock_energy_data_with_missing['Datetime'],
mock_energy_data_with_missing['Energy_Production'],
c=mock_energy_data_with_missing['Energy_Production'].isna(),
cmap='coolwarm',
label='Missing Values', s=10)
plt.title('模擬能源生產(chǎn)數(shù)據(jù)集(10分鐘間隔采樣)')
plt.xlabel('時(shí)間')
plt.ylabel('能源生產(chǎn)量')
plt.legend(['能源生產(chǎn)(含缺失值)', '缺失值'])
plt.grid(True)
plt.show()
圖1:模擬能源生產(chǎn)數(shù)據(jù)可視化。藍(lán)線表示能源生產(chǎn)數(shù)據(jù),散點(diǎn)表示缺失值的位置
從上圖中,我們可以清晰地觀察到以下特征:
- 數(shù)據(jù)展現(xiàn)出明顯的周期性波動(dòng),這反映了能源生產(chǎn)的晝夜變化規(guī)律
- 缺失值(散點(diǎn)標(biāo)記)隨機(jī)分布在整個(gè)時(shí)間序列中
- 能源生產(chǎn)量在白天和夜間有顯著的水平差異
這個(gè)數(shù)據(jù)集為我們研究不同補(bǔ)充方法的效果提供了理想的測(cè)試基礎(chǔ)。在接下來的分析中,我們將詳細(xì)探討如何運(yùn)用機(jī)器學(xué)習(xí)方法來補(bǔ)充這些缺失值。
機(jī)器學(xué)習(xí)在時(shí)間序列補(bǔ)充中的應(yīng)用基礎(chǔ)
機(jī)器學(xué)習(xí)方法的優(yōu)勢(shì)
在時(shí)間序列數(shù)據(jù)分析中,機(jī)器學(xué)習(xí)方法相比傳統(tǒng)補(bǔ)充方法具有獨(dú)特優(yōu)勢(shì)。傳統(tǒng)方法通?;诤?jiǎn)單的統(tǒng)計(jì)假設(shè),而機(jī)器學(xué)習(xí)方法則能夠自適應(yīng)地學(xué)習(xí)數(shù)據(jù)中的復(fù)雜模式和多維依賴關(guān)系。
非線性關(guān)系處理:在能源生產(chǎn)等實(shí)際場(chǎng)景中,變量之間往往存在復(fù)雜的非線性關(guān)系。機(jī)器學(xué)習(xí)模型能夠自動(dòng)捕捉這些非線性模式,而無需預(yù)先指定關(guān)系形式。
多維特征利用:當(dāng)數(shù)據(jù)集包含多個(gè)相關(guān)變量時(shí),機(jī)器學(xué)習(xí)模型可以同時(shí)考慮多個(gè)特征的影響,從而提供更準(zhǔn)確的估計(jì)。
大規(guī)模缺失處理:對(duì)于連續(xù)時(shí)間段的缺失,機(jī)器學(xué)習(xí)可以通過學(xué)習(xí)數(shù)據(jù)的長(zhǎng)期模式來提供更可靠的補(bǔ)充值。
異常模式識(shí)別:在處理非隨機(jī)缺失時(shí),機(jī)器學(xué)習(xí)方法表現(xiàn)出較強(qiáng)的魯棒性,能夠識(shí)別并適應(yīng)異常模式。
線性回歸補(bǔ)充方法實(shí)現(xiàn)
我們首先探討線性回歸這一基礎(chǔ)但高效的補(bǔ)充方法。以下是詳細(xì)的實(shí)現(xiàn)步驟:
import pandas as pd
import numpy as np
from datetime import datetime
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
# 第一步:數(shù)據(jù)預(yù)處理
# 將時(shí)間索引作為特征,能源生產(chǎn)量作為目標(biāo)變量
features = data_with_index[['Time_Index']]
target = data_with_index['Energy_Production']
# 第二步:分離完整數(shù)據(jù)和缺失數(shù)據(jù)
non_missing_data = data_with_index.dropna(subset=['Energy_Production'])
missing_data = data_with_index[data_with_index['Energy_Production'].isna()]
# 第三步:模型訓(xùn)練
# 使用完整數(shù)據(jù)訓(xùn)練線性回歸模型
regressor = LinearRegression()
regressor.fit(non_missing_data[['Time_Index']], non_missing_data['Energy_Production'])
# 第四步:缺失值預(yù)測(cè)
predicted_values = regressor.predict(missing_data[['Time_Index']])
# 第五步:將預(yù)測(cè)值填充到原始數(shù)據(jù)集
filled_data = data_with_index.copy()
filled_data.loc[filled_data['Energy_Production'].isna(), 'Energy_Production'] = predicted_values
filled_data = filled_data[['Datetime', 'Energy_Production']]
# 第六步:結(jié)果可視化(展示2023年1月數(shù)據(jù))
start_month = datetime(2023, 1, 1)
end_month = datetime(2023, 1, 31)
original_month_data = mock_energy_data_with_missing[
(mock_energy_data_with_missing['Datetime'] >= start_month) &
(mock_energy_data_with_missing['Datetime'] <= end_month)
]
imputed_month_data = filled_data[
(filled_data['Datetime'] >= start_month) &
(filled_data['Datetime'] <= end_month)
]
plt.figure(figsize=(14, 7))
plt.plot(imputed_month_data['Datetime'], imputed_month_data['Energy_Production'],
label='補(bǔ)充后數(shù)據(jù)', color='green', alpha=0.8)
plt.plot(original_month_data['Datetime'], original_month_data['Energy_Production'],
label='原始數(shù)據(jù)(含缺失)', color='red', alpha=0.9)
plt.title('原始數(shù)據(jù)與線性回歸補(bǔ)充數(shù)據(jù)對(duì)比(2023年1月)')
plt.xlabel('時(shí)間')
plt.ylabel('能源生產(chǎn)量')
plt.legend()
plt.grid(True)
plt.show()
圖2:線性回歸補(bǔ)充效果可視化。綠線表示補(bǔ)充后的數(shù)據(jù),紅線表示原始數(shù)據(jù)
補(bǔ)充效果的多維度評(píng)估
為了全面評(píng)估補(bǔ)充效果,我們需要從多個(gè)維度進(jìn)行分析。以下是詳細(xì)的評(píng)估框架:
from statsmodels.tsa.seasonal import seasonal_decompose
# 統(tǒng)計(jì)特征分析
original_stats = mock_energy_data_with_missing['Energy_Production'].describe()
imputed_stats = filled_data['Energy_Production'].describe()
# 創(chuàng)建比較表
stats_comparison = pd.DataFrame({
'Metric': original_stats.index,
'Original Data': original_stats.values,
'Imputed Data (Linear Regression)': imputed_stats.values
})
# 輸出統(tǒng)計(jì)比較結(jié)果
print("數(shù)據(jù)統(tǒng)計(jì)特征對(duì)比:")
print(stats_comparison)
這個(gè)統(tǒng)計(jì)分析揭示了以下關(guān)鍵發(fā)現(xiàn):
- 數(shù)據(jù)完整性:補(bǔ)充后的數(shù)據(jù)集從7648個(gè)觀測(cè)值增加到8497個(gè),實(shí)現(xiàn)了完整覆蓋。
- 中心趨勢(shì):補(bǔ)充后數(shù)據(jù)的均值(185.07)與原始數(shù)據(jù)基本一致,表明保持了數(shù)據(jù)的整體水平。
- 離散程度:補(bǔ)充數(shù)據(jù)的標(biāo)準(zhǔn)差(120.31)略低于原始數(shù)據(jù)(126.82),表明發(fā)生了一定程度的平滑。
- 分布特征:雖然最大值和最小值保持不變,但中位數(shù)的變化反映出分布形態(tài)有所改變。
通過這些初步分析,我們可以看到線性回歸方法在保持?jǐn)?shù)據(jù)基本特征方面表現(xiàn)良好,但也存在一定的局限性,特別是在處理數(shù)據(jù)的變異性方面。在下一部分中,我們將進(jìn)一步探討更多高級(jí)評(píng)估指標(biāo),以及決策樹回歸等其他補(bǔ)充方法的表現(xiàn)。
時(shí)間序列補(bǔ)充效果的深入評(píng)估
在時(shí)間序列分析中,僅依靠基本的統(tǒng)計(jì)指標(biāo)是不夠的。我們需要特別關(guān)注數(shù)據(jù)的時(shí)序特性,包括自相關(guān)性、趨勢(shì)和季節(jié)性模式。讓我們逐步深入這些關(guān)鍵評(píng)估維度。
自相關(guān)性分析
自相關(guān)性反映了時(shí)間序列中相鄰觀測(cè)值之間的依賴關(guān)系。保持適當(dāng)?shù)淖韵嚓P(guān)結(jié)構(gòu)對(duì)于確保補(bǔ)充數(shù)據(jù)的時(shí)序特性至關(guān)重要。以下是詳細(xì)的分析過程:
import statsmodels.api as sm
def plot_acf_comparison(original_series, imputed_series, lags=50):
"""
繪制并比較原始數(shù)據(jù)和補(bǔ)充數(shù)據(jù)的自相關(guān)函數(shù)
參數(shù):
original_series: 原始時(shí)間序列
imputed_series: 補(bǔ)充后的時(shí)間序列
lags: 滯后階數(shù)
"""
plt.figure(figsize=(14, 5))
# 分析原始數(shù)據(jù)的自相關(guān)性
plt.subplot(1, 2, 1)
sm.graphics.tsa.plot_acf(original_series.dropna(), lags=lags,
ax=plt.gca(), title="原始數(shù)據(jù)的自相關(guān)函數(shù)")
plt.grid(True)
# 分析補(bǔ)充數(shù)據(jù)的自相關(guān)性
plt.subplot(1, 2, 2)
sm.graphics.tsa.plot_acf(imputed_series, lags=lags,
ax=plt.gca(), title="補(bǔ)充數(shù)據(jù)的自相關(guān)函數(shù)")
plt.grid(True)
plt.tight_layout()
plt.show()
# 執(zhí)行自相關(guān)分析
plot_acf_comparison(mock_energy_data_with_missing['Energy_Production'],
filled_data['Energy_Production'])
圖3:自相關(guān)函數(shù)對(duì)比分析。左圖顯示原始數(shù)據(jù)的自相關(guān)性,右圖顯示補(bǔ)充后數(shù)據(jù)的自相關(guān)性
從自相關(guān)分析中,我們可以觀察到幾個(gè)重要特征:
- 短期相關(guān)性:補(bǔ)充數(shù)據(jù)在短期滯后期(1-5個(gè)lag)的自相關(guān)系數(shù)與原始數(shù)據(jù)非常接近,表明短期時(shí)間依賴關(guān)系得到了良好保持。
- 周期性特征:兩個(gè)圖中都清晰顯示出規(guī)律的波動(dòng)模式,這反映了數(shù)據(jù)中的日周期特性被很好地保留。
- 相關(guān)強(qiáng)度:補(bǔ)充數(shù)據(jù)的自相關(guān)系數(shù)整體略低于原始數(shù)據(jù),這是線性回歸補(bǔ)充過程中不可避免的平滑效應(yīng)導(dǎo)致的。
時(shí)間序列分解分析
為了更深入地理解補(bǔ)充效果,我們使用STL(Seasonal-Trend decomposition using Loess)方法將時(shí)間序列分解為趨勢(shì)、季節(jié)性和殘差組件:
# 執(zhí)行STL分解
original_series = mock_energy_data_with_missing['Energy_Production']
imputed_series = filled_data['Energy_Production']
# 考慮每日144個(gè)觀測(cè)值的周期(10分鐘采樣間隔)
original_decompose = seasonal_decompose(original_series.interpolate(),
model='additive', period=144)
imputed_decompose = seasonal_decompose(imputed_series.interpolate(),
model='additive', period=144)
# 繪制趨勢(shì)比較
plt.figure(figsize=(14, 5))
plt.plot(original_decompose.trend, label='原始趨勢(shì)', color='blue')
plt.plot(imputed_decompose.trend, label='補(bǔ)充數(shù)據(jù)趨勢(shì)',
color='green', linestyle='--')
plt.title('趨勢(shì)組件比較:原始數(shù)據(jù) vs 線性回歸補(bǔ)充')
plt.legend()
plt.grid(True)
plt.show()
# 繪制季節(jié)性比較
plt.figure(figsize=(14, 5))
plt.plot(original_decompose.seasonal, label='原始季節(jié)性', color='blue')
plt.plot(imputed_decompose.seasonal, label='補(bǔ)充數(shù)據(jù)季節(jié)性',
color='green', linestyle='--')
plt.xlim(0, 4000)
plt.title('季節(jié)性組件比較:原始數(shù)據(jù) vs 線性回歸補(bǔ)充')
plt.legend()
plt.grid(True)
plt.show()
圖4:趨勢(shì)組件比較。藍(lán)線表示原始數(shù)據(jù)趨勢(shì),綠虛線表示補(bǔ)充數(shù)據(jù)趨勢(shì)
圖5:季節(jié)性組件比較。展示了原始數(shù)據(jù)和補(bǔ)充數(shù)據(jù)的周期性模式。
通過分解分析我們發(fā)現(xiàn):
趨勢(shì)組件特征:
- 補(bǔ)充數(shù)據(jù)很好地保持了原始數(shù)據(jù)的長(zhǎng)期趨勢(shì)方向
- 趨勢(shì)線的平滑程度增加,這是線性回歸方法的特性導(dǎo)致的
- 關(guān)鍵的趨勢(shì)轉(zhuǎn)折點(diǎn)得到了準(zhǔn)確保持
季節(jié)性組件特征:
- 日內(nèi)周期的基本模式被準(zhǔn)確捕獲
- 補(bǔ)充數(shù)據(jù)的季節(jié)性振幅略有減小,表明極值被部分平滑
- 周期性的時(shí)間點(diǎn)(如日出、日落時(shí)段)的變化模式得到保持
這些分析結(jié)果提示我們,雖然線性回歸方法在保持?jǐn)?shù)據(jù)的基本時(shí)序特性方面表現(xiàn)不錯(cuò),但在處理極值和突變點(diǎn)方面可能存在局限,所以我們選用一些更好的模型如決策樹回歸器,來改善這些方面的表現(xiàn)。
決策樹回歸方法的應(yīng)用與評(píng)估
在觀察到線性回歸方法的局限性后,我們引入決策樹回歸器作為一種更靈活的補(bǔ)充方法。決策樹的非線性特性使其能夠更好地捕捉數(shù)據(jù)中的復(fù)雜模式。
決策樹回歸器的實(shí)現(xiàn)
讓我們首先看看如何使用決策樹進(jìn)行缺失值補(bǔ)充:
from sklearn.tree import DecisionTreeRegressor
# 配置并訓(xùn)練決策樹模型
# max_depth=5用于防止過擬合,同時(shí)保持足夠的模型復(fù)雜度
tree_regressor = DecisionTreeRegressor(max_depth=5, random_state=42)
tree_regressor.fit(non_missing_data[['Time_Index']],
non_missing_data['Energy_Production'])
# 使用訓(xùn)練好的模型預(yù)測(cè)缺失值
tree_predicted_values = tree_regressor.predict(missing_data[['Time_Index']])
# 將預(yù)測(cè)值填充到原始數(shù)據(jù)集
tree_filled_data = data_with_index.copy()
tree_filled_data.loc[tree_filled_data['Energy_Production'].isna(),
'Energy_Production'] = tree_predicted_values
tree_filled_data = tree_filled_data[['Datetime', 'Energy_Production']]
# 可視化決策樹補(bǔ)充結(jié)果
plt.figure(figsize=(14, 7))
plt.plot(tree_imputed_month_data['Datetime'],
tree_imputed_month_data['Energy_Production'],
label='決策樹補(bǔ)充數(shù)據(jù)', color='orange', alpha=0.8)
plt.plot(original_month_data['Datetime'],
original_month_data['Energy_Production'],
label='原始數(shù)據(jù)(含缺失)', color='red', alpha=0.9)
plt.title('原始數(shù)據(jù)與決策樹補(bǔ)充數(shù)據(jù)對(duì)比(2023年1月)')
plt.xlabel('時(shí)間')
plt.ylabel('能源生產(chǎn)量')
plt.legend()
plt.grid(True)
plt.show()
圖6:決策樹補(bǔ)充結(jié)果可視化。橙線表示決策樹補(bǔ)充的數(shù)據(jù),紅線表示原始數(shù)據(jù)
從圖中可以直觀地看到,決策樹方法在保持?jǐn)?shù)據(jù)特征方面展現(xiàn)出了以下優(yōu)勢(shì):
- 更好地保持了數(shù)據(jù)的局部變化特征
- 對(duì)極值的處理更為準(zhǔn)確
- 補(bǔ)充結(jié)果展現(xiàn)出更自然的波動(dòng)性
補(bǔ)充效果對(duì)比分析
讓我們通過各項(xiàng)指標(biāo)來系統(tǒng)比較兩種方法的表現(xiàn):
# 統(tǒng)計(jì)指標(biāo)比較
stats_comparison['決策樹補(bǔ)充數(shù)據(jù)'] = tree_filled_data['Energy_Production'].describe()
核心統(tǒng)計(jì)指標(biāo)對(duì)比:
Metric Original Data Linear Regression Decision Tree
count 7648.000000 8497.000000 8497.000000
mean 185.073509 185.073842 184.979184
std 126.816229 120.313162 120.633636
min -7.549833 -7.549833 -7.549833
25% 51.793304 54.186258 53.797479
50% 256.996772 185.197681 185.545605
75% 302.217789 298.324435 298.531049
max 415.581945 415.581945 415.581945
這些數(shù)據(jù)揭示了一些重要的發(fā)現(xiàn):
分布特征保持:
- 決策樹補(bǔ)充的數(shù)據(jù)在標(biāo)準(zhǔn)差方面(120.63)比線性回歸(120.31)更接近原始數(shù)據(jù)(126.82)
- 兩種方法都很好地保持了數(shù)據(jù)的整體范圍(最小值和最大值)
- 決策樹在四分位數(shù)的保持上表現(xiàn)更好,特別是在中位數(shù)方面
自相關(guān)性分析:
圖7:兩種方法的自相關(guān)分析對(duì)比
決策樹方法在時(shí)間依賴性的保持方面表現(xiàn)出明顯優(yōu)勢(shì):
- 更準(zhǔn)確地保持了短期相關(guān)性強(qiáng)度
- 更好地捕捉了周期性模式
- 自相關(guān)結(jié)構(gòu)的衰減特征更接近原始數(shù)據(jù)
- 趨勢(shì)和季節(jié)性分解:
圖8:趨勢(shì)組件比較
圖9:季節(jié)性組件比較
從分解結(jié)果可以看出:
- 決策樹方法在保持趨勢(shì)的細(xì)節(jié)特征方面表現(xiàn)更好
- 季節(jié)性模式的振幅和相位都得到了更準(zhǔn)確的保持
- 整體而言,決策樹補(bǔ)充的數(shù)據(jù)展現(xiàn)出更自然的時(shí)間序列特性
方法優(yōu)劣勢(shì)總結(jié)與應(yīng)用建議
基于以上分析,我們可以得出以下結(jié)論:
線性回歸方法的特點(diǎn):
- 優(yōu)勢(shì):計(jì)算效率高,實(shí)現(xiàn)簡(jiǎn)單
- 劣勢(shì):對(duì)非線性模式的捕捉能力有限
- 適用場(chǎng)景:數(shù)據(jù)呈現(xiàn)明顯的線性趨勢(shì),且波動(dòng)較為規(guī)律
決策樹方法的特點(diǎn):
- 優(yōu)勢(shì):能更好地處理非線性關(guān)系,保持?jǐn)?shù)據(jù)的局部特征
- 劣勢(shì):計(jì)算復(fù)雜度較高,需要更多的參數(shù)調(diào)優(yōu)
- 適用場(chǎng)景:數(shù)據(jù)具有復(fù)雜的非線性模式,需要保持精細(xì)的局部特征
實(shí)踐建議:
- 對(duì)于簡(jiǎn)單的時(shí)間序列,可以優(yōu)先考慮線性回歸方法
- 在處理復(fù)雜模式的數(shù)據(jù)時(shí),建議使用決策樹方法
- 可以根據(jù)具體應(yīng)用場(chǎng)景的需求(如計(jì)算資源限制、精度要求等)來選擇合適的方法
本文展示了機(jī)器學(xué)習(xí)方法在時(shí)間序列缺失值補(bǔ)充中的有效性,并提供了方法選擇的實(shí)踐指導(dǎo)。這些方法和評(píng)估框架可以推廣到其他類似的時(shí)間序列分析場(chǎng)景中。