首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算PySpark中双图的相对频率

问计算PySpark中双图的相对频率
EN

Stack Overflow用户
提问于 2021-12-29 16:24:17
回答 1查看 527关注 0票数 2

我在试着在文本文件中数单词对。首先,我对文本做了一些预处理,然后对单词对数如下所示:

((Aspire, to), 1) ; ((to, inspire), 4) ; ((inspire, before), 38)...

现在,我想报告按以下顺序排列的1000对最频繁的对:

  1. Word (对的第二个词)
  2. 相对频率(成对/第二个词总发生)

以下是我迄今为止所做的

代码语言:javascript
复制
from pyspark.sql import SparkSession
import re

spark = SparkSession.builder.appName("Bigram occurences and relative frequencies").master("local[*]").getOrCreate()
sc = spark.sparkContext
text = sc.textFile("big.txt")

tokens = text.map(lambda x: x.lower()).map(lambda x: re.split("[\s,.;:!?]+", x))
pairs = tokens.flatMap(lambda xs: (tuple(x) for x in zip(xs, xs[1:]))).map(lambda x: (x, 1)).reduceByKey(lambda x, y: x + y)
frame = pairs.toDF(['pair', 'count'])

# Dataframe ordered by the most frequent pair to the least
most_frequent = frame.sort(frame['count'].desc())
# For each row, trying to add a column with the relative frequency, but I'm getting an error
with_rf = frame.withColumn("rf", frame['count'] / (frame.pair._2.sum()))

我想我已经接近我想要的结果了,但我想不出答案。我对星火和DataFrames一般都是新手。我也试过

代码语言:javascript
复制
import pyspark.sql.functions as F
frame.groupBy(frame['pair._2']).agg((F.col('count') / F.sum('count')).alias('rf')).show()

任何帮助都将不胜感激。

frame 编辑:下面是数据格式的示例

代码语言:javascript
复制
+--------------------+-----+
|                pair|count|
+--------------------+-----+
|{project, gutenberg}|   69|
|  {gutenberg, ebook}|   14|
|         {ebook, of}|    5|
|    {adventures, of}|    6|
|           {by, sir}|   12|
|     {conan, doyle)}|    1|
|     {changing, all}|    2|
|         {all, over}|   24|
+--------------------+-----+

root
 |-- pair: struct (nullable = true)
 |    |-- _1: string (nullable = true)
 |    |-- _2: string (nullable = true)
 |-- count: long (nullable = true)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-29 17:46:17

relative frequency可以通过使用window函数来计算,该函数根据pair中的第二个单词进行分区,并应用sum操作。

然后,在count的基础上,将df中的条目限制在x的顶部,最后以第二个词对和相对频率进行排序。

代码语言:javascript
复制
from pyspark.sql import functions as F
from pyspark.sql import Window as W

data = [(("project", "gutenberg"), 69,),
        (("gutenberg", "ebook"), 14,),
        (("ebook", "of"), 5,),
        (("adventures", "of"), 6,),
        (("by", "sir"), 12,),
        (("conan", "doyle"), 1,),
        (("changing", "all"), 2,),
        (("all", "over"), 24,), ]

df = spark.createDataFrame(data, ("pair", "count", ))

ws = W.partitionBy(F.col("pair")._2).rowsBetween(W.unboundedPreceding, W.unboundedFollowing)

(df.withColumn("relative_freq", F.col("count") / F.sum("count").over(ws))
   .orderBy(F.col("count").desc())
   .limit(3) # change here to select top 1000
   .orderBy(F.desc(F.col("pair")._2), F.col("relative_freq").desc())
).show()

"""
+--------------------+-----+-------------+
|                pair|count|relative_freq|
+--------------------+-----+-------------+
|         {all, over}|   24|          1.0|
|{project, gutenberg}|   69|          1.0|
|  {gutenberg, ebook}|   14|          1.0|
+--------------------+-----+-------------+
"""
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70522152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档