機器學習 — 推薦系統

大樹2發表於2018-02-09

機器學習 — 推薦系統

 

作者:大樹 深圳
更新時間:2018.02.08

email:59888745@qq.com

說明:因內容較多,會不斷更新 xxx學習總結;

回主目錄:2017 年學習記錄和總結

 

技術架構

1.對內容資料,使用者資料,行為資料,進行資料處理,格式化,清洗,歸併等;
2.根據業務規則建立推薦系統,內容畫像,使用者畫像,行為畫像;
3.根據建立的各種畫像,進行相關推薦,個性化推薦,相關推薦,熱門推薦等;
4.推薦形式有,相似度推薦,相關內容推薦,好友推薦,排名推薦.

 

核心演算法是計算相似度,歐幾里得距離公式,排名等。

 

 

機器學習 — 推薦系統

dennychen in shenzhen

1提供推薦

1。協作過裡

2。蒐集偏好

3。尋找相近的使用者

4。推薦物品,根據使用者相似度推薦,根據物品排名推薦

5。匹配商品

6。構建推薦系統

7。基於物品的過裡

8。使用資料集

9。基於使用者進行過裡還是基於物品進行過裡

2。計算使用者相似度, 歐幾里得距離 pearson相關度

3。計算兩個人的相似度,本來是推薦平均評分較高的作品,考慮到兩個人的愛好相似程度,對評分根據相似度進行加權平均.

In [ ]:
 
from math import sqrt

critics={'dennychen': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5,
 'tomastang': 3.0, 'Superman Returns': 3.5, 'You, Me and Dupree': 2.5,
 'The Night Listener': 3.0},
'alexye': {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5,
 'Just My Luck': 1.5, 'Superman Returns': 5.0, 'The Night Listener': 3.0,
 'You, Me and Dupree': 3.5},
'Michaelzhou': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.0,
 'Superman Returns': 3.5, 'The Night Listener': 4.0},
'josephtcheng': {'Snakes on a Plane': 3.5, 'Just My Luck': 3.0,
 'The Night Listener': 4.5, 'Superman Returns': 4.0,
 'You, Me and Dupree': 2.5},
'antyonywang': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
 'Just My Luck': 2.0, 'Superman Returns': 3.0, 'The Night Listener': 3.0,
 'You, Me and Dupree': 2.0},
'jackfan': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
 'The Night Listener': 3.0, 'Superman Returns': 5.0, 'You, Me and Dupree': 3.5},
'Toby': {'Snakes on a Plane':4.5,'You, Me and Dupree':1.0,'Superman Returns':4.0}}

print(critics['dennychen']['Lady in the Water'])
print(critics['alexye']['Lady in the Water'])
# a ['Lady in the Water', 'Snakes on a Plane', 'Superman Returns', 'You, Me and Dupree', 'The Night Listener']
# sum_of_squares 3.5
In [37]:
import pandas as pd
from math import sqrt

critics={'dennychen': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5,
 'tomastang': 3.0, 'Superman Returns': 3.5, 'You, Me and Dupree': 2.5,
 'The Night Listener': 3.0},
'alexye': {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5,
 'Just My Luck': 1.5, 'Superman Returns': 5.0, 'The Night Listener': 3.0,
 'You, Me and Dupree': 3.5},
'Michaelzhou': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.0,
 'Superman Returns': 3.5, 'The Night Listener': 4.0},
'josephtcheng': {'Snakes on a Plane': 3.5, 'Just My Luck': 3.0,
 'The Night Listener': 4.5, 'Superman Returns': 4.0,
 'You, Me and Dupree': 2.5},
'antyonywang': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
 'Just My Luck': 2.0, 'Superman Returns': 3.0, 'The Night Listener': 3.0,
 'You, Me and Dupree': 2.0},
'jackfan': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
 'The Night Listener': 3.0, 'Superman Returns': 5.0, 'You, Me and Dupree': 3.5},
'Toby': {'Snakes on a Plane':4.5,'You, Me and Dupree':1.0,'Superman Returns':4.0}}

 
# 歐幾里得距離評價,評價2這之間的相似度,值越接近1,相似度越高
def sim_distance(prefs, person1, person2):
    si = {}
    for item in prefs[person1]:
        if item in prefs[person2]:
            si[item] = 1
            
    if len(si) == 0:
        return 0
    a =[item  for item in prefs[person1] if item in prefs[person2]]
    print('a',a)
    sum_of_squares = sum([pow(prefs[person1][item] - prefs[person2][item], 2) for item in prefs[person1] if item in prefs[person2]])
    print('sum_of_squares',sum_of_squares)
    return 1 / (1 + sqrt(sum_of_squares))

print(sim_distance(critics, 'dennychen', 'Michaelzhou'))
print(sim_distance(critics, 'dennychen', 'alexye'))
 
a ['Lady in the Water', 'Snakes on a Plane', 'Superman Returns', 'The Night Listener']
sum_of_squares 1.25
0.4721359549995794
a ['Lady in the Water', 'Snakes on a Plane', 'Superman Returns', 'You, Me and Dupree', 'The Night Listener']
sum_of_squares 3.5
0.3483314773547883
In [38]:
sim_pearson(critics, 'dennychen', 'alexye')
Out[38]:
0.6085806194501843
In [ ]:
 
In [32]:
import pandas as pd
from math import sqrt

critics={'dennychen': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5,
 'tomastang': 3.0, 'Superman Returns': 3.5, 'You, Me and Dupree': 2.5,
 'The Night Listener': 3.0},
'alexye': {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5,
 'Just My Luck': 1.5, 'Superman Returns': 5.0, 'The Night Listener': 3.0,
 'You, Me and Dupree': 3.5},
'Michaelzhou': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.0,
 'Superman Returns': 3.5, 'The Night Listener': 4.0},
'josephtcheng': {'Snakes on a Plane': 3.5, 'Just My Luck': 3.0,
 'The Night Listener': 4.5, 'Superman Returns': 4.0,
 'You, Me and Dupree': 2.5},
'antyonywang': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
 'Just My Luck': 2.0, 'Superman Returns': 3.0, 'The Night Listener': 3.0,
 'You, Me and Dupree': 2.0},
'jackfan': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
 'The Night Listener': 3.0, 'Superman Returns': 5.0, 'You, Me and Dupree': 3.5},
'Toby': {'Snakes on a Plane':4.5,'You, Me and Dupree':1.0,'Superman Returns':4.0}}

 
# 歐幾里得距離評價,評價2這之間的相似度,值越接近1,相似度越高
def sim_distance(prefs, person1, person2):
    si = {}
    for item in prefs[person1]:
        if item in prefs[person2]:
            si[item] = 1
            
    if len(si) == 0:
        return 0
    a =[item  for item in prefs[person1] if item in prefs[person2]]
    print('a',a)
    sum_of_squares = sum([pow(prefs[person1][item] - prefs[person2][item], 2) for item in prefs[person1] if item in prefs[person2]])
    print('sum_of_squares',sum_of_squares)
    return 1 / (1 + sqrt(sum_of_squares))

# 皮爾遜相關度評價
def sim_pearson(prefs, person1, person2):
    # 得到兩者評價過的相同商品
    si = {}
    for item in prefs[person1]:
        if item in  prefs[person2]:
            si[item] = 1
   
    n = len(si)
    # 如果兩個使用者之間沒有相似之處則返回1
    if n == 0:
        return 1
    
    # 對各自的所有偏好求和
    sum1 = sum([prefs[person1][item] for item in si])
    sum2 = sum([prefs[person2][item] for item in si])
    
    # 求各自的平方和
    sum1_square = sum([pow(prefs[person1][item], 2) for item in si])
    sum2_square = sum([pow(prefs[person2][item], 2) for item in si])
    
    # 求各自的乘積的平方
    sum_square = sum([prefs[person1][item] * prefs[person2][item] for item in si])
    
    # 計算pearson相關係數
    den = sqrt((sum1_square - pow(sum1, 2) / n) * (sum2_square - pow(sum2, 2) / n))
    if den == 0:
        return 0

    return (sum_square - (sum1 * sum2/n)) / den



def topMatches(prefs, person, n = 5, simlarity = sim_pearson):
    scores = [(simlarity(prefs, person, other), other) for other in prefs if other != person]
    
    # 對列表進行排序,評價高者排在前面
    scores.sort()
    print('scores:',scores)
    scores.reverse()
    # 取指定個數的(不需要判斷n的大小,因為python中的元組可以接受正、負不在範圍內的index)
    return scores[0:n]



# 利用其他所有人的加權平均給使用者推薦
def get_recommendations(prefs, person, similarity=sim_pearson):
    # 其他使用者對某個電影的評分加權之後的總和
    totals = {}
    # 其他使用者的相似度之和
    sim_sums = {}
    for other in prefs:
        # 不和自己比較
        if other == person:
            continue
        
        # 求出相似度
        sim = similarity(prefs, person, other)
        # 忽略相似度小於等於情況0的
        if sim <= 0:
            continue
        
        # 獲取other所有的評價過的電影評分的加權值
        for item in prefs[other]:
            # 只推薦使用者沒看過的電影
            if item not in prefs[person] or prefs[person][item] == 0:
                #print item
                # 設定預設值
                totals.setdefault(item, 0)
                # 求出該電影的加權之後的分數之和
                totals[item] += prefs[other][item] * sim
                # 求出各個使用者的相似度之和
                sim_sums.setdefault(item, 0)
                sim_sums[item] += sim
        

    # 對於加權之後的分數之和取平均值
    rankings = [(total / sim_sums[item], item) for item, total in totals.items()]

    # 返回經過排序之後的列表
    rankings.sort()
    rankings.reverse()
    return rankings

sim_distance(critics, 'dennychen', 'Michaelzhou')
# sim_pearson(critics, 'Lisa Rose', 'Gene Seymour')
topMatches(critics, 'dennychen', n = 3)

# get_recommendations(critics, 'Toby')
# get_recommendations(critics, 'Toby', similarity=sim_distance)
 
a ['Lady in the Water', 'Snakes on a Plane', 'Superman Returns', 'The Night Listener']
sum_of_squares 1.25
scores: [(0.40451991747794525, 'Michaelzhou'), (0.5606119105813882, 'josephtcheng'), (0.6085806194501843, 'alexye'), (0.7071067811865475, 'antyonywang'), (0.7470178808339965, 'jackfan'), (0.9912407071619299, 'Toby')]
Out[32]:
[(0.9912407071619299, 'Toby'),
 (0.7470178808339965, 'jackfan'),
 (0.7071067811865475, 'antyonywang')]
In [ ]:
 

相關文章