factorization machines with r
TRANSCRIPT
![Page 1: Factorization machines with r](https://reader036.vdocuments.pub/reader036/viewer/2022081900/58f9a916760da3da068b6adc/html5/thumbnails/1.jpg)
Factorization Machineswith RShota Yasui@housecat442
![Page 2: Factorization machines with r](https://reader036.vdocuments.pub/reader036/viewer/2022081900/58f9a916760da3da068b6adc/html5/thumbnails/2.jpg)
Factorization Machines(FMs)とは?
● ちゃんと知りたい人は下記の論文を参照○ Factorization machines○ S Rendle○ 2010 IEEE International Conference on Data Mining, 995-1000
○ http://www.csie.ntu.edu.tw/~b97053/paper/Rendle2010FM.pdf
● Regression, Classification, Rankingに使える機械学習モデル。
● 感覚的には「投入した特徴全ての組合せを加味してくれるモデル」。
● Kaggleの”Display Advertisment Challenge”で優勝したチームがGBDTとスタッキ
ングさせて使った。
![Page 3: Factorization machines with r](https://reader036.vdocuments.pub/reader036/viewer/2022081900/58f9a916760da3da068b6adc/html5/thumbnails/3.jpg)
Factorization Machinesのモデルglobal bias普通の回帰式でのintercept
普通の回帰部分n個の特徴。
全パターン交互作用項の近似部分
Wがpositive definiteなmatrixであれば、十分なサイズのkを持つmatrix,Vによって以下の様に分解できる。
W = V・V^t
分解してモデルに代入すると求めるパラメーターの数がn*kへと減るので計算が楽になる。
![Page 4: Factorization machines with r](https://reader036.vdocuments.pub/reader036/viewer/2022081900/58f9a916760da3da068b6adc/html5/thumbnails/4.jpg)
Rでの扱い方
● libFMexeパッケージが利用可能
● c++で実装されたlibFMをRから使うためのパッケージ。
● 基本的な使い方は以下の流れ。
a. libFMをインストール
b. libFMexeパッケージをgithubからインストール
c. R内でlibFMの場所を指定して、 libFM関数を使う。
![Page 5: Factorization machines with r](https://reader036.vdocuments.pub/reader036/viewer/2022081900/58f9a916760da3da068b6adc/html5/thumbnails/5.jpg)
libFMのインストール
● http://www.libfm.org/ へ行きダウンロード
○ macであれば解凍して、cd ./解凍したディレクトリ/libfm-1.43.srcでディレクトリ
を移動してから make all
○ windowsは解凍したらそのまま使える。
● libFMexeのインストール
○ devtoolsがインストールされたRで以下を実行する。
○ devtools::install_github("andland/libFMexe")
● 準備は一旦これでおk
![Page 6: Factorization machines with r](https://reader036.vdocuments.pub/reader036/viewer/2022081900/58f9a916760da3da068b6adc/html5/thumbnails/6.jpg)
競馬の予測をしてみる
● データ:2014/3-2016/3のデータを地方中央問わず。
○ あるサイトからスクレイピングしました汗
● 1, 2着を1とし、それ以降を0とした分類問題として解く。
● オッズの人気による予測をベースラインとして、オッズ予測よりも精度の高い分類
器を作りたい。
● FMはデータ上で起きていなかった変数の組み合わせに対してもパラメーターが近
似されている。
● なので、今までに起きていなかった馬の組み合わせでのレースにもちゃんと予測値
を出せる。。。ハズ。
![Page 7: Factorization machines with r](https://reader036.vdocuments.pub/reader036/viewer/2022081900/58f9a916760da3da068b6adc/html5/thumbnails/7.jpg)
データの準備
sparseMatrix形式でデータを準備。今回はあらかじめ整形してある。
labelにしたいデータだけ取り出して、データセットからは削る。
sampleを使ってテストデータのフラグを立てる。
sp_matrix_libFMでsparseMatix形式のデータをlibFMで読み取り可能な形式に変換する。
data.frameからも変換できる関数があるが圧倒的に遅い。
![Page 8: Factorization machines with r](https://reader036.vdocuments.pub/reader036/viewer/2022081900/58f9a916760da3da068b6adc/html5/thumbnails/8.jpg)
学習
pathを通すのが面倒なので、 exe_locを使ってlibFMの場所を指定。
kの大きさの指定
学習時の iteration
学習taskの種類regression = “r”
● libFMの結果として test_setに対する予測値が帰ってくる。
![Page 9: Factorization machines with r](https://reader036.vdocuments.pub/reader036/viewer/2022081900/58f9a916760da3da068b6adc/html5/thumbnails/9.jpg)
結果オッズの人気上位2位が勝つと想定した時の結果precision = 3337/(3555+3337) = 0.48
FMで予測し、閾値を training_dataでのlabelの平均とした場合の結果。precision = 8965/(8965+5911) = 0.6AUROCだと0.6程度。。。泣
● ベースラインとなる戦略よりはマシな戦略を立てられそう。● でもしょぼい。● ちなみにxgboostでやったらAUROCは0.7程度。。。orz● 実際に馬券を買う事を考えるとこの評価では正直微妙。
![Page 10: Factorization machines with r](https://reader036.vdocuments.pub/reader036/viewer/2022081900/58f9a916760da3da068b6adc/html5/thumbnails/10.jpg)
まとめ
● libFMはRからでも使える。
● まだ起きてない組み合わせへの予測は魅力的。
● けどとりあえずやってみたからといって何か良い結果が出るわけでもない。
● あと、競馬で儲けようと思ったら予測を当てるだけでなく、当たった時にリターンが
大きくなるような買い方も考えなければいけない。
● 人生は困難でいっぱい。
![Page 11: Factorization machines with r](https://reader036.vdocuments.pub/reader036/viewer/2022081900/58f9a916760da3da068b6adc/html5/thumbnails/11.jpg)
(参考)
● libFMexeを動かすまで
○ http://qiita.com/siero5335/items/8a8eb8de8633041d865a
● Rで競馬予想をやってみた
○ https://drive.google.com/file/d/0BzS0JN_XV85MVzFzSWszdTlwckk/view
● libFMexe(github)○ https://github.com/andland/libFMexe
![Page 12: Factorization machines with r](https://reader036.vdocuments.pub/reader036/viewer/2022081900/58f9a916760da3da068b6adc/html5/thumbnails/12.jpg)
Enjoy!