やってみようと思ったコンペが既に終わっていたので、kernelだけ読んでみた。
内容
パレートの法則(パレートのほうそく)は、イタリアの経済学者ヴィルフレド・パレートが発見した冪乗則。経済において、全体の数値の大部分は、全体を構成するうちの一部の要素が生み出しているという理論。80:20の法則、ばらつきの法則とも呼ばれる。
というのを信じて多くのマーケティングチームが投資をしている。
今回のお題は、Google Merchandise Store(G Store)というGoogleの小売店のデータ(ただデータはGoogle analyticsのもの)を参考に顧客毎の売上を予測する。
評価方法
Root Mean Squared Error (RMSE)
これはよくあるタイプの評価方法な気がする。
が、これは既に終わっていた…
ので以下ちょっと勉強のために読んでみる。
データ
今はデータをダウンロードできない期間だったので、ダウンロードしていません。
なので中身を見れていないですが、内容はこんな感じ。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
fullVisitorId- A unique identifier for each user of the Google Merchandise Store. channelGrouping - The channel via which the user came to the Store. date - The date on which the user visited the Store. device - The specifications for the device used to access the Store. geoNetwork - This section contains information about the geography of the user. socialEngagementType - Engagement type, either "Socially Engaged" or "Not Socially Engaged". totals - This section contains aggregate values across the session. trafficSource - This section contains information about the Traffic Source from which the session originated. visitId - An identifier for this session. This is part of the value usually stored as the _utmb cookie. This is only unique to the user. For a completely unique ID, you should use a combination of fullVisitorId and visitId. visitNumber - The session number for this user. If this is the first session, then this is set to 1. visitStartTime - The timestamp (expressed as POSIX time). hits - This row and nested fields are populated for any and all types of hits. Provides a record of all page visits. customDimensions - This section contains any user-level or session-level custom dimensions that are set for a session. This is a repeated field and has an entry for each dimension that is set. totals - This set of columns mostly includes high-level aggregate data. |
解法
とりあえずKernelを最上位の読んでみる。
まず、
In this notebook, let us explore the given dataset and make some inferences along the way. Also finally we will build a baseline light gbm model to get started.
ということでlight gbm model
ってなんだという疑問が…
Light GBM is a gradient boosting framework that uses tree based learning algorithm.
非常にわかりやすい記事なので一度読んでみてください。
Light GBMはtree based learning algorithmの中でも早くてメモリ消費も少ないために最近人気があるようなのですが、学習させると枝の伸ばすのではなく、枝を増やしていく方法を使っているからのようです。
一方でオーバーフィットしやすいので小さなデータセットには適していないようです。
筆者の感覚では10,000行以上のデータがないと使うべきではないとのこと。
paperもありました。
kerlenみてると以下で簡単にできそう。
1 2 |
import lightgbm as lgb |
Also it is important to note that some of the fields are in json format.
データセットが手元にないいま大事な情報だった。
なんか最近参加しているprojectもjsonのフィールドがあるのはこういうことか。
データはあとでちゃんとみよう。
ユーザーの累積の売上高をみると、パレートの法則を裏付けるっぽい結果となる。
しかし本当は売上に貢献しているユーザーは全体の1.3%であった。
データの加工。
They are quite a few. Since the values are constant, we can just drop them from our feature list and save some memory and time in our modeling process.
まぁまぁ衝撃的だったのが、NaNともう一種類だけの値のカラムをdropするということだった。
こういうの普通にやってるのか?
trainデータからtrain dataとvalidation dataを作る。
So there are no common dates between train and test set. So it might be a good idea to do time based validation for this dataset.
time based validation
とは?
Now let us create development and validation splits based on time to build the model. We can take the last two months as validation sample.
こういうことか。
trainとtestはもともとあるけど、trainの直近2ヶ月のところをvalidationとして検証するという。
ん?何か問題が起こってるっぽいぞ。
Now let us compute the evaluation metric on the validation data as mentioned in this new discussion thread. So we need to do a sum for all the transactions of the user and then do a log transformation on top. Let us also make the values less than 0 to 0 as transaction revenue can only be 0 or more.
this new discussion threadはここ
1 2 3 4 5 6 7 8 9 |
from sklearn import metrics pred_val[pred_val < 0] = 0 val_pred_df = pd.DataFrame({"fullVisitorId":val_df["fullVisitorId"].values}) val_pred_df["transactionRevenue"] = val_df["totals.transactionRevenue"].values val_pred_df["PredictedRevenue"] = np.expm1(pred_val) #print(np.sqrt(metrics.mean_squared_error(np.log1p(val_pred_df["transactionRevenue"].values), np.log1p(val_pred_df["PredictedRevenue"].values)))) val_pred_df = val_pred_df.groupby("fullVisitorId")["transactionRevenue", "PredictedRevenue"].sum().reset_index() print(np.sqrt(metrics.mean_squared_error(np.log1p(val_pred_df["transactionRevenue"].values), np.log1p(val_pred_df["PredictedRevenue"].values)))) |
1.709925685736863
So we are getting a validation score of 1.70 using this method against the public leaderboard score of 1.44. So please be cautious while dealing with this.
Now let us prepare the submission file similar to validation set.
これは、sum(log)
で評価するといってたのに、log(sum)
で評価してるじゃないか、ということのよう?
だから、気をつけてね。
その他
これトップのsolutionとか結構紹介されてると思うのだけど、どこに載ってるんだろうか…?