我正在尝试运行以下示例代码:
import org.apache.spark.mllib.fpm.PrefixSpan
val sequences = sc.parallelize(Seq(
Array(Array(1, 2), Array(3)),
Array(Array(1), Array(3, 2), Array(1, 2)),
Array(Array(1, 2), Array(5)),
Array(Array(6))
), 2).cache()
val prefixSpan = new PrefixSpan()
.setMinSupport(0.5)
.setMaxPatternLength(5)
val model = prefixSpan.run(sequences)
model.freqSequences.collect().foreach { freqSequence =>
println(
freqSequence.sequence.map(_.mkString("[", ", ", "]")).mkString("[", ", ", "]") +
", " + freqSequence.freq
)
}
我需要将model.freqSequences格式化为类似于以下内容(它是具有序列和freq的数据帧)
|[WrappedArray(2,3)] | 3
|[WrappedArray(1)] | 2
|[WrappedArray(2,1)] | 1
答案 0 :(得分:1)
在flatten
上使用freqSequence.sequence
并应用toDF
应该会提供您想要的输出
model.freqSequences.map(freqSequence => (freqSequence.sequence.flatten, freqSequence.freq)).toDF("array", "freq").show(false)
应该给您
+------+----+
|array |freq|
+------+----+
|[2] |3 |
|[3] |2 |
|[1] |3 |
|[2, 1]|3 |
|[1, 3]|2 |
+------+----+
我希望答案会有所帮助