Spark MlLib 频繁模式挖掘,类型参数边界

Spark MlLib Frequent Pattern Mining, type parameter bounds

我在键值对中有数据键是列索引,值是该列值中的任何内容。我的原始文件只是一个 csv。所以我有以下内容:

val myData = sc.textFile(file1)
  .map(x => x.split('|'))
  .flatMap(x => x.zipWithIndex)
  .map(x => x.swap)
  .groupByKey().cache

这会将我的数据放入 myData:Array[(Int, Iterable[String])]

val fpg = new FPGrowth()
  .setMinSupport(0.2)
  .setNumPartitions(1)

val model = fpg.run(myData)

我遇到以下问题:

<console>:29: error: inferred type arguments [Nothing,(Int, Iterable[String])] do not conform to method run's type parameter bounds [Item,Basket <: Iterable[Item]]

我正在尝试学习如何使用 MlLib,但不太了解这个问题。我也试过删除索引和 .map(x=>x._2) 并只制作可迭代数据的集合,但这也失败了。

这应该可以解决您的问题:

fpg.run(myData.values.map(_.toArray))

基本上 FPGrowth 需要 ArrayItems。传递来自 groupByKey 的输出无效,因为它包含 Tuple2,来自 map(x => x._2) 的输出无效,因为值不是 Array

RDD 的每个元素代表一个篮子,应该只包含唯一的项目。如果您希望重复,您可以使用 _.toSet.toArray_distinct.toArray.