一文講解Python時間序列數(shù)據(jù)的預處理
來源:Deephub Imba
時間序列數(shù)據(jù)的定義及其重要性。 時間序列數(shù)據(jù)的預處理步驟。 構建時間序列數(shù)據(jù),查找缺失值,對特征進行去噪,并查找數(shù)據(jù)集中存在的異常值。
時間序列是在特定時間間隔內(nèi)記錄的一系列均勻分布的觀測值。
時間序列數(shù)據(jù)預處理
import pandas as pd
passenger = pd.read_csv('AirPassengers.csv')
passenger['Date'] = pd.to_datetime(passenger['Date'])
passenger.sort_values(by=['Date'], inplace=True, ascending=True)

時間序列中的缺失值
基于時間的插值 樣條插值 線性插值
from matplotlib.pyplot import figure
import matplotlib.pyplot as plt
figure(figsize=(12, 5), dpi=80, linewidth=10)
plt.plot(passenger['Date'], passenger['Passengers'])
plt.title('Air Passengers Raw Data with Missing Values')
plt.xlabel('Years', fontsize=14)
plt.ylabel('Number of Passengers', fontsize=14)
plt.show()

passenger[‘Linear’] = passenger[‘Passengers’].interpolate(method=’linear’)
passenger[‘Spline order 3’] = passenger[‘Passengers’].interpolate(method=’spline’, order=3)
passenger[‘Time’] = passenger[‘Passengers’].interpolate(method=’time’)
methods = ['Linear', 'Spline order 3', 'Time']
from matplotlib.pyplot import figure
import matplotlib.pyplot as plt
for method in methods:
? figure(figsize=(12, 4), dpi=80, linewidth=10)
? plt.plot(passenger["Date"], passenger[method])
? plt.title('Air Passengers Imputation using: ' + types)
? plt.xlabel("Years", fontsize=14)
? plt.ylabel("Number of Passengers", fontsize=14)
? plt.show()

時間序列去噪
rolling_google = google_stock_price['Open'].rolling(20).mean()
plt.plot(google_stock_price['Date'], google_stock_price['Open'])
plt.plot(google_stock_price['Date'], rolling_google)
plt.xlabel('Date')
plt.ylabel('Stock Price')
plt.legend(['Open','Rolling Mean'])
plt.show()

denoised_google_stock_price = fft_denoiser(value, 0.001, True)
plt.plot(time, google_stock['Open'][0:300])
plt.plot(time, denoised_google_stock_price)
plt.xlabel('Date', fontsize = 13)
plt.ylabel('Stock Price', fontsize = 13)
plt.legend([‘Open’,’Denoised: 0.001'])
plt.show()

時間序列中的離群值檢測



可能的面試問題
預處理時間序列數(shù)據(jù)的方法有哪些,與標準插補方法有何不同? 時間序列窗口是什么意思? 你聽說過孤立森林嗎?如果是,那么你能解釋一下它是如何工作的嗎? 什么是傅立葉變換,我們?yōu)槭裁葱枰?/span> 填充時間序列數(shù)據(jù)中缺失值的不同方法是什么?
總結

加入知識星球【我們談論數(shù)據(jù)科學】
500+小伙伴一起學習!
·?推薦閱讀?·
評論
圖片
表情
