【问题标题】:Parallel-processing efficient_apriori code in PythonPython中的并行处理efficient_apriori代码
【发布时间】:2021-12-18 14:38:55
【问题描述】:

我有来自 eshop 的 1200 万条数据。我想使用efficient_apriori 包计算关联规则。 The problem is that 12 millions observations are too many,所以计算时间太长了。有没有办法加快算法的速度?我正在考虑一些并行处理或将 python 代码编译成 C。我尝试了 PYPY,但 PYPY 不支持 pandas 包。感谢您提供任何帮助或想法。

如果你想看我的代码:

import pandas as pd

from efficient_apriori import apriori

orders = pd.read_csv("orders.csv", sep=";")

customer = orders.groupby("id_customer")["name"].agg(tuple).tolist()

itemsets, rules = apriori(
            customer, min_support=100/len(customer), min_confidence=0
        )

【问题讨论】:

  • 您的 12M 数据需要多长时间?
  • @ferdy 我在 10 小时后停止了它..
  • 我在github.com/remykarem/apriori-rs/tree/master/benchmarks/data 获取了数据,大约 500k+,然后将其复制 20 倍到另一个文件中以达到 10M+。 csv to dataframe: 60s, groupby: 3s, apriori: 14s一共77s我用customer = orders.groupby("Invoice")["StockCode"].agg(tuple).tolist()

标签: python apriori


【解决方案1】:

你能用这种方法并行运行这个任务吗:

from multiprocessing import Pool

length_of_input_file=len(raw_data_min)
total_offset_count=4  # number of parallel process to run
offset=int(length_of_input_file/total_offset_count // 1)
dataNew1=customer[0:offset-1]
dataNew2=customer[offset:2*offset-1]
dataNew3=customer[2*offset:3*offset-1]
dataNew4=customer[3*offset:4*offset-1]

def calculate_frequent_itemset(fractional_data):
    """Function that calculated the frequent dataset parallely"""
    itemsets, rules = apriori(fractional_data, min_support=MIN_SUPPORT, 
    min_confidence=MIN_CONFIDENCE)
    return itemsets, rules
        
p=Pool()
frequent_itemsets=p.map(calculate_frequent_itemset,(dataNew1,dataNew2,dataNew3,dataNew4))
p.close()
p.join()

itemsets1, rules1 =frequent_itemsets[0]
itemsets2, rules2=frequent_itemsets[1]
itemsets3, rules3=frequent_itemsets[2]
itemsets4, rules4=frequent_itemsets[3]

【讨论】:

  • 伟大而优雅的解决方案,谢谢!
  • 请注意,该方案不考虑分割数据中的先验,即dataNew1中的事务不考虑在dataNew2和其他分割数据集中的事务中,这将导致不准确和较少的关联信息。
  • @ferdy 感谢您的意见!你说的对。你有什么想法,如何改进我的解决方案?
  • @vojtam 我在你的帖子中有一条关于基准数据的评论,你能试试吗?
猜你喜欢
  • 2017-09-17
  • 1970-01-01
  • 2018-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-22
  • 1970-01-01
相关资源
最近更新 更多