【实战】使用 Python 分析 14 亿条数据

英文 / Steve Stagg,翻译 / Ryden Sun,编辑 / 昱良
juejin.im/post/5aceae206fb9a028d2084fea


Google Ngram viewer是一个有趣和有用的工具,它使用谷歌从书本中扫描来的海量的数据宝藏,绘制出单词使用量随时间的变化。举个例子,单词 Python (区分大小写)
【实战】使用 Python 分析 14 亿条数据插图

这幅图来自:books.google.com/ngrams/grap…,描绘了单词  Python  的使用量随时间的变化。
它是由谷歌的 n-gram 数据集驱动的,根据书本印刷的每一个年份,记录了一个特定单词或词组在谷歌图书的使用量。然而这并不完整(它并没有包含每一本已经发布的书!),数据集中有成千上百万的书,时间上涵盖了从 16 世纪到 2008 年。数据集可以免费从这里下载。
我决定使用 Python 和我新的数据加载库 PyTubes 来看看重新生成上面的图有多容易。

挑战

【实战】使用 Python 分析 14 亿条数据插图1
1-gram 的数据集在硬盘上可以展开成为 27 Gb 的数据,这在读入 python 时是一个很大的数据量级。Python可以轻易地一次性地处理千兆的数据,但是当数据是损坏的和已加工的,速度就会变慢而且内存效率也会变低。
总的来说,这 14 亿条数据(1,430,727,243)分散在 38 个源文件中,一共有 2 千 4 百万个(24,359,460)单词(和词性标注,见下方),计算自 1505 年至 2008 年。
当处理 10 亿行数据时,速度会很快变慢。并且原生 Python 并没有处理这方面数据的优化。幸运的是,numpy 真的很擅长处理大体量数据。使用一些简单的技巧,我们可以使用 numpy 让这个分析变得可行。
在 python/numpy 中处理字符串很复杂。字符串在 python 中的内存开销是很显著的,并且 numpy 只能够处理长度已知而且固定的字符串。基于这种情况,大多数的单词有不同的长度,因此这并不理想。

Loading the data

下面所有的代码/例子都是运行在 8 GB 内存 的 2016 年的 Macbook Pro。如果硬件或云实例有更好的 ram 配置,表现会更好。
1-gram 的数据是以 tab 键分割的形式储存在文件中,看起来如下:
  1. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(129, 162, 190);word-wrap: break-word !important;">Python</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1587</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">4</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">2</span>

  2. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(129, 162, 190);word-wrap: break-word !important;">Python</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1621</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1</span>

  3. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(129, 162, 190);word-wrap: break-word !important;">Python</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1651</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">2</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">2</span>

  4. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(129, 162, 190);word-wrap: break-word !important;">Python</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1659</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1</span>

每一条数据包含下面几个字段:
  1. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1.</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(129, 162, 190);word-wrap: break-word !important;">Word</span>

  2. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">2.</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(129, 162, 190);word-wrap: break-word !important;">Year</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> of </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(129, 162, 190);word-wrap: break-word !important;">Publication</span>

  3. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">3.</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(129, 162, 190);word-wrap: break-word !important;">Total</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> number of times the word was seen</span>

  4. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">4.</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(129, 162, 190);word-wrap: break-word !important;">Total</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> number of books containing the word</span>

为了按照要求生成图表,我们只需要知道这些信息,也就是:
  1. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1.</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> 这个单词是我们感兴趣的?</span>

  2. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">2.</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> 发布的年份</span>

  3. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">3.</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> 单词使用的总次数</span>

通过提取这些信息,处理不同长度的字符串数据的额外消耗被忽略掉了,但是我们仍然需要对比不同字符串的数值来区分哪些行数据是有我们感兴趣的字段的。这就是 pytubes 可以做的工作:
  1. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(178, 148, 187);word-wrap: break-word !important;">import</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> tubes</span>

  2. 1
     

  3. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">FILES = glob.glob(path.expanduser(</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(181, 189, 104);word-wrap: break-word !important;">"~/src/data/ngrams/1gram/googlebooks*"</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">))</span>

  4. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">WORD = </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(181, 189, 104);word-wrap: break-word !important;">"Python"</span>

  5. 1
     

  6. 1
     

  7. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">one_grams_tube = (tubes.</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(129, 162, 190);word-wrap: break-word !important;">Each</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">(FILES)</span>

  8. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">    .read_files()</span>

  9. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">    .split()</span>

  10. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">    .tsv(essay-headers=</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(178, 148, 187);word-wrap: break-word !important;">False</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">)</span>

  11. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">    .multi(</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(178, 148, 187);word-wrap: break-word !important;">lambda</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> row: (</span>

  12. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">        row.</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(178, 148, 187);word-wrap: break-word !important;">get</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">(</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">0</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">).equals(WORD.encode(</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(181, 189, 104);word-wrap: break-word !important;"> utf-8 </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">)),</span>

  13. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">        row.</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(178, 148, 187);word-wrap: break-word !important;">get</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">(</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">).to(</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(178, 148, 187);word-wrap: break-word !important;">int</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">),</span>

  14. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">        row.</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(178, 148, 187);word-wrap: break-word !important;">get</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">(</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">2</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">).to(</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(178, 148, 187);word-wrap: break-word !important;">int</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">)</span>

  15. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">    ))</span>

  16. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">)</span>

差不多 170 秒(3 分钟)之后, onegrams_ 是一个 numpy 数组,里面包含差不多 14 亿行数据,看起来像这样(添加表头部为了说明):
  1. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">╒═══════════╤════════╤═════════╕</span>

  2. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">│   </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(129, 162, 190);word-wrap: break-word !important;">Is_Word</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> │   </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(129, 162, 190);word-wrap: break-word !important;">Year</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> │   </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(129, 162, 190);word-wrap: break-word !important;">Count</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> │</span>

  3. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">╞═══════════╪════════╪═════════╡</span>

  4. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">│         </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">0</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> │   </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1799</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> │       </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">2</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> │</span>

  5. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">├───────────┼────────┼─────────┤</span>

  6. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">│         </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">0</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> │   </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1804</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> │       </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> │</span>

  7. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">├───────────┼────────┼─────────┤</span>

  8. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">│         </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">0</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> │   </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1805</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> │       </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> │</span>

  9. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">├───────────┼────────┼─────────┤</span>

  10. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">│         </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">0</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> │   </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1811</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> │       </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> │</span>

  11. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">├───────────┼────────┼─────────┤</span>

  12. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">│         </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">0</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> │   </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1820</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> │     ... │</span>

  13. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">╘═══════════╧════════╧═════════╛</span>

从这开始,就只是一个用 numpy 方法来计算一些东西的问题了:

每一年的单词总使用量

谷歌展示了每一个单词出现的百分比(某个单词在这一年出现的次数/所有单词在这一年出现的总数),这比仅仅计算原单词更有用。为了计算这个百分比,我们需要知道单词总量的数目是多少。
幸运的是,numpy让这个变得十分简单:
  1. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">last_year = </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">2008</span>

  2. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">YEAR_COL = </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(181, 189, 104);word-wrap: break-word !important;"> 1</span>

  3. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">COUNT_COL = </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(181, 189, 104);word-wrap: break-word !important;"> 2</span>

  4. 1
     

  5. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">year_totals, bins = np.histogram(</span>

  6. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">    one_grams[YEAR_COL],</span>

  7. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">    density=</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(178, 148, 187);word-wrap: break-word !important;">False</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">,</span>

  8. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">    range=(</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">0</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">, last_year+</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">),</span>

  9. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">    bins=last_year + </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">,</span>

  10. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">    weights=one_grams[COUNT_COL]</span>

  11. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">)</span>

绘制出这个图来展示谷歌每年收集了多少单词:
【实战】使用 Python 分析 14 亿条数据插图2
很清楚的是在 1800 年之前,数据总量下降很迅速,因此这回曲解最终结果,并且会隐藏掉我们感兴趣的模式。为了避免这个问题,我们只导入 1800 年以后的数据:
  1. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">one_grams_tube = (tubes.</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(129, 162, 190);word-wrap: break-word !important;">Each</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">(FILES)</span>

  2. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">    .read_files()</span>

  3. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">    .split()</span>

  4. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">    .tsv(essay-headers=</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(178, 148, 187);word-wrap: break-word !important;">False</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">)</span>

  5. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">    .skip_unless(</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(178, 148, 187);word-wrap: break-word !important;">lambda</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> row: row.</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(178, 148, 187);word-wrap: break-word !important;">get</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">(</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">).to(</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(178, 148, 187);word-wrap: break-word !important;">int</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">).gt(</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1799</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">))</span>

  6. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">    .multi(</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(178, 148, 187);word-wrap: break-word !important;">lambda</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> row: (</span>

  7. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">        row.</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(178, 148, 187);word-wrap: break-word !important;">get</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">(</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">0</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">).equals(word.encode(</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(181, 189, 104);word-wrap: break-word !important;"> utf-8 </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">)),</span>

  8. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">        row.</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(178, 148, 187);word-wrap: break-word !important;">get</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">(</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">).to(</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(178, 148, 187);word-wrap: break-word !important;">int</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">),</span>

  9. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">        row.</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(178, 148, 187);word-wrap: break-word !important;">get</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">(</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">2</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">).to(</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(178, 148, 187);word-wrap: break-word !important;">int</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">)</span>

  10. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">    ))</span>

  11. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">)</span>

这返回了 13 亿行数据(1800 年以前只有 3.7% 的的占比)
【实战】使用 Python 分析 14 亿条数据插图3

Python 在每年的占比百分数

获得 python 在每年的占比百分数现在就特别的简单了。
使用一个简单的技巧,创建基于年份的数组,2008 个元素长度意味着每一年的索引等于年份的数字,因此,举个例子,1995 就只是获取 1995 年的元素的问题了。
这都不值得使用 numpy 来操作:
  1. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">word_rows = one_grams[IS_WORD_COL]</span>

  2. 1
     

  3. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">word_counts = np.zeros(last_year+</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">1</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">)</span>

  4. 1
     

  5. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(178, 148, 187);word-wrap: break-word !important;">for</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> _, year, count </span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(178, 148, 187);word-wrap: break-word !important;">in</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;"> one_grams[word_rows]:</span>

  6. 1
     

  7. 1
    <span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">    word_counts[year] += (</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(222, 147, 95);word-wrap: break-word !important;">100</span><span class="" style="max-width: 100%;box-sizing: border-box;color: rgb(197, 200, 198);word-wrap: break-word !important;">*count) / year_totals[year]</span>

绘制出 word_counts 的结果:
【实战】使用 Python 分析 14 亿条数据插图4
形状看起来和谷歌的版本差不多
【实战】使用 Python 分析 14 亿条数据插图5
实际的占比百分数并不匹配,我认为是因为下载的数据集,它包含的用词方式不一样(比如:Python_VERB)。这个数据集在 google page 中解释的并不是很好,并且引起了几个问题:
  • 人们是如何将 Python 当做动词使用的?

  • Python  的计算总量是否包含  Python_VERB ?等

幸运的是,我们都清楚我使用的方法生成了一个与谷歌很像的图标,相关的趋势都没有被影响,因此对于这个探索,我并不打算尝试去修复。

性能

谷歌生成图片在 1 秒钟左右,相较于这个脚本的 8 分钟,这也是合理的。谷歌的单词计算的后台会从明显的准备好的数据集视图中产生作用。
举个例子,提前计算好前一年的单词使用总量并且把它存在一个单独的查找表会显著的节省时间。同样的,将单词使用量保存在单独的数据库/文件中,然后建立第一列的索引,会消减掉几乎所有的处理时间。
这次探索 确实 展示了,使用 numpy 和 初出茅庐的 pytubes 以及标准的商用硬件和 Python,在合理的时间内从十亿行数据的数据集中加载,处理和提取任意的统计信息是可行的,

语言战争

为了用一个稍微更复杂的例子来证明这个概念,我决定比较一下三个相关提及的编程语言:Python,Pascal, 和 Perl.
源数据比较嘈杂(它包含了所有使用过的英文单词,不仅仅是编程语言的提及,并且,比如,python 也有非技术方面的含义!),为了这方面的调整, 我们做了两个事情:
  1. 只有首字母大写的名字形式能被匹配(Python,不是 python)

  2. 每一个语言的提及总数已经被转换到了从 1800 年到 1960 年的百分比平均数,考虑到 Pascal 在 1970 年第一次被提及,这应该有一个合理的基准线。

结果:

【实战】使用 Python 分析 14 亿条数据插图6
对比谷歌 (没有任何的基准线调整):
【实战】使用 Python 分析 14 亿条数据插图7
运行时间: 只有 10 分钟多一点
代码: https://gist.github.com/stestagg/910859576f44f20e509822365414290d

以后的 PyTubes 提升

在这个阶段,pytubes 只有单独一个整数的概念,它是 64 比特的。这意味着 pytubes 生成的 numpy 数组对所有整数都使用 i8 dtypes。在某些地方(像 ngrams 数据),8 比特的整型就有点过度,并且浪费内存(总的 ndarray 有 38Gb,dtypes 可以轻易的减少其 60%)。我计划增加一些等级 1,2 和 4 比特的整型支持(github.com/stestagg/py…)
更多的过滤逻辑 – Tube.skip_unless() 是一个比较简单的过滤行的方法,但是缺少组合条件(AND/OR/NOT)的能力。这可以在一些用例下更快地减少加载数据的体积。
更好的字符串匹配 —— 简单的测试如下:startswith, endswith, contains, 和 isoneof 可以轻易的添加,来明显地提升加载字符串数据是的有效性。
一如既往,非常欢迎大家 patches!
推荐阅读
【Python】13 个适合『中级开发者』练手的项目
何恺明:从高考状元到CV领域年轻翘楚,靠“去雾算法”成为“CVPR最佳论文”首位华人得主
【项目】用 Python 一键分析你的上网行为, 看是在认真工作还是摸鱼
【Github】8月机器学习开源项目排行榜TOP 10
【Linux】BASH基本攻略,分分钟PK掉黑客达人
【实战】使用 Python 分析 14 亿条数据插图8
喜欢就点击“在看”吧!
    已同步到看一看

    发送中

    点赞