常见词序

Python 中的 Spark SQL 入门

Mark Plutowski

Data Scientist

训练

Python 中的 Spark SQL 入门

预测

Python 中的 Spark SQL 入门

末词预测

Python 中的 Spark SQL 入门

序列

Python 中的 Spark SQL 入门

序列末尾

Python 中的 Spark SQL 入门

敏捷的棕色狐狸

Python 中的 Spark SQL 入门

句子括号

Python 中的 Spark SQL 入门

另一种聚合类型

Python 中的 Spark SQL 入门

视频

Python 中的 Spark SQL 入门

分类数据

Python 中的 Spark SQL 入门

分类型 vs 有序型

  • 分类型: he, hi, she, that, they
  • 有序型: 1, 2, 3, 4, 5
Python 中的 Spark SQL 入门

序列分析

Python 中的 Spark SQL 入门

前后词

Python 中的 Spark SQL 入门

三元组

query3 = """
   SELECT 
   id,
   word AS w1,
   LEAD(word,1) OVER(PARTITION BY part ORDER BY id ) AS w2,
   LEAD(word,2) OVER(PARTITION BY part ORDER BY id ) AS w3
   FROM df
""" 
Python 中的 Spark SQL 入门

将窗口函数 SQL 用作子查询

query3agg = """
SELECT w1, w2, w3, COUNT(*) as count FROM (
   SELECT 
   word AS w1,
   LEAD(word,1) OVER(PARTITION BY part ORDER BY id ) AS w2,
   LEAD(word,2) OVER(PARTITION BY part ORDER BY id ) AS w3
   FROM df
)
GROUP BY w1, w2, w3 
ORDER BY count DESC
""" 

spark.sql(query3agg).show()
Python 中的 Spark SQL 入门

将窗口函数 SQL 用作子查询——输出

+-----+-----+-----+-----+
|   w1|   w2|   w3|count|
+-----+-----+-----+-----+
|  one|   of|  the|   49|
|    i|think| that|   46|
|   it|   is|    a|   46|
|   it|  was|    a|   45|
| that|   it|  was|   38|
|  out|   of|  the|   35|
|.....|.....|.....|.....|
Python 中的 Spark SQL 入门

最常见的三元组

+-----+-----+-----+-----+
|   w1|   w2|   w3|count|
+-----+-----+-----+-----+
|  one|   of|  the|   49|
|    i|think| that|   46|
|   it|   is|    a|   46|
|   it|  was|    a|   45|
| that|   it|  was|   38|
|  out|   of|  the|   35|
| that|    i| have|   35|
|there|  was|    a|   34|
|    i|   do|  not|   34|
| that|   it|   is|   33|
| that|   he|  was|   30|
| that|   he|  had|   30|
| that|    i|  was|   28|
+-----+-----+-----+-----+
Python 中的 Spark SQL 入门

另一种聚合类型

query3agg = """
SELECT w1, w2, w3, length(w1)+length(w2)+length(w3) as length FROM (
   SELECT 
   word AS w1,
   LEAD(word,1) OVER(PARTITION BY part ORDER BY id ) AS w2,
   LEAD(word,2) OVER(PARTITION BY part ORDER BY id ) AS w3
   FROM df
   WHERE part <> 0 and part <> 13
)
GROUP BY w1, w2, w3 
ORDER BY length DESC
""" 

spark.sql(query3agg).show(truncate=False)
Python 中的 Spark SQL 入门

另一种聚合类型

+-------------------+-------------------+---------------+------+
|                 w1|                 w2|             w3|length|
+-------------------+-------------------+---------------+------+
|comfortable-looking|           building|    two-storied|    38|
|         widespread|comfortable-looking|       building|    37|
|      extraordinary|      circumstances|      connected|    35|
|      simple-minded|      nonconformist|      clergyman|    35|
|       particularly|          malignant|  boot-slitting|    34|
|       unsystematic|        sensational|     literature|    33|
|       oppressively|        respectable|     frock-coat|    33|
|         relentless|        keen-witted|   ready-handed|    33|
|   travelling-cloak|                and|  close-fitting|    32|
|        ruddy-faced|      white-aproned|       landlord|    32|
|  fellow-countryman|            colonel|       lysander|    32|
+-------------------+-------------------+---------------+------+
Python 中的 Spark SQL 入门

让我们练习吧

Python 中的 Spark SQL 入门

Preparing Video For Download...