Share
Embed
プログラミングLT_2019_はじめての推薦システム.pdf / introduction-to-recommender-system
2019/4/30 の プログラミングLT 2019 (https://npoint.connpass.com/event/118783/) (http://prolt.n-point.pw/about/) での発表内容です。
推薦システムのイントロダクション的ななにか。
推薦システムとは?的な話と,
シンプルなユーザベースの協調フィルタリングを実装つきで説明する内容となっています。
Yuya Matsumura
April 30, 2019
Transcript
38 slides
-
✓ Yuya Matsumura ✓ Software Engineer ✓ Wantedly, Inc. Recommendation
Team ✓ Interested in Information Retrieval, Machine Learning Self-Introduction @yu-ya4 @yu__ya4 -
It is often necessary to make choices without sufficient personal
experience of the alternatives. In everyday life, we rely on recommendations from other people either by word of mouth, recommendation letters, movie and book reviews printed in newspapers, or general surveys such as Zagat’s restaurant guides. Recommender systems assist and augment this natural social process. ਪનγεςϜͱ [Resnick+ 97] ࣗͷܦݧ͔ΒͷΈͰ͋·ΓΑ͘Θ͔Βͳ͍ͷͷத͔ΒɼͲ͏ ͯ͠ͲΕ͔Λબͳ͚ΕͳΒͳ͍ͱ͍͏͜ͱΑ͋͘Δɻ͜ͷ Α͏ͳࡍΘͨͨͪ͠ɼޱίϛɼਪનঢ়ɼ ৽ฉͷॻධөըධɼϨ ετϥϯΨΠυͳͲͷଞਓ͔ΒͷਪનʹཔΔ͜ͱΛ ৗతʹߦͬͯ ͍Δɻ ਪનγεςϜͱɼ͜͏ͨࣾ͠ձͰී௨ʹߦΘΕ͍ͯΔҰ࿈ ͷߦҝΛิॿͨ͠Γɼଅਐͨ͠Γ͢ΔͷͰ͋Δɻ [Resnick 97] P. Resnick and H. R. Varian. Recommender systems. Communications of the ACM, Vol. 40, No. 3, pp. 56–58, 1997. -
Recommenders: Tools to help identify worthwhile stuff ਪનγεςϜͱ [Konstan+ 03]
ਪનγεςϜ: ͲΕ͕Ձͷ͋Δͷ͔ಛఆ͢ΔͷΛॿ͚Δಓ۩ [Konstan 03] J. A. Konstan and J. Riedl. Recommender systems: Collaborating in commerce and communities. In Proc. of the SIGCHI Conf. on Human Factors in Computing Systems, Tutorial, 2003. -
Recommenders: Tools to help identify worthwhile stuff ࠶ܝɿਪનγεςϜͱ [Konstan+ 03]
ਪનγεςϜ: ͲΕ͕Ձͷ͋Δͷ͔ಛఆ͢ΔͷΛॿ͚Δಓ۩ [Konstan 03] J. A. Konstan and J. Riedl. Recommender systems: Collaborating in commerce and communities. In Proc. of the SIGCHI Conf. on Human Factors in Computing Systems, Tutorial, 2003. -
ਪનγεςϜͷٕज़ཁૉ 㾎 ώϡʔϚϯΠϯλϑΣʔε w ΞΠςϜͷ༰ਪનʹؔ࿈ͨ͠ใΛఏࣔ w ਪનʹඞཁͳใΛར༻ऀ͔Βత֬ʹऩू 㾎 ػցֶशɾ౷ܭత༧ଌɾใݕࡧʢਪનΞϧΰϦζϜʣ w
ऩूͨ͠σʔλʹجͮ͘ਪનใͷੜ w తʹԠͨ͡ਪનใͷม 㾎 σʔλϕʔεɾฒྻܭࢉɾωοτϫʔΫ w ਪનʹඞཁͳ্Λੵɾॲཧɻྲྀ௨ͤ͞Δج൫ http://www.kamishima.net/archive/recsysdoc.pdf -
ਪનγεςϜͷٕज़ཁૉ 㾎 ώϡʔϚϯΠϯλϑΣʔε w ΞΠςϜͷ༰ਪનʹؔ࿈ͨ͠ใΛఏࣔ w ਪનʹඞཁͳใΛར༻ऀ͔Βత֬ʹऩू 㾎 ػցֶशɾ౷ܭత༧ଌɾใݕࡧʢਪનΞϧΰϦζϜʣ w
ऩूͨ͠σʔλʹجͮ͘ਪનใͷੜ w తʹԠͨ͡ਪનใͷม 㾎 σʔλϕʔεɾฒྻܭࢉɾωοτϫʔΫ w ਪનʹඞཁͳ্Λੵɾॲཧɻྲྀ௨ͤ͞Δج൫ http://www.kamishima.net/archive/recsysdoc.pdf ͜͜ʹ͍ͭͯ͠·͢" -
ਪનख๏ͷେ·͔ͳྨ ڠௐϑΟϧλϦϯά ༰(ίϯςϯπ)ϕʔε ϑΟϧλϦϯά ϙϐϡϥϦςΟ ϋΠϒϦου MIX, ͍͍ͱ͜औΓ ଞͷϢʔβͷߦಈ(ߪങɼӾཡ etc.)ཤྺΛར༻
Ϣʔβίϯςϯπ()ͷ ใΛར༻ ϧʔϧϕʔε ਓؾϥϯΩϯά -
ਪનख๏ͷେ·͔ͳྨ ڠௐϑΟϧλϦϯά ϙϐϡϥϦςΟ ϋΠϒϦου MIX, ͍͍ͱ͜औΓ ଞͷϢʔβͷߦಈ(ߪങɼӾཡ etc.)ཤྺΛར༻ ਓؾϥϯΩϯά ༰(ίϯςϯπ)ϕʔε
ϑΟϧλϦϯά Ϣʔβίϯςϯπ()ͷ ใΛར༻ -
༰ϕʔεϑΟϧλϦϯά ϢʔβͷϓϩϑΝΠϧͱΞΠςϜͷಛͱͷϚονϯάʹΑΓɼ Ϣʔβͷᅂʹ߹க͢ΔΞΠςϜΛਪન ϢʔβϓϩϑΝΠϧ ✖ ΞΠςϜͷಛ ࿀Ѫ ϛεςϦʔ 90ͷ࡞ ࠷৽ͷ࡞࿀ѪՈ
ฤू ฤ δϟϯϧ: SF ࡞ऀ: ాத ଠ қ: ͍͠ Ϣʔβ͕Ή(աڈʹΜͩ)ΞΠςϜͷಛ δϟϯϧɼ࡞ऀɼՁ֨ͳͲͷΞΠςϜͷϝλσʔλɼ ղੳʹΑΓநग़ͨ͠ಛ δϟϯϧ: ϛεςϦʔ ࡞ऀ: ླ Ұ қ: қ͍͠ -
ਪનख๏ͷେ·͔ͳྨ ༰(ίϯςϯπ)ϕʔε ϑΟϧλϦϯά ϙϐϡϥϦςΟ ϋΠϒϦου MIX, ͍͍ͱ͜औΓ Ϣʔβίϯςϯπ()ͷ ใΛར༻ ਓؾϥϯΩϯά
ڠௐϑΟϧλϦϯά ଞͷϢʔβͷߦಈ(ߪങɼӾཡ etc.)ཤྺΛར༻ -
ᅂ͕ࣅ͍ͯΔϢʔβ ਪન͍ͨ͠λʔήοτϢʔβͱࣅ͍ͯΔߦಈ (ߪೖɼϖʔδӾཡɼ͓ؾʹೖΓ etc.)ΛऔͬͨଞͷϢʔβ ✖ ⭕ ׆ಈར༻ऀ (ਪન͍ͨ͠λʔήοτ) ඪຊར༻ऀ(ଞͷϢʔβ) ⭕
⭕ ✖ ⭕ ⭕ ✖ ˚ ✖ ✖ ⭕ ࣅ͍ͯΔ ࣅ͍ͯͳ͍ -
⭕ ⭕ ⭕ ⭕ ⭕ ⭕ ⭕ ʁ ⭕ Ξ
Π ς Ϝ Ϣʔβ 1. ϢʔβͲ͏͠ͷྨࣅΛܭࢉͯ͠ɼᅂͷࣅ͍ͯΔϢʔβΛݟ͚ͭΔɻ ✖ ✖ ✖ ✖ ✖ ⭕ ⭕ ⭕ ʁ ✖ ⭕ ✖ A B C D E ứ ϋ Ϧ ϙ λ Ừ ứ ࣙ ԓ Ừ ứ ౷ ܭ ֶ ೖ Ừ ứ ࢦ ྠ ޠ Ừ ứ φ ϧ χ Ξ Ừ - - - ✖ # ਪન͞ΕͨΞΠςϜʹ͍ͭͯͷใͷΈଘࡏ͢Δɻ # ߪೖͨ͠Β 1, ߪೖ͠ͳ͔ͬͨΒ 0 data = { "A": { "φϧχΞ": 1, "ϋϦϙλ": 1, "ࣙԓ": 0, "ࢦྠޠ": 1, "౷ܭֶೖ": 0 }, "B": { "ϋϦϙλ": 0, "ࣙԓ": 1, "ࢦྠޠ": 0, "౷ܭֶೖ": 1 }, "C": { "φϧχΞ": 1, "ࣙԓ": 0, "ࢦྠޠ": 1 }, "D": { "φϧχΞ": 1, "ϋϦϙλ": 0, "ࣙԓ": 1, "౷ܭֶೖ": 1 }, "E": { "φϧχΞ": 0, "ࣙԓ": 1, "ࢦྠޠ": 1, "౷ܭֶೖ": 0 }, } ⭕ɿߪೖ ✖ɿਪન͞Εͨʢݟͨʣ͕ະߪೖ - ɿະਪનɻݟͨ͜ͱͳ͍ɻ -
# δϟοΧʔυΛܭࢉ def jaccard_similarity(x, y): intersection = len(x.intersection(y)) union =
len(x.union(y)) return float(intersection / union) ࠓճྨࣅईͷܭࢉʹδϟοΧʔυʢJaccard indexʣΛར༻ →ʮ̎ͭͷू߹ʹؚ·Ε͍ͯΔཁૉͷ͏ͪڞ௨ཁૉ͕ΊΔׂ߹ʯΛද͢ɻ Ϣʔβ͕ߪೖͨ͠ΞΠςϜͷू߹Ͳ͏͠ͷ δϟοΧʔυΛྨࣅʹ → ಉ͡ΞΠςϜΛߪೖ͍ͯ͠ΔϢʔβ΄Ͳྨࣅ a = set(key for key, val in data["A"].items() if val == 1) c = set(key for key, val in data["C"].items() if val == 1) >>> a {'φϧχΞ', 'ࢦྠޠ', 'ϋϦϙλ'} >>> c {'φϧχΞ', 'ࢦྠޠ'} >>> jaccard_similarity(a, c) 0.6666666666666666 1. ϢʔβͲ͏͠ͷྨࣅΛܭࢉͯ͠ɼᅂͷࣅ͍ͯΔϢʔβΛݟ͚ͭΔɻ -
# աڈͷߦಈཤྺʹج͍ͮͨ 2 ϢʔβͷྨࣅΛܭࢉ def get_similarity(user1, user2): # Ϣʔβͷߦಈཤྺ history1
= data[user1] history2 = data[user2] # ྆ํͱͷϢʔβʹਪન͞ΕͨΞΠςϜͷू߹ΛͱΔɻ recommended_items1 = set(history1.keys()) recommended_items2 = set(history2.keys()) recommended_both = recommended_items1.intersection(recommended_items2) #྆ํͱʹਪન͞ΕͨΞΠςϜ͕ͳ͚Εྨࣅ 0 ͱ͢Δɻ if len(recommended_both) == 0: return 0.0 # ྆ํͱͷϢʔβʹਪન͞Ε͍͔ͯͯͭɼߪೖͨ͠ΞΠςϜͷू߹ΛͱΔɻ possitive_items1 = set([key for key, val in history1.items() if key in recommended_both and val == 1]) possitive_items2 = set([key for key, val in history2.items() if key in recommended_both and val == 1]) return jaccard_similarity(possitive_items1, possitive_items2) ϢʔβͲ͏͠ͷྨࣅͷܭࢉ 1. ϢʔβͲ͏͠ͷྨࣅΛܭࢉͯ͠ɼᅂͷࣅ͍ͯΔϢʔβΛݟ͚ͭΔɻ -
⭕ ⭕ ⭕ ⭕ ⭕ ⭕ ⭕ ʁ ⭕ Ξ
Π ς Ϝ Ϣʔβ ✖ ✖ ✖ ✖ ✖ ⭕ ⭕ ⭕ ʁ ✖ ⭕ ✖ A B C D ứ ϋ Ϧ ϙ λ Ừ ứ ࣙ ԓ Ừ ứ ౷ ܭ ֶ ೖ Ừ ứ ࢦ ྠ ޠ Ừ ứ φ ϧ χ Ξ Ừ - - - ✖ sim = get_similarity("C", "E") >>> sim 0.3333333333333333 E 1. ϢʔβͲ͏͠ͷྨࣅΛܭࢉͯ͠ɼᅂͷࣅ͍ͯΔϢʔβΛݟ͚ͭΔɻ ྆ํͱͷϢʔβʹਪન͞Ε͍͔ͯͯͭɼ ߪೖ͞ΕͨΞΠςϜͷू߹Ͳ͏͠ͷδϟοΧʔυΛྨࣅͱ͢Δ -
def get_recommends(user): # ରϢʔβ͕·ͩਪન͞Ε͍ͯͳ͍ΞΠςϜͷू߹ɿਪનରΞΠςϜ new_items = all_items - set(data[user].keys()) #
ਪનରΞΠςϜͷ༧ଌධՁΛೖΕΔശ sum_scores = {item: 0.0 for item in new_items} sum_sim = {item: 0 for item in new_items} # # ରϢʔβҎ֎ͷϢʔβ others = list(data.keys()); others.remove(user) for other in others: sim = get_similarity(user, other) for new_item in new_items: if new_item in data[other]: # "ධՁ" × "ྨࣅ" ΛਪનͷείΞͱͯ͠ɼશϢʔβʹ͍ͭͯ߹ܭ͢Δ sum_scores[new_item] += data[other][new_item] * sim # ΞΠςϜ͝ͱͷϢʔβͷྨࣅͷ߹ܭΛܭࢉ͓͖ͯ͠ɼ্هͷείΞΛׂΔʢՃॏฏۉͷΠϝʔδʣ sum_sim[new_item] += sim recommends = {item: sum_scores[item] / sum_sim[item] for item in new_items} recommends = sorted(recommends.items(), key=lambda x:x[1], reverse=True) return recommends 2. ᅂͷࣅ͍ͯΔϢʔβͷߦಈཤྺ͔ΒɼະͷΞΠςϜʹର͢Δ༧ଌධՁΛܭࢉ͢Δɻ ྨࣅͷߴ͍ϢʔβͷධՁ΄ͲॏΈ͕ߴ͘ͳΔΑ͏ʹ༧ଌධՁΛܭࢉ -
⭕ ⭕ ⭕ ⭕ ⭕ ⭕ ⭕ ʁ ⭕ Ξ
Π ς Ϝ Ϣʔβ ✖ ✖ ✖ ✖ ✖ ⭕ ⭕ ⭕ ʁ ✖ ⭕ ✖ A B C D E ứ ϋ Ϧ ϙ λ Ừ ứ ࣙ ԓ Ừ ứ ౷ ܭ ֶ ೖ Ừ ứ ࢦ ྠ ޠ Ừ ứ φ ϧ χ Ξ Ừ - - - ✖ sim_A = get_similarity("C", "A") sim_B = get_similarity("C", "B") sim_D = get_similarity("C", "D") sim_E = get_similarity("C", "E") >>> sim_A, sim_B, sim_D, sim_E 1.0 0.0 0.5 0.3333333333333333 sco re C = sco re A * sim A + sco re B * sim B + sco re D * sim D + sco re E * sim E simA + simB + simD + simE = 0 * 1.0 + 1 * 0.0 + 1 * 0.5 + 0 * 0.333 1.0 + 0.0 + 0.5 + 0.333 = 0.2727... recommends = get_recommends("C") >>> recommends [ ('ϋϦϙλ', 0.6666666666666666), ('౷ܭֶೖ', 0.27272727272727276) ] 2. ᅂͷࣅ͍ͯΔϢʔβͷߦಈཤྺ͔ΒɼະͷΞΠςϜʹର͢Δ༧ଌධՁΛܭࢉ͢Δɻ -
⭕ ⭕ ⭕ ⭕ ⭕ ⭕ ⭕ ʁ ⭕ Ξ
Π ς Ϝ Ϣʔβ ✖ ✖ ✖ ✖ ✖ ⭕ ⭕ ⭕ ʁ ✖ ⭕ ✖ A B C D E ứ ϋ Ϧ ϙ λ Ừ ứ ࣙ ԓ Ừ ứ ౷ ܭ ֶ ೖ Ừ ứ ࢦ ྠ ޠ Ừ ứ φ ϧ χ Ξ Ừ - - - ✖ recommends = get_recommends("C") >>> recommends [ ('ϋϦϙλ', 0.6666666666666666), ('౷ܭֶೖ', 0.27272727272727276) ] 3. ༧ଌධՁͷߴ͍ͷΛϢʔβʹਪન͢Δɻ ਪન! -
·ͱΊ 㾎 ਪનγεςϜͱʁ w ʮͲΕ͕Ձͷ͋Δͷ͔ಛఆ͢ΔͷΛॿ͚Δಓ۩ʯ w ώϡʔϚϯΠϯλϑΣʔεػցֶशɾ౷ܭత༧ଌɾใݕࡧσʔλϕʔεɾ ฒྻܭࢉɾωοτϫʔΫͳͲͷٕज़ཁૉ͔ΒΓཱͭ 㾎 ਪનख๏ͷྨ
w ϙϐϡϥϦςΟ༰ϕʔεڠௐϑΟϧλϦϯάϋΠϒϦου 㾎 ڠௐϑΟϧλϦϯάʢϢʔβϕʔεʣͷΈ w ࣅ͍ͯΔϢʔβΛݟ͚ͭΔʂ w ࣅ͍ͯΔϢʔβͷߦಈཤྺʹԠͯ͡ධՁΛ༧ଌͯ͠ਪનʂ -
Ref. [Resnick 97] P. Resnick and H. R. Varian. Recommender
systems. Communications of the ACM, Vol. 40, No. 3, pp. 56–58, 1997. [Konstan 03] J. A. Konstan and J. Riedl. Recommender systems: Collaborating in commerce and communities. In Proc. of the SIGCHI Conf. on Human Factors in Computing Systems, Tutorial, 2003. http://www.kamishima.net/archive/recsysdoc.pdf https://www.slideshare.net/xamat/recsys-2014-tutorial-the-recommender-problem-revisited http://blog.brainpad.co.jp/entry/2017/02/03/153000 https://www.slideshare.net/KentaOku/ss-50762836 https://qiita.com/hik0107/items/96c483afd6fb2f077985