偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

計算時間序列周期的三種方法

開發(fā) 前端
周期是數(shù)據(jù)中出現(xiàn)重復(fù)模式所需的時間長度。更具體地說,它是模式的一個完整周期的持續(xù)時間。在這篇文章中,將介紹計算時間序列周期的三種不同方法。

周期是數(shù)據(jù)中出現(xiàn)重復(fù)模式所需的時間長度。更具體地說,它是模式的一個完整周期的持續(xù)時間。在這篇文章中,將介紹計算時間序列周期的三種不同方法。

圖片

我們使用City of Ottawa 數(shù)據(jù)集,主要關(guān)注的是每天的服務(wù)呼叫數(shù)量。所以不需要對病房名稱進行初始數(shù)據(jù)處理。Ottawa 數(shù)據(jù)集在渥太華市提供的數(shù)據(jù)門戶網(wǎng)站上免費提供。

讓我們加載2019-2022年的這些數(shù)據(jù),并將它們連接起來得到一個df。

from google.colab import drive
drive.mount('/content/gdrive')
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

file_path = '/content/gdrive/My Drive/Colab Notebooks/Data/SR-2019.xlsx'
records2019 = pd.read_excel(file_path)#,encoding='utf16')

file_path = '/content/gdrive/My Drive/Colab Notebooks/Data/SR-2020.xlsx'
records2020 = pd.read_excel(file_path)#,encoding='utf16')

file_path = '/content/gdrive/My Drive/Colab Notebooks/Data/2021_Monthly_Service_Requests_EN.xlsx'
records2021 = pd.read_excel(file_path)#,encoding='utf16')

file_path = '/content/gdrive/My Drive/Colab Notebooks/Data/2022_Monthly_Service_Requests.csv'
records2022 =pd.read_csv(file_path)

records=pd.concat([records2019,records2020,records2021,records2022],axis=0)

讓我們根據(jù)服務(wù)調(diào)用日期聚合這些數(shù)據(jù),并得到一個簡單的圖。

records["DATE_RAISED"]=pd.to_datetime(records.DATE_RAISED)
record_by_date=records.groupby("DATE_RAISED")["TYPE"].count().sort_index()
record_by_date.plot(figsize = (25, 10))
plt.ylabel('Number of requests')
plt.grid(visible=True,which='both')
plt.figure()

record_by_date.iloc[100:130].plot(figsize = (25, 10))
plt.ylabel('Number of requests')
plt.grid(visible=True,which='both')

圖片

填充缺失

讓我們檢查一下我們的數(shù)據(jù)是否包含了所有的日期。

start_date = record_by_date.index.min()
end_date = record_by_date.index.max()

# create a complete date range for the period of interest
date_range = pd.date_range(start=start_date, end=end_date, freq='D')

# compare the date range to the index of the time series
missing_dates = date_range[~date_range.isin(record_by_date.index)]

if len(missing_dates) > 0:
print("Missing dates:", missing_dates)
else:
print("No missing dates")

正如所預(yù)期的那樣,數(shù)據(jù)缺少一些日期的值。讓我們用相鄰日期的平均值填充這些值。

# Reindex to fill missing dates
idx = pd.date_range(start=record_by_date.index.min(), end=record_by_date.index.max(), freq='D')
record_by_date = record_by_date.reindex(idx, fill_value=0)

# Add missing dates with average of surrounding values
for date in missing_dates:
prev_date = date - pd.DateOffset(days=1)
next_date = date + pd.DateOffset(days=1)
prev_val = record_by_date.loc[prev_date] if prev_date in record_by_date.index else np.nan
next_val = record_by_date.loc[next_date] if next_date in record_by_date.index else np.nan
avg_val = np.nanmean([prev_val, next_val])
record_by_date.loc[date] = avg_val

這就是我們要做的所有預(yù)處理了,在所有這些步驟之后,我們嘗試檢測這個時間序列的周期。一般來說,基于假日模式和一般的人類習(xí)慣,我們希望在數(shù)據(jù)中看到七天的周期,我們來看看是不是有這樣的結(jié)果。

1、目測

最簡單的方法就是目測。這是一種主觀的方法,而不是一種正式的或統(tǒng)計的方法,所以我把它作為我們列表中的原始方法。

圖片

如果我們看一下這張圖的放大部分,我們可以看到7天的周期。最低值出現(xiàn)在5月14日、21日和28日。但最高點似乎不遵循這個模式。但在更大的范圍內(nèi),我們?nèi)匀豢梢哉f這個數(shù)據(jù)集的周期是7天。

下面我們來正式的進行分析:

2、自相關(guān)分析

我們將繪制時間序列的自相關(guān)值。查看acf圖中各種滯后值的峰值。與第一個顯著峰值對應(yīng)的滯后可以給出周期的估計。

對于這種情況,我們看看50個滯后值,并使用statmodels包中的方法繪制acf。

from statsmodels.graphics.tsaplots import plot_acf

fig, ax = plt.subplots(figsize=(14,7))
plot_acf(record_by_date.values.squeeze(), lags=50,ax=ax,title='Autocorrelation', use_vlines=True);
lags = list(range(51))
ax.set_xticks(lags);
ax.set_xticklabels(lags);

圖片

從上圖可以看出,在7、1、21等處有峰值。這證實了我們的時間序列有7天的周期。

3、快速傅里葉變換

對時間序列進行傅里葉變換,尋找主頻分量。主頻率的倒數(shù)可以作為周期的估計值。

傅里葉變換是一種數(shù)學(xué)運算,它把一個復(fù)雜的信號分解成一組更簡單的正弦和余弦波。傅里葉變換廣泛應(yīng)用于信號處理、通信、圖像處理以及其他許多科學(xué)和工程領(lǐng)域。它允許我們在頻域中分析和操作信號,這通常是一種比在時域中更自然和直觀的理解和處理信號的方法。

from scipy.fft import fft

# Calculate the Fourier transform
yf = np.fft.fft(record_by_date)
xf = np.linspace(0.0, 1.0/(2.0), len(record_by_date)//2)

# Find the dominant frequency
# We have to drop the first element of the fft as it corresponds to the
# DC component or the average value of the signal
idx = np.argmax(np.abs(yf[1:len(record_by_date)//2]))
freq = xf[idx]

period =(1/freq)
print(f"The period of the time series is {period}")

輸出為:The period of the time series is 7.030927835051545。這與我們使用acf和目視檢查發(fā)現(xiàn)的每周周期相似。

4、周期圖

周期圖 Periodogram 是一個信號或序列的功率譜密度(PSD)圖。換句話說它是一個顯示信號中每個頻率包含多少總功率的圖表。周期圖是通過計算信號的傅里葉變換的幅值平方得到的,常用于信號處理和頻譜分析。在某種意義上,只是前面給出的基于fft的方法的擴展。

from scipy.signal import periodogram

freq, power = periodogram(record_by_date)
period = 1/freq[np.argmax(power)]
print(f"The period of the time series is {period}")

plt.plot(freq, power)
plt.xlabel('Frequency (Hz)')
plt.ylabel('Power spectral density')
plt.show()

圖片

周期圖可以清楚地看出,信號的最高功率在0.14,對應(yīng)于7天的周期。

總結(jié)

本文,我們介紹了尋找時間序列周期的三種不同方法,通過使用這三種方法,我們能夠識別信號的周期性,并使用常識進行確認。

責(zé)任編輯:華軒 來源: DeepHub IMBA
相關(guān)推薦

2009-07-08 12:56:32

編寫Servlet

2009-07-30 16:27:33

C#比較時間

2010-09-14 15:10:49

CSS注釋

2009-12-11 18:49:39

預(yù)算編制博科資訊

2022-07-13 16:06:16

Python參數(shù)代碼

2024-11-15 07:00:00

Python發(fā)送郵件

2011-04-18 15:32:45

游戲測試測試方法軟件測試

2023-08-14 17:58:13

RequestHTTP請求

2023-09-15 16:25:50

2011-06-10 10:43:12

Ubuntu應(yīng)用安裝

2009-06-23 10:45:18

Hibernate支持

2023-05-16 16:07:07

大數(shù)據(jù)數(shù)據(jù)管理工具

2021-07-13 12:31:27

IT組織改進首席技術(shù)官

2009-07-23 15:17:54

JDBC連接Acces

2016-09-09 13:07:56

CentOSJDKLinux

2023-09-25 15:08:43

Python方離群值

2021-09-10 18:09:42

SQL注入漏洞網(wǎng)絡(luò)攻擊

2010-10-20 13:52:07

SQL Server數(shù)

2021-12-20 07:11:26

Java List排序 Java 基礎(chǔ)

2021-10-09 06:59:36

技術(shù)MyBatis數(shù)據(jù)
點贊
收藏

51CTO技術(shù)棧公眾號