SlideShare a Scribd company logo
1 of 21
英語ゼミ 14章
Use and Remove
Seasonality
2020/06/10
B4 土屋速斗
1
2
目次
14.1 Seasonality in Time Series
14.2 Minimum Daily Temperatures Dataset
14.3 Seasonal Adjustment with Differencing
14.4 Seasonal Adjustment with Modeling
14.5 Summary
3
この章で扱うこと
 時系列データのおける季節性の定義と機械学習を用いた予
測における影響
 毎日の気温のデータを用いて、適切な季節性を構築する
様々な手法
 季節性を直接モデリングする方法と、観測データから差し
引く方法
4
14.1 時系列データにおける季節性
季節性とは ⇒ 時間の経過に伴い規則的に繰り返されるサイクル
特に1年毎の繰り返しを季節変動と呼ぶ
一貫して、同じ周期の場合を”季節性”、そうでない
場合を”サイクル”と呼ぶ
機械学習によるモデリングにおいて、季節性の扱い方は次の2点
季節性の除去:時系列データから季節性を特定して除去する
ことで入力と出力の関係がより明確になる
季節性の追加:季節性をモデリングの際の追加の情報として
扱い、モデルの向上を図る
5
14.1 時系列データにおける季節性
季節性の種類 ⇒ ・時間帯 ・毎日 ・毎週 ・毎月 ・年間
種類が様々なので、どの季節性かを判断するかは主観的になる
簡単な方法としては、異なる尺度でトレンドラインを引くこと
季節性の種類が判定できたら、それをモデル化することで、
時系列データから除去することができる
このプロセスを季節調整と呼び、
取り除かれたデータを季節性定常と呼び、
明確な季節性を持つ時系列データは季節性非定常と呼ばれる
6
14.2 日別最適気温データセット
今回は、一日の最低気温のデータセッ
トを例に説明する
このデータセットは、オーストラリア
のメルボルン市の10年間
(1981-1990年) の最低気温を表している
7
14.3 差分による季節調整
差分による季節調整⇒季節的な成分を補正する簡単な方法は、
周期に合わせて差分をとること
Ex) 一週間毎に季節性がある場合は、先週の同じ曜日での値で
差分をとることで、季節性を消すことができる
今回は、1年毎なので、365日前のデータで削除
365データ分はそのままプロットするが、うるう年は無視
8
# deseasonalize a time series using differencing
from pandas import read_csv
from matplotlib import pyplot
series = read_csv('daily-minimum-temperatures.csv’,
header=0, index_col=0, parse_dates=True, squeeze=True)
X = series.values # 元データの読み込み
pyplot.figure(figsize=(10,4))
pyplot.subplot(1,2,1)
pyplot.title('Original')
pyplot.plot(X) # 元データの可視化
diff = list()
days_in_year = 365
for i in range(days_in_year, len(X)):
value = X[i] - X[i - days_in_year]
diff.append(value) # 元データから、365日前のデータを引く
Pyplot.subplot(1,2,2)
Pyplot.title(‘Differencing’)
Pyplot.plot(diff)
Pyplot.show() # 差分データの可視化
14.3 差分による季節調整
9
14.3 差分による季節調整
先程のは、うるう年を無視していた
月毎の気温は安定している(一か月の中で気温変化はない)と考
えて、毎月の平均気温の変化に対応したデータセットを作る。
昨年同月の平均気温で差分をとった結果を確認したい
Date
1981-01-31 17.712903
1981-02-28 17.678571
1981-03-31 13.500000
1981-04-30 12.356667
1981-05-31 9.490323
1981-06-30 7.306667
1981-07-31 7.577419
1981-08-31 7.238710
1981-09-30 10.143333
1981-10-31 10.087097
1981-11-30 11.890000
1981-12-31 13.680645
1982-01-31 16.567742
10
14.3 差分による季節調整
# calculate and plot monthly average
from pandas import read_csv
from matplotlib import pyplot
series = read_csv('daily-minimum-
temperatures.csv', header=0, index_col=0,
parse_dates=True, squeeze=True)
resample = series.resample(‘M‘) #月ごとにデータを集計
monthly_mean = resample.mean() #月ごとの平均をとる
print(monthly_mean.head(13))
monthly_mean.plot()
pyplot.show()
11
14.3 差分による季節調整
1周期前(1年前の同月)のデータを引くことで、季節性の削除を
する。
12
14.3 差分による季節調整
# deseasonalize monthly data by differencing
from pandas import read_csv
from matplotlib import pyplot
series = read_csv('daily-minimum-
temperatures.csv', header=0, index_col=0, parse_dates=True, squeeze=
True)
resample = series.resample('M')
monthly_mean = resample.mean() # 月ごとの平均データフレーム
X = series.values
diff = list()
months_in_year = 12
for i in range(months_in_year, len(monthly_mean)):
value = monthly_mean[i] - monthly_mean[i - months_in_year]
diff.append(value) # 月平均データフレームから去年同月の値を引く
pyplot.plot(diff)
pyplot.title('Differencing')
pyplot.show()
13
14.3 差分による季節調整
次に、前年の同じ月の月平均最低気温を用いて、日別最低気温
のデータセットを補正することができる
ここでも最初の年のデータをスキップする
日別データではなく月別データを用いた補正の方が安定する
可能性がある
14
14.3 差分による季節調整
# deseasonalize a time series using month-based differencing
from pandas import read_csv
from matplotlib import pyplot
series = read_csv('daily-minimum-
temperatures.csv', header=0, index_col=0, parse_dates=True, squeeze=
True)
X = series.values
diff = list()
days_in_year = 365
for i in range(days_in_year, len(X)):
month_str = str(series.index[i].year-1)+’-’
+ str(series.index[i].month) # 昨年同月のindex
month_mean_last_year = series[month_str].mean()
value = X[i] - month_mean_last_year # 元データ-同月のデータ
diff.append(value)
pyplot.plot(diff)
pyplot.show()
15
14.3 差分による季節調整
月毎の区切り方は、温度データにおいて意味はなさないかも
しれない
より、柔軟な区切り方としては、週ごとの平均気温を用いる
方が好ましいかもしれない
また、さらに良い方法として挙げられるのは複数のスケール
を用いて補正する方法
スケールの例
• 一日毎
• 週または、週のような複数の日にち事
• 月または、複数の週毎
• 四半期や季節毎、または複数の月毎
16
14.4 モデリングによる季節調整
先程は、季節性を1周期前から差分をとるという方式だったが、
今回は季節性をモデリング(=数式)していく
一般的な季節成分はSin波で表現できる可能性が高い。
一度、季節性をモデリングできれば、任意の時間での季節成分
を求めることができる
今回、モデリングするための教師あり学習において、Numpyの
Polyfitライブラリを利用する
教師データとして、1年分またはすべてのデータを使うことが
できるが、両方ためすことが理想的である
17
14.4 モデリングによる季節調整
Polyfit関数を使う際に、渡す引数は次のとおりである
polyfit(X(年), y(観測値), degree(次数))
一貫性のある正弦波のような季節性のためには、4次または5次
の多項式にすることが多い
今回は、4次のモデルを構築するため次式で表現できる
𝑦 = 𝑏1 𝑥4
+ 𝑏2 𝑥3
+ 𝑏3 𝑥2
+ 𝑏4 𝑥 + 𝑏5
b1 = -1.17308000e-08
b2 = 9.30253946e-06
b3 = -2.15977594e-03
b4 = 1.19147966e-01
b5 = 1.38980178e+01
18
#データ読み込み省略
X = [i%365 for i in range(0, len(series))]
y = series.values
degree = 4
coef = polyfit(X, y, degree) #モデル構築
print('Coefficients: %s' % coef)
curve = list()
for i in range(len(X)):
value = coef[-1]
for d in range(degree):
value += X[i]**(degree-d) * coef[d] #季節モデルの値の計算
curve.append(value)
pyplot.plot(series.values)
pyplot.plot(curve, color='red', linewidth=3)
pyplot.show()
14.4 モデリングによる季節調整
𝑦 = 𝑏1 𝑥4 + 𝑏2 𝑥3 + 𝑏3 𝑥2 + 𝑏4 𝑥 + 𝑏5
19
14.4 モデリングによる季節調整
Polyfit関数の欠点は、うるう年を考慮していないことである
これは、2/29の観測値を削除することで解決する
以下に、このモデルを使って得られる季節成分を、元のデータ
から引くことで得られる季節定常性のグラフを得られる
モデリングによる季節調整
20
X = [i%365 for i in range(0, len(series))]
y = series.values
degree = 4
coef = polyfit(X, y, degree) #季節モデル構築
curve = list()
for i in range(len(X)):
value = coef[-1]
for d in range(degree):
value += X[i]**(degree-d) * coef[d] #季節モデルの値の計算
curve.append(value)
values = series.values #元データの取り出し
diff = list()
for i in range(len(values)):
value = values[i] - curve[i] #元データ-季節モデル
diff.append(value)
pyplot.plot(diff)
pyplot.show()
14.4 モデリングによる季節調整
21
14.5 まとめ
この章では、Pythonで季節調整された時系列データセットを
作成する方法を学んだ
• 時系列データにおける季節性の重要性
• 季節調整データセットを得るための差分
• 直接、モデリングして差分を取る手法

More Related Content

Featured

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming LanguageSimplilearn
 

Featured (20)

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

Time series-forecasting-14