数据挖掘与机器学习-第七章 关联规则和协同过滤


数据挖掘与机器学习-第七章 关联规则和协同过滤

1
2
3
4
5
关联规则最早用于购物篮分析

关联规则(Association Rules或Basket Analysis)是形如X->Y的蕴含式。其中,X和分别称为关联规则的先导(Antecedent,或Left-Hand-Side,LHS)和后继(Consequent或Right-Hand-Side,RHS)。

关联规则利用其支持度和置信度从大量数据中挖掘出有价值的数据项之间的相互关系。
1
一些常见概念:数据库D,事务T,项集I,支持度Confidence,置信度Support

推荐算法

推荐算法目的

image-20220503203033498

如何进行推荐

image-20220503203210462

关联规则挖掘

image-20220503203351304 image-20220503203618438

啤酒尿布案例

image-20220503203728962

image-20220503203830348

关联规则的引言

关联规则定义

image-20220503204107481

关联规则度量
支持度

image-20220503204251715

置信度

image-20220503204641040

image-20220503204744910

1
如果满足最小支持度阈值和最小置信度阈值,则我们认为关联规则是有效的。这些阈值可以认为设定。

关联规则挖掘的步骤

image-20220503204911541

image-20220503205415499

image-20220503205538418

image-20220503205828246

1
关联规则的挖掘过程主要包含两个阶段:第一个阶段从资料集合中找出所有的高频项目组(Frequent Item),第二阶段从这些高频项目组产生关联规则(Association Rule)。
1
2
第一阶段高频的含义是是指某一项目组出现的频率相对于所有记录而言,必须达到某一水平。也就是支持度。
一个满足最小支持度的k-itemset,称为高频K-项目组(Frequent k-itemset),一般表示为Frequent k。算法从Frequent k项目组中再产生Frequent k+1,直到无法再找到更长的高频项目组为止。
1
第二阶段:从高频项目组产生关联规则,这是利用前一步骤的高频k-项目组来产生规则,在最小置信度的条件门槛下,若一规则所求得的置信度满足最小置信度,称此规则为关联规则。

Apriori算法

image-20220503210240246

apriori定律

image-20220503210330591

image-20220503210443080

apriori算法

image-20220503210523821

image-20220503210639340

image-20220503210922697

apriori算法核心思想

image-20220503211449052

image-20220503212213358

image-20220503212434808 image-20220504103736736 image-20220504103823217 image-20220504103906853

apriori算法的缺点

1
2
3
4
1.对数据库的扫描次数过多
2.会产生大量的中间项集
3.采用唯一支持度
4.算法的适应面窄
1
2
3
4
主要改进方向:
减少候选集产生
降低无效的扫描库次数
提高候选集与原数据的比较次数

apriori算法实现

image-20220504165123328 image-20220504165203678 image-20220504165252234 image-20220504165335268 image-20220504165423507 image-20220504165500970

协同过滤(基于用户)

协同过滤(基于用户)

image-20220504111224639 image-20220504111306768 image-20220504111355397 image-20220504111620461 image-20220504112139300 image-20220504112251678 image-20220504112333799

image-20220504112411626

image-20220504112904173 image-20220504113246629 image-20220504113628453

协同过滤(基于物品)

image-20220504113744512 image-20220504113953624 image-20220504114048120

image-20220504114147730

image-20220504114252204 image-20220504114403470 image-20220504114610063 image-20220504114625553 image-20220504114654628

image-20220504114715624

image-20220504114744048

image-20220504114805601

image-20220504114827492 image-20220504115324968 image-20220504115354970

实验八 协同过滤算法

1
2
【实验原理】
协同过滤简单来说是利用某兴趣相投、拥有共同经验之群体的喜好来推荐用户感兴趣的信息,个人通过合作的机制给予信息相当程度的回应(如评分)并记录下来以达到过滤的目的进而帮助别人筛选信息,回应不一定局限于特别感兴趣的,特别不感兴趣信息的纪录也相当重要。
1
2
3
题目:使用协同过滤(基于用户)构建简单的电影推荐系统
假设我们以字典形式保存用户关于电影的评分数据,请构建一个电影推荐程序。
STEP1:编写函数计算欧式距离字典数据中两两用户的欧式距离。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# A dictionary of movie critics and their ratings of a small#
critics={'A':{'老炮儿':3.5,'唐人街探案':1.0},
'B':{'老炮儿':2.5,'唐人街探案':3.5,'星球大战':3.0,'寻龙诀':3.5,'神探夏洛克':2.5,'小门神':3.0},
'C':{'老炮儿':3.0,'唐人街探案':3.5,'星球大战':1.5,'寻龙诀':5.0,'神探夏洛克':3.0,'小门神':3.5},
'D':{'老炮儿':2.5,'唐人街探案':3.5,'寻龙诀':3.5,'神探夏洛克':4.0},
'E':{'老炮儿':3.5,'唐人街探案':2.0,'星球大战':4.5,'神探夏洛克':3.5,'小门神':2.0},
'F':{'老炮儿':3.0,'唐人街探案':4.0,'星球大战':2.0,'寻龙诀':3.0,'神探夏洛克':3.0,'小门神':2.0},
'G':{'老炮儿':4.5,'唐人街探案':1.5,'星球大战':3.0,'寻龙诀':5.0,'神探夏洛克':3.5}}
print(critics["B"]['星球大战'])
from math import sqrt


# Returns a distance-based similarity score for person1 and person2
def sim_distance(prefs,person1,person2):
# Get the list of shared_times
si={}
for item in prefs[person1]:
if item in prefs[person2]:si[item]=1
# if they have no rating in common, return 0
if len(si)==0:return 0
# Add up the squares of all the differences
sum_of_squares=sum([pow(prefs[person1][item]-prefs[person2][item],2) for item in prefs[person1] if item in prefs[person2]])
return 1/(1+sqrt(sum_of_squares))
print(sim_distance(critics, 'A', 'B'))
1
STEP2:编写函数依据欧式距离大小以及协同过滤算法(用户)实现电影的推荐。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Gets recommendations for a person by using a weighted average
# of every other user's rankings
def getRecommendations(prefs, person, similarity=sim_distance):
totals={}
simSums={}
for other in prefs:
# don't compare me to myself
if other == person:continue
sim=similarity(prefs,person,other)
# ignore scores of zero or lower
if sim<=0:continue
for item in prefs[other]:
# only score movies I haven't seen yet
if item not in prefs[person] or prefs[person][item]==0:
# Similarity * Score
totals.setdefault(item,0)
totals[item]+=prefs[other][item]*sim
# Sum of similarities
simSums.setdefault(item,0)
simSums[item]+=sim
# Create the normalized list
rankings=[(total/simSums[item],item) for item,total in totals.items()]
# Return the sorted list
rankings.sort()
rankings.reverse()
return rankings
print(getRecommendations(critics, 'A'))

image-20220504163224086


本文标题:数据挖掘与机器学习-第七章 关联规则和协同过滤

文章作者:TTYONG

发布时间:2022年05月03日 - 20:05

最后更新:2022年05月05日 - 10:05

原始链接:http://tianyong.fun/%E6%95%B0%E6%8D%AE%E6%8C%96%E6%8E%98%E6%8A%80%E6%9C%AF%E4%B8%8E%E5%BA%94%E7%94%A8-%E7%AC%AC%E4%B8%83%E7%AB%A0-%E5%85%B3%E8%81%94%E8%A7%84%E5%88%99%E5%92%8C%E5%8D%8F%E5%90%8C%E8%BF%87%E6%BB%A4.html

许可协议: 转载请保留原文链接及作者。

多少都是爱
0%