python实现Apriori算法

Apriori算法

两个概念:

支持度:A、B同时发生的概率

置信度:若A发生,B发生的概率

Apriori算法的实现

  1. 设定阈值:最小支持度和最小置信度
  2. 计算支持度:Supprot(A=>B)=(A与B同时发生的数量)/事件的总数

                                                            =Support_count(A and B)/Total_count(A)

          置信度:Confidence(A=>B)  =  P(B|A) = supprot(a and b)/support(a)

                                                                       =  Support_count(A and B)/ support(a)

   3、筛选

   4、继续计算

Python实现的apriori算法和所使用的数据见:Apriori.py + 测试数据

Python实现的apriori算法和所使用的数据见:
#Apriori算法计算购买课程的关联性
from apriori import *
import pandas as pda
import xlrd
filename='E:\\programCode\\lesson_buy.xlsx'
dataframe=pda.read_excel(filename,header=None)#参数header将读取的表格去掉表头,第一条纪录也成为数据
#转化一下数据
change=lambda x:pda.Series(1,index=x[pda.notnull(x)])
mapok=map(change,dataframe.as_matrix())
data=pda.DataFrame(list(mapok)).fillna(0)
print(data)

#设置临界支持度、置信度
spt=0.1
cfd=0.3
#计算关联结果
find_rule(data,spt,cfd,'&&')

猜你喜欢

转载自blog.csdn.net/xx20cw/article/details/84890874