Python 如何在pyspark中生成具有数量的关联规则?
现在,我试图解决在PySpark环境中使用关联规则生成具有数量相关性的项目的问题 原始数据如下所示:Python 如何在pyspark中生成具有数量的关联规则?,python,apache-spark,hadoop,hive,pyspark,Python,Apache Spark,Hadoop,Hive,Pyspark,现在,我试图解决在PySpark环境中使用关联规则生成具有数量相关性的项目的问题 原始数据如下所示: ---------------------------- trans item_code item_qty ---------------------------- 001 A 2 001 B 3 002 A 4 002 B 6 002
----------------------------
trans item_code item_qty
----------------------------
001 A 2
001 B 3
002 A 4
002 B 6
002 C 10
003 D 1
----------------------------
我的目标是预测给定项目的可能附加项目。例如,给定{A:2,B:3},可能的新集可能是{A:2,B:3,C:5}。因此,我的第一步是生成带有数量系数的关联规则。我的方法是使用FP-growth算法生成频繁项集,然后找到一对一的关联规则。通过置信阈值对规则进行过滤,计算出数量系数。我想要的关联规则数据框架如下(item1->item2):
我的spark版本是1.5.1。我的代码在这里:
#!/usr/bin/python
from pyspark import SparkContext,HiveContext
from pyspark.mllib.fpm import FPGrowth
import time
#read raw data from database
def read_data():
sql="""select t.orderno_nosplit,
t.prod_code,
t.item_code,
sum(t.item_qty)
as item_qty
from ioc_fdm.fdm_dwr_ioc_fcs_pk_spu_item_f_chain t
group by t.prod_code, t.orderno_nosplit,t.item_code """
data=sql_context.sql(sql)
return data.cache()
#calculate quantity coefficient of two items
def qty_coef(item1,item2):
sql =""" select t1.item, t1.qty from table t1
where t1.trans in
(select t2.trans from spu_table t2 where t2.item ='%s'
and
(select t3.trans from spu_table t3 where t3.item = '%s' """ % (item1,item2)
df=sql_context.sql(sql)
qty_item1=df.filter(df.item_code==item1).agg({"item_qty":"sum"}).first()[0]
qty_item2=df.filter(df.item_code==item2).agg({"item_qty":"sum"}).first()[0]
coef=float(qty_item2)/qty_item1
return coef
def train(prod):
spu=total_spu.filter(total_spu.prod_code == prod)
print 'data length',spu.count(),time.strftime("%H:%M:%S")
supp=0.1
conf=0.7
sql_context.registerDataFrameAsTable(spu,'spu_table')
sql_context.cacheTable('spu_table')
print 'table register over', time.strftime("%H:%M:%S")
trans_sets=spu.rdd.repartition(32).map(lambda x:(x[0],x[2])).groupByKey().mapvalues(list).values().cache()
print 'trans group over',time.strftime("%H:%M:%S")
model=FPGrowth.train(trans_sets,supp,10)
print 'model train over',time.strftime("%H:%M:%S")
model_f1=model.freqItemsets().filter(lambda x: len(x[0]==1))
model_f2=model.freqItemsets().filter(lambda x: len(x[0]==2))
#register model_f1 as dictionary
model_f1_tuple=model_f1.map(lambda (U,V):(tuple(U)[0],V))
model_f1Map=model_f1_tuple.collectAsMap()
#convert model_f1Map to broadcast
bc_model=sc.broadcast(model_f1Map)
#generate association rules
model_f2_conf=model_f2.map(lambda x:(x[0][0],x[0][1],float(x[1])/bc_model.value[x[0][0]],float(x[1]/bc_model.value[x[0][1]])))
print 'conf calculation over',time.strftime("%H:%M:%S")
model_f2_conf_flt=model_f2_conf.flatMap(lambda x: (x[0],x[1]))
#filter the association rules by confidence threshold
model_f2_conf_flt_ftr=model_f2_conf_flt.filter(lambda x: x[2]>=conf)
#calculate the quantity coefficient for the filtered association rules
#since we cannot use nested sql operations in rdd, I have to collect the rules to list first
asso_list=model_f2_conf_flt_ftr.map(lambda x: list(x)).collect()
print 'coef calculation over',time.strftime("%H:%M:%S")
for row in asso_list:
row.append(qty_coef(row[0],row[1]))
#rewrite the list to dataframe
asso_df=sql_context.createDataFrame(asso_list,['item1','item2','conf','coef'])
sql_context.clearCache()
path = "hdfs:/user/hive/wilber/%s"%(prod)
asso_df.write.mode('overwrite').parquet(path)
if __name__ == '__main__':
sc = SparkContext()
sql_context=HiveContext(sc)
prod_list=sc.textFile('hdfs:/user/hive/wilber/prod_list').collect()
total_spu=read_data()
print 'spu read over',time.strftime("%H:%M:%S")
for prod in list(prod_list):
print 'prod',prod
train(prod)
代码可以运行,但运行速度非常慢。我知道这可能主要是因为计算最后部分的数量系数的步骤。但我不知道是否有更有效地使用Spark(1.5.1)方法来实现这一结果。多谢各位 如果粘贴这些项目会怎么样
--------------
trans item
--------------
001 A2
001 B3
002 A4
002 B6
002 C10
003 D1
---------------
然后是交易
trans item
-------------
001 A2,B3
002 A4,B6,C10
003 D1
然后运行关联可能重复的@user9613318是的,这也是我的帖子。但这一问题尚未得到回答。我真的希望有人能给我一些关于这个问题的想法。我知道你希望得到一个答案,但是把同一个问题再贴一遍是不好的,也不会让你更接近得到一个答案。一旦你有了足够的代表,你可以发布一个赏金来增加知名度,但最好是只提供最小的和可重复的例子(两个限定符同样重要)。只要浏览一下您拥有的代码,您就可以毫无理由地进行收集—这是目标的第一件事。@user9613318谢谢!可能是因为我以前的帖子描述不清楚也不简单。我简化了描述,并将重点放在关键问题上。希望能解决。
trans item
-------------
001 A2,B3
002 A4,B6,C10
003 D1