PrefixSpan序列提取误解

时间:2016-11-14 16:16:45

标签: python apache-spark apache-spark-mllib pattern-mining

我在列表中有一组大小为3的元组,表示窗口序列。 我需要的是使用pyspask能够得到(给定元组的两个第一部分)第三个部分。

所以我需要它根据频率创建三个元素的序列。

这就是我在做的事情:

data = [[['a','b','c'],['b','c','d'],['c','d','e'],['d','e','f'],['e','f','g'],['f','g','h'],['a','b','c'],['d','e','f'],['a','b','c'],['b','c','d'],['f','g','h'],['d','e','f'],['b','c','d']]]
rdd = spark.sparkContext.parallelize(data,2)
rdd.cache()
model = PrefixSpan.train( rdd, 0.2, 3)

print(sorted(model.freqSequences().take(100)))

虽然,我希望看到它们的序列和频率遵循字母表,但它们不会。

我得到的序列如下:

FreqSequence(sequence=[[u'c'], [u'd'], [u'b']], freq=1)
FreqSequence(sequence=[[u'g'], [u'c'], [u'c']], freq=1)

未出现在已定义的那些中。显然,我构建我的功能的方式存在问题,或者我在这个算法的目的和功能上缺少一些东西。

谢谢!

1 个答案:

答案 0 :(得分:1)

首先让我们看看你的输入:

rdd.count()
1

如您所见,您创建的数据集只包含一个序列。它可以描述为:

<(abc)(bcd)(cde)(def)(efg)(fgh)(abc)(def)(abc)(bcd)(fgh)(def)(bcd)>

因此,根据输入,您获得的模式确实是正确的。例如

FreqSequence(sequence=[[u'c'], [u'd'], [u'b']], freq=1)

对应于:

...(abc)(def)(abc)...

如果数据集的每个元素代表单个序列数据,则可以具有以下形状:

rdd = sc.parallelize([
    [['a'], ['b'], ['c']], [['b'], ['c'], ['d']], [['c'], ['d'], ['e']],
    [['d'], ['e'], ['f']], [['e'], ['f'], ['g']], [['f'], ['g'], ['h']],
    [['a'], ['b'], ['c']], [['d'], ['e'], ['f']], [['a'], ['b'], ['c']],
    [['b'], ['c'], ['d']], [['f'], ['g'], ['h']], [['d'], ['e'], ['f']],
    [['b'], ['c'], ['d']]
])

rdd.count()
13
rdd.first()
[['a'], ['b'], ['c']]

其中:

  • 每个元素都是一个列表列表。
  • 每个内部列表代表给定位置的可能替代方案。

数据结构如下:

model = PrefixSpan.train(rdd, 0.2, 3)
model.freqSequences().top(5, key=lambda x: len(x.sequence))
[FreqSequence(sequence=[['d'], ['e'], ['f']], freq=3),
 FreqSequence(sequence=[['b'], ['c'], ['d']], freq=3),
 FreqSequence(sequence=[['a'], ['b'], ['c']], freq=3),
 FreqSequence(sequence=[['f'], ['g']], freq=3),
 FreqSequence(sequence=[['d'], ['f']], freq=3)]
model.freqSequences().top(5, key=lambda x: x.freq)
[FreqSequence(sequence=[['d']], freq=7),
 FreqSequence(sequence=[['c']], freq=7),
 FreqSequence(sequence=[['f']], freq=6),
 FreqSequence(sequence=[['b']], freq=6),
 FreqSequence(sequence=[['b'], ['c']], freq=6)]