
132
|
第
7
章
主题列表是排好序的:
val topicPairs = medline.flatMap(t => {
t.sorted.combinations(2)
}).toDF("pairs")
topicPairs.createOrReplaceTempView("topic_pairs")
val cooccurs = spark.sql("""
SELECT pairs, COUNT(*) cnt
FROM topic_pairs
GROUP BY pairs""")
cooccurs.cache()
cooccurs.count()
由于我们的数据中有
14 548
个主题,总共可能有
14 548*14 547/2 = 105 814 878
个无序的
伴生二元组。然而,伴生组的计数结果显示数据集中实际上只有
213 745
组,只占可能数
量的很小一部分。如果考察一下数据中最常出现的伴生二元组,我们可以得到如下结果:
cooccurs.createOrReplaceTempView("cooccurs")
spark.sql("""
SELECT pairs, cnt
FROM cooccurs
ORDER BY cnt DESC
LIMIT 10""").collect().foreach(println)
...
[WrappedArray(Demography, Population Dynamics),288]
[WrappedArray(Government ...