机器学习 — 推荐系统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)