機器學習 — 推薦系統javascript
做者:大樹 深圳
更新時間:2018.02.08css
email:59888745@qq.comhtml
說明:因內容較多,會不斷更新 xxx學習總結;html5
技術架構java
1.對內容數據,用戶數據,行爲數據,進行數據處理,格式化,清洗,歸併等;
2.根據業務規則創建推薦系統,內容畫像,用戶畫像,行爲畫像;
3.根據創建的各類畫像,進行相關推薦,個性化推薦,相關推薦,熱門推薦等;
4.推薦形式有,類似度推薦,相關內容推薦,好友推薦,排名推薦.python
核心算法是計算類似度,歐幾里得距離公式,排名等。jquery
機器學習 — 推薦系統linux
dennychen in shenzhenandroid
1提供推薦 css3
1。協做過裏
2。蒐集偏好
3。尋找相近的用戶
4。推薦物品,根據用戶類似度推薦,根據物品排名推薦
5。匹配商品
6。構建推薦系統
7。基於物品的過裏
8。使用數據集
9。基於用戶進行過裏仍是基於物品進行過裏
2。計算用戶類似度, 歐幾里得距離 pearson相關度
3。計算兩我的的類似度,原本是推薦平均評分較高的做品,考慮到兩我的的愛好類似程度,對評分根據類似度進行加權平均.
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
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'))
sim_pearson(critics, 'dennychen', 'alexye')
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)