見出し画像

#D-TW07 会社行きたくない予測について (Don't want to work forecasting)

(English Follows)
前回は集まったデータを形態素解析(最小単位に分解したり、情報を付加したり)、それをまたくっつけて分析の単位に再構成するみたいな話をかきました。地味なパートが続いたので、すこしだけ深層学習を使ってみたみたいな話もしてみたいなと。ということで、過去のデータを使って、平時であればどのような値になりそうかを予測し、実際の値と比較して楽しむ(?)という部分の話をしたいと思います。あ、でも作業はやはり地味さが売りなのでやったことはやはり地味でした。地味な作業。。。嫌いじゃないです(笑)いつもどおりコード自体にはほぼ触れずに説明してありますのでご理解ください。

  1. 何をしたかったのか

  2. 時系列データの予測

  3. Prophetのチューニング

何をしたかったのか
まずは、ほぼ毎日(たまにプログラムが止まっているから"ほぼ"、、、すみません)更新しているダッシュボード(こちら)を御覧ください。ぜひ見てみてください(見てもらえると嬉しい)。ツイッターで毎日「会社行きたくない」という単語を検索して、その数を日別に表示してあります。基本的には日曜日の夜(月曜日の朝)にピコッ上がって、やっぱり月曜日は会社行きたくない人が増えるんだねと教えてくれます。そう、あなたは一人じゃない!(?)それを見ているだけでも癒やされはするのですが、天気予報の温度とか、花粉の量みたいに、例年の○倍ですとか言えたら、その日の値が特別なのか、いつもどおりなのかが分かるのではないかと思ったのです。

最初は平均値とかと比較することも考えたのですが、曜日ごとにブレも大きく、そして過去データもたくさん撮ろうとすればするほどお金もかかってしまうので。。。個人のプロジェクトなので使えるお金にも限界ありまして。2018年のデータまでしか遡れなかったのです。そこで、機械学習で予測して、「本日の会社行きたくないインデックスは例年と比べて極端に高く、原因は新垣結衣さんが結婚されたことによるガッキーロスだと思われます」(昨年データによる実話)みたいなことが言えたらちょっと面白いなと思ったのです。

時系列データの予測
それを行うのに、モデルを自分で作成するという選択肢もありましたが、初心者だったので、何か使いやすい部品(ライブラリ)が無いかと探してみると。。。当時Facebook (現在のメタ社)がProphetという、時系列データの予測のためのモデルを無料で提供していることが分かり、ちょっと使ってみようかってなりました(軽い)。年次、月次、日次の季節性を含んだ予測に強いとあり、このデータにピッタリとも思いました。

このデータというのは上記のようなデータです。週別に明らかな季節性(日曜日が基本高い)があり、暑い時期や寒い時期には上がり、年々緩やかに増えているという季節性がはっきりとある一方、地震とか、コロナニュースのように季節性には関係なく上がるときもありという感じです。ちなみに上の青い線が日曜夜(月曜朝まで)です。なんて美しいデータなんでしょう。。。

Prophetのチューニング
Prophetというライブラリーはインストールも、使い方も本当に簡単でしたが、一つ困ったことが出てきました。連休です。基本的には金、土曜日が低くて、日曜日が高いデータなのですが、連休前には更に低いとか、連休最終日は更に高いという性質があり、その部分がいつも予測がずれてしまうのです。過去データが少なすぎるという事情もあると思います。過去データ3年分程度なのでパターンの繰り返しが3回しか取れない。しかしProphetにはカレンダーをパラメーターとして入れられるという機能がついてました。ここが地味ですが、2018年からのカレンダーを見ながら、主要な日にフラグを立てていきました。いろいろ試しながらモデルの当てはまりを確認したのですが、最終的には休日、休日前、休日最後、連休最後というフラグを立てたデータを作成するくらいで大体「いい感じ」のデータになりました。いい感じっていう言葉がサイエンスっぽくなくてジワる。

ま、これくらい揃っていれば、「例年との差」みたいなコメントをするには十分かなと。モデル自体のチューニングにはそれほど手を入れずこれくらいの精度は出てくれました。今はまだ差が出たときの理由が自動的に出てくるようにはなっていないのですが、今度はそのへんを作り込みたいですね。趣味の領域で時系列データを触るということはあまりないかもしれませんが、なかなか簡単で楽しかったです。

The last post was about tokenizing and parsing Japanese sentence which is the very first step of Natural language processing (I personally believe). Since i wrote about many non-sexy jobs for few times, let me talk a bit about smart side of data science (??). I used past data to forecast latest period. Then enjoy comparison between actual data and forecasting done by machine learning. However it ended up with again tough manual jobs which I don't hate.

  1. What I wanted to do

  2. Chronological data Forecasting

  3. Tuning Prophet model

What I wanted to do
First of all, please HAVE A LOOK at this data dashboard (Here) which is update "almost" everyday. Well the update program stops occasionally, sorry in advance. It will be so glad if you look at the data (Insistent). I search for "Don't want to work" (of course in Japanese) on Twitter and plot daily data everyday (Almost). Basically it is not difficult to imagine that Sunday night is higher than other days. Yes, you are not alone in the world. Seeing that data only can be fun but I wanted to explain like weather forecasting or pollen allergy information. Today people don't want to work 2 times higher than usual… Why did I find interesting to do so… Hahaha. 

At first, I considered to compare with average or other simple stats but data fluctuate and I didn't have enough past data. Free twitter API allows you to get only past 7day data and I had to pay for the past data. Also this is the personal project to I didn't have enough budget for getting long term historical data.  So I had data only from 2018. So I decided to try machine learning to forecast. Then wanted to make a comment like "Today's index is 3 times higher than expected because of Japanese famous celebrity "Yui Aragaki" got married and people lost motivation to work today" (This is true story from the last year data). 

Chronological data Forecasting
To make it happen, I gave up the choice to create a model by myself since I was too beginner(down)… Then I tried to look for 3rd party library to be able to use for chronological data forecasting. I found "Prophet" provided from Facebook (Meta now) for free and decided to try using it for this purpose. Because it is described that the model specialized in yearly, monthly, weekly seasonal data change.

Actual data is describe in the chart above. Basically it has strong weekly seasonality and higher in hot season and cold season. Also it has increased gradually in a long run. On the other hand it has random spikes which is unexpected like natural disasters and COVID related news. Such a lovely data… (Data Otaku!)

Tuning Prophet model
It was very easy to install and use Prophet library but there was one issue which is "long holiday". Basically it is high on Sunday and low on Friday and Saturday. But it is even lower before long weekend and farther high on the last day of long weekend. It always show the gap there. I guess it is due to the short past learning data. I only had 3 year historical data. It means that there only only 3 circles to learn the annual pattern of data. So I used optional parameter that allows us to input daily data patterns. I manually set flags for each data to let the model know the impact of a particular day. I made few types of calendar to the the fit against the actual data. In the end, I used the calendar which has 4 types of date, "Holiday", "The day before holiday", "the last day of holiday" and "The last day of long holiday" which shows "Good feeling". I like the way we describe "Good fit or Good feeling" which do not sound scientific, Hahahaha.

For my purpose, the fit shown above is more than enough. I didn't tune the model that much but add the information made manually… My next challenge would be to show the reasons of the gap automatically! (I didn't even touch it yet). Maybe you don't have many opportunity to touch to chronological data for your hobby but it was fun and exciting (and easy. thanks latest technologies!).

Reference (参考)
1) Facebook Open Source (2022), https://facebook.github.io/prophet/

この記事が気に入ったらサポートをしてみませんか?