FP-growth - 交易中的项目必须是唯一的

FP-growth - Items in a transaction must be unique

我的计算机中有 运行 代码,并且具有频繁模式挖掘功能。我使用 FP-growth,但是 pyspark 抛出错误,我不知道如何解决它,所以使用 pyspark 的人可以帮助我吗?

首先我得到数据

data = sc.textFile(somewhere)

这一步没有错误 然后

transactions = data.map(lambda line: line.strip().split(' '))

接下来是

model = FPGrowth.train(transactions, minSupport=0.2, numPartitions=10)

这会引发错误

An error occurred while calling o19.trainFPGrowthModel.:org.apache.spark.SparkException: Job aborted due to stage failure: Task 1 in stage 1.0 failed 1 times, most recent failure: Lost task 1.0 in stage 1.0 (TID 3, localhost): org.apache.spark.SparkException: Items in a transaction must be unique but got WrappedArray(,  ,  A,  ,  Seq,  0xBB20C554Ack,  0xE6A8BA01Win,  0x7D78TcpLen,  20).

我的数据是这样的

 transactions.take(1)

[[u'03/07',
  u' 10',
  u' 22',
  u' 04.439824',
  u' 139',
  u' 1',
  u' 1',
  u' spp_sdf',
  u' SDFCombinationAlert',
  u' Classification',
  u' SenstiveData',
  u' Priority',
  u' 2',
  u' PROTO',
  u' 254',
  u' 197.218.177.69',
  u' 172.16.113.84']]

好吧,你得到的异常是不言自明的。传递给 FP-growth 的每个桶都必须包含一组项目,因此不能有重复项。例如,这不是有效输入:

transactions = sc.parallelize([["A", "A", "B", "C"], ["B", "C", "A", "A"]])
FPGrowth.train(transactions, minSupport=0.2, numPartitions=10)
## Py4JJavaError: An error occurred while calling o71.trainFPGrowthModel.
## ...
## Caused by: org.apache.spark.SparkException: 
##   Items in a transaction must be unique but got WrappedArray(A, A, B, C).

在向下游传递这些项目之前,您已确保项目是唯一的。

unique = transactions.map(lambda x: list(set(x))).cache()
FPGrowth.train(unique, minSupport=0.2, numPartitions=10)

备注:

  • 最好在 运行 FPGrowth 之前 cache 数据。
  • 主观上它不是您使用的数据的最佳选择。