Mean Average Precision
H&Mコンペの評価指標MAP@12
H&M Personalized Fashion Recommendations
正直あまり理解しないまま進めてました・・・笑
救世主登場!
Understanding Mean Average Precision
- 非常にわかりやすい記事を見つけたので備忘録として書き下す
- 順を追って説明してくれているため非常にわかりやすい
求めているもの
$$ MAP@12 = \dfrac{1}{U} \sum_{u=1}^{U} \dfrac{1}{min(m,12)} \sum_{k=1}^{min(n,12)} P(k) \times rel(k) $$
where 𝑈 is the number of customers, 𝑃(𝑘) is the precision at cutoff 𝑘, 𝑛 is the number predictions per customer, 𝑚 is the number of ground truth values per customer, and 𝑟𝑒𝑙(𝑘) is an indicator function equaling 1 if the item at rank 𝑘 is a relevant (correct) label, zero otherwise.
- U:customersの数
- P(k):cutoff kにおける精度
- n:customerごとの予測の数
- m:customerごとの真値の数
- rel(k):指示関数、ランクkのアイテムが正しく分類できていれば1(真の値の集合に存在していれば1)、そうでなければ0
Precision
$$ \text{Precision} = \dfrac{\text{num of correct predictions}} {\text{num of all predictions}} = \dfrac{TP}{TP + FP} $$
- Trueと予測し正しかったもの / Trueと予測したもの
- フラグ立てたもののうち正しく予測できたフラグの割合ってところですかね
Precision at k
Precision at cutoff k, P(k), is simply the precision calculated by considering only the subset of your predictions from rank 1 through k.
- P(k)は予測値のランク1〜kまでのsubsetのみを考慮したPrecisionということ
- kでばっさりと切り捨てて評価するイメージですかね
例示
- ground truth = [a,b,c,d,e]
- pred = [a,e,f,g,b]
- then for P@3
- cutoffが1
- →predから1つ目だけを評価する
- →つまりbのみ評価する
- →ground_truthにbがあるか確認する
- →ある!
$$ \text{Precision at 3} = \dfrac{| { gt } \cap \text{ {pred[:3]}} |} { \text{{pred[:3]}} } = \dfrac{2}{3} \ \text{※gt = ground truth} $$
なるほど・・・
Rel at k
rel(k):指示関数、ランクkのアイテムが正しく分類できていれば1(真の値の集合に存在していれば1)、そうでなければ0
例示
- ground truth = [a,b,c,d,e]
- pred = [a,e,f,g,b]
- then for rel@3
- ランクkのアイテムのみ評価する
- →つまりf
- →fは真の値の集合に・・・いない!
- つまりrel(3) = 0
結構大変だけどもう少し・・・
Average Precision at k
This is simply the mean of the product of P@k and rel(k) for all values of k
$$ \dfrac{1}{min(n,12)} \sum_{k=1}^{min(n,12)} P(k) \times rel(k) $$
- あぁランク1から繰り返して、cutoff kのp(recision)(k)と指示関数rel(k)の掛け算し算出した値の平均なんだねって感じ
- 指示関数が0のときは加算は0になるので無得点って感じですね
- これだ!と予測した序列に応じて上位ほど得点の重みがあるってところでしょうか
こんがらがってくるけど・・・
例示
- ground truth = [a,b,c,d,e]
- pred = [a,f,c,g,b]
then for AP@5
rank k P(k) rel(k) P(k) x rel(k) 1 1 1 1 2 1 / 2 = 0.5 0 0 3 2 / 3 = 0.66.. 1 0.66.. 4 2 / 4 = 0.5 0 0 5 3 / 5 = 0.6 1 0.6 sum 2.266.. sum / 5 (= ave) 0.4533.. - ∴ AP@5 = 0.4533..
Mean Average Precision at k
- やっと来ました・・・
- みてみましょう
$$ MAP@12 = {\color{red} \dfrac{1}{U} \sum_{u=1}^{U} } \dfrac{1}{min(m,12)} \sum_{k=1}^{min(n,12)} P(k) \times rel(k) $$
- なるほど$\frac{1}{U} \sum_{u=1}^{U}$ が追加の部分で他はこれまで順を追ってきたものとなりますね
- Uはnumber of customersであるので、これまで追ってきた計算をユーザーの数だけ繰り返して平均を算出すれば良さそうですね
- あんまりわかっていない気になりごと
- kaggleではmin(m,12)とmin(n,12)と分かれている
- mはground truthの数だけどこれが例えばm=1だとすると1/1となり分母が最小化される
- m>=12だと1/12となってこのパターンが一番スコアを減少させる力学が働く
- 基本的にはmax分、12個予測しないとスコア稼ぎ観点では損をするといった感じなのかな