有没有办法使collection.Counter(Python2.7)知道它的输入列表是排序的?

问题

我已经用不同的方式玩(在Python 2.7中),从语料库或字符串列表中提取(单词,频率)元组列表,并比较其效率.据我所知,在正常情况下,一个未排序的列表,收集模块中的Countermethod优于任何我在别处找到的东西,但似乎并没有太多优势一个预先排序的列表,我已经提出了在这种特殊情况下很容易击败的方法.那么简而言之,有没有什么内置的方式来通知柜台呢,这个事实呢已经列出了一个列表来进一步加速呢?

(接下来的部分是Counter的工作是非法的,你可能希望跳到最后,在处理排序列表时失去它的魅力.)

未排序的输入列表

一种不行的方法

天真的方法是使用sorted([(word,corpus.count(word))在集合(语料)]中的单词),但是一旦你的语料库是几千个项目长的话,那个可靠的可靠地让你进入运行时问题 – 毫不奇怪,因为你遍历了n个单词的整个列表很多次,其中m是唯一的单词的数量.

排序列表本地搜索

所以我试图做的事情之前,我发现计数器是确保所有的搜索是严格本地的,首先排序输入列表(我也必须删除数字和标点符号,并将所有条目转换为小写,以避免重复像“foo”,’Foo’和’foo:’).

#Natural Language Toolkit,for access to corpus; any other source for a long text will do,though.
import nltk 

# nltk corpora come as a class of their own,as I udnerstand it presenting to the
# outside as a unique list but underlyingly represented as several lists,with no more
# than one ever loaded into memory at any one time,which is good for memory issues 
# but rather not so for speed so let's disable this special feature by converting it
# back into a conventional list:
corpus = list(nltk.corpus.gutenberg.words()) 

import string
drop = string.punctuation+string.digits  

def wordcount5(corpus,Case=False,lower=False,StrippedSorted=False):
    '''function for extracting word frequencies out of a corpus. Returns an alphabetic list
    of tuples consisting of words contained in the corpus with their frequencies.  
    Default is case-insensitive,but if you need separate entries for upper and lower case 
    spellings of the same words,set option Case=True. If your input list is already sorted
    and stripped of punctuation marks/digits and/or all lower case,you can accelerate the 
    operation by a factor of 5 or so by declaring so through the options "Sorted" and "lower".'''
    # you can ignore the following 6 lines for Now,they're only relevant with a pre-processed input list
    if lower or Case:
        if StrippedSorted:
            sortedc = corpus 
        else:    
            sortedc = sorted([word.replace('--',' ').strip(drop)
                   for word in sorted(corpus)])
    # here we sort and purge the input list in the default case:
    else:
            sortedc = sorted([word.lower().replace('--',' ').strip(drop)
                   for word in sorted(corpus)])
    # start iterating over the (sorted) input list:
    scindex = 0
    # create a list:
    freqs = []
    # identify the first token:
    currentword = sortedc[0]
    length = len(sortedc)
    while scindex < length:
        wordcount = 0
        # increment a local counter while the tokens == currentword
        while scindex < length and sortedc[scindex] == currentword:
            scindex += 1
            wordcount += 1
        # store the current word and final score when a) a new word appears or
        # b) the end of the list is reached
        freqs.append((currentword,wordcount))
        # if a): update currentword with the current token
        if scindex < length:
            currentword = sortedc[scindex]
    return freqs

查找收藏

这是更好的,但仍然不如使用集合模块中的Counter类,它创建一个{word:word}的条目的字典(我们仍然要做同样的剥离和降低,但没有排序) :

from collections import Counter
cnt = Counter()
for word in [token.lower().strip(drop) for token in corpus]:
    cnt[word] += 1
# optionally,if we want to have the same output format as before,# we can do the following which negligibly adds in runtime:
wordfreqs = sorted([(word,cnt[word]) for word in cnt])

在古腾堡语料库计数器方法在我的机器上快了30%(5秒,而不是7.2),这主要通过排序例程来解释,这个程序吃了大约2.1秒(如果你没有,不想安装nltk包(自然语言工具包),它提供对这个语料库的访问,任何其他足够长的文本被正确分割成词级的字符串列表将显示相同的信息)

比较性能

以我的特殊方法使用重言式作为延迟执行的条件,这给了我们计数器的方法

import time
>>> if 1:
...     start = time.time()
...     cnt = Counter()
...     for word in [token.lower().strip(drop) for token in corpus if token not in [" ",""]]:
...         cnt[word] += 1
...     time.time()-start
...     cntgbfreqs = sorted([(word,cnt[word]) for word in cnt])
...     time.time()-start
... 
4.999882936477661
5.191655874252319

(我们看到,将结果格式化为元组列表的最后一步占用时间的不到5%).

与我的功能相比:

>>> if 1:
...     start = time.time()
...     gbfreqs = wordcount5(corpus)
...     time.time()-start
... 
7.261770963668823

排序的输入列表 – 当计数器“失败”

但是,您可能已经注意到,我的功能允许指定输入已经被排序,剥离了标点符号垃圾,并转换为小写.如果我们已经为某些其他操作创建了这样的转换版本,使用它(并声明如此)可以极大地加速我的wordcount5的操作:

>>> sorted_corpus = sorted([token.lower().strip(drop) for token in corpus if token not in [" ",""]])
>>> if 1:
...     start = time.time()
...     strippedgbfreqs2 = wordcount5(sorted_corpus,lower=True,StrippedSorted=True)
...     time.time()-start
... 
0.9050078392028809

在这里,我们已经将运行时减少了一个因子. 8通过不必排序语料库并转换项目.当然,后者在使用这个新列表的计数器时也是如此,所以期望它也有点快一些,但是似乎没有利用它被排序的事实,现在需要两倍于我的功能,比以前快30%

>>> if 1:
...     start = time.time()
...     cnt = Counter()
...     for word in sorted_corpus:
...         cnt[word] += 1
...     time.time()-start
...     strippedgbfreqs = [(word,cnt[word])for word in cnt]
...     time.time()-start
... 
1.9455058574676514
2.0096349716186523

当然,我们可以使用与wordcount5中使用相同的逻辑 – 递增一个本地计数器,直到我们遇到一个新的单词,然后才把最后一个单词与计数器的当前状态一起存储,并将该计数器复位为0表示下一个单词 – 只使用Counter作为存储,但Counter方法的固有效率似乎丢失了,性能在我的功能的范围内,用于创建一个字典,转换成一个元组列表的额外负担现在看起来比使用更麻烦当我们处理原始语料库时:

>>> def countertest():
...     start = time.time()
...     sortedcnt = Counter()
...     c = 0
...     length = len(sorted_corpus)
...     while c < length:
...         wcount = 0
...         word = sorted_corpus[c]
...         while c < length and sorted_corpus[c] == word:
...             wcount+=1
...             c+=1
...         sortedcnt[word] = wcount
...         if c < length:
...             word = sorted_corpus[c]
...     print time.time()-start
...     return sorted([(word,sortedcnt[word]) for word in sortedcnt])
...     print time.time()-start
... 
>>> strippedbgcnt = countertest()
0.920727014542
1.08029007912

(结果的相似性并不是非常令人惊讶,因为我们实际上禁用了Counter自己的方法,并将其滥用作为使用与以前相同的方法获得的值的存储.)

因此,我的问题是:有更通用的方式来通知计数器,它的输入列表已经被排序,并使它将当前的密钥保存在内存中,而不是每次重新查找 – 可预测的 – 遇到下一个令牌同一个字?换句话说,是否可以通过将计数器/字典类的固有效率与排序列表的明显好处相结合来进一步提高预排序列表的性能,或者我已经在0.9秒的硬限制上用于统计2M条目的列表?

可能没有很大的改进空间 – 在做最简单的事情时,我可以想到的时间在55秒左右,仍然需要遍历相同的列表并检查每个单独的值,而.25为set(语料库)没有计数,但也许有一些itertools魔术在那里有助于接近这些数字?

(注意:我是Python的一个相对新手,一般来说是编程,所以如果我错过了一些明显的事情,那么可以借口)

编辑12月1日:

另外一件事,除了排序本身,这使我的所有方法都比较慢,将2M字符串中的每一个字符串转换为小写,并剥离它们可能包含的任何标点符号或数字.我以前尝试过快捷方式,通过计算未处理的字符串,然后转换结果并删除重复,同时添加他们的计数,但我必须做错了,它使事情变得如此慢一些.因此,我恢复到以前的版本,转换原始语料库中的所有内容,现在不能完全重建我在那里做的.

如果我现在尝试,我会改进转换字符串最后.我仍然通过循环列表(结果)来完成.我所做的是写出几个函数,它们之间转换JF Sebastian的获胜的default_dict方法(格式为[(“word”,int),(“Word”,int)],(“word2”)的输出中的键,…])变成小写并剥离它们的标点符号,并折叠在该操作之后保持相同的所有键的计数(代码如下).优点是我们现在正在处理大约5万个条目的列表,而不是> 2M在语料库中.这样我现在在1.25秒内从语料库(作为一个列表)到一个不区分大小写的字数,忽略我的机器上的标点符号,从大约4.5,以Counter方法字符串转换为第一步.但也可能有一个基于字典的方法,我在sum_sorted()中做什么呢?

码:

def striplast(resultlist,lower_or_Case=False):
    """function for string conversion of the output of any of the `count_words*` methods"""
    if lower_or_Case:
        strippedresult = sorted([(entry[0].strip(drop),entry[1]) for entry in resultlist])
    else:
        strippedresult = sorted([(entry[0].lower().strip(drop),entry[1]) for entry in resultlist])
    strippedresult = sum_sorted(strippedresult)
    return strippedresult

def sum_sorted(inputlist):
    """function for collapsing the counts of entries left identical by striplast()"""
    ilindex = 0
    freqs = []
    currentword = inputlist[0][0]
    length = len(inputlist)
    while ilindex < length:
        wordcount = 0
        while ilindex < length and inputlist[ilindex][0] == currentword:
            wordcount += inputlist[ilindex][1]
            ilindex += 1
        if currentword not in [""," "]:
            freqs.append((currentword,wordcount))
        if ilindex < length and inputlist[ilindex][0] > currentword:
            currentword = inputlist[ilindex][0]
    return freqs

def count_words_defaultdict2(words,loc=False): 
    """modified version of J.F. Sebastian's winning method,added a final step collapsing
    the counts for words identical except for punctuation and digits and case (for the 
    latter,unless you specify that you're interested in a case-sensitive count by setting
    l(ower_)o(r_)c(ase) to True) by means of striplast()."""
    d = defaultdict(int)
    for w in words:
        d[w] += 1
    if col=True:
        return striplast(sorted(d.items()),lower_or_case=True)
    else:
        return striplast(sorted(d.items()))

我做了一些首先尝试使用groupy来完成sum_sorted()和/或striplast()所做的工作,但是我无法完全弄清楚如何把它引入到[entry [1]]的列表中count_words结果中的条目按条目[0]排序.我最接近的是:

# "i(n)p(ut)list",toylist for testing purposes:

list(groupby(sorted([(entry[0].lower().strip(drop),entry[1]) for entry in  iplist])))

# returns:

[(('a',1),<itertools._grouper object at 0x1031bb290>),(('a',2),<itertools._grouper object at 0x1031bb250>),3),<itertools._grouper object at 0x1031bb210>),5),<itertools._grouper object at 0x1031bb2d0>),8),<itertools._grouper object at 0x1031bb310>),(('b',<itertools._grouper object at 0x1031bb350>),7),<itertools._grouper object at 0x1031bb390>)]

# So what I used instead for striplast() is based on list comprehension:

list(sorted([(entry[0].lower().strip(drop),entry[1]) for entry in  iplist]))

# returns:

[('a',('a',('b',7)]

解决方法

给出一个排序的单词列表,您是否尝试过itertools.groupby的传统的Pythonic方法
from itertools import groupby
some_data = ['a','a','b','c','c']
count = dict( (k,sum(1 for i in v)) for k,v in groupby(some_data) ) # or
count = {k:sum(1 for i in v) for k,v in groupby(some_data)}
# {'a': 2,'c': 3,'b': 1}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


我最近重新拾起了计算机视觉,借助Python的opencv还有face_recognition库写了个简单的图像识别demo,额外定制了一些内容,原本想打包成exe然后发给朋友,不过在这当中遇到了许多小问题,都解决了,记录一下踩过的坑。 1、Pyinstaller打包过程当中出现warning,跟d
说到Pooling,相信学习过CNN的朋友们都不会感到陌生。Pooling在中文当中的意思是“池化”,在神经网络当中非常常见,通常用的比较多的一种是Max Pooling,具体操作如下图: 结合图像理解,相信你也会大概明白其中的本意。不过Pooling并不是只可以选取2x2的窗口大小,即便是3x3,
记得大一学Python的时候,有一个题目是判断一个数是否是复数。当时觉得比较复杂不好写,就琢磨了一个偷懒的好办法,用异常处理的手段便可以大大程度帮助你简短代码(偷懒)。以下是判断整数和复数的两段小代码: 相信看到这里,你也有所顿悟,能拓展出更多有意思的方法~
文章目录 3 直方图Histogramplot1. 基本直方图的绘制 Basic histogram2. 数据分布与密度信息显示 Control rug and density on seaborn histogram3. 带箱形图的直方图 Histogram with a boxplot on t
文章目录 5 小提琴图Violinplot1. 基础小提琴图绘制 Basic violinplot2. 小提琴图样式自定义 Custom seaborn violinplot3. 小提琴图颜色自定义 Control color of seaborn violinplot4. 分组小提琴图 Group
文章目录 4 核密度图Densityplot1. 基础核密度图绘制 Basic density plot2. 核密度图的区间控制 Control bandwidth of density plot3. 多个变量的核密度图绘制 Density plot of several variables4. 边
首先 import tensorflow as tf tf.argmax(tenso,n)函数会返回tensor中参数指定的维度中的最大值的索引或者向量。当tensor为矩阵返回向量,tensor为向量返回索引号。其中n表示具体参数的维度。 以实际例子为说明: import tensorflow a
seaborn学习笔记章节 seaborn是一个基于matplotlib的Python数据可视化库。seaborn是matplotlib的高级封装,可以绘制有吸引力且信息丰富的统计图形。相对于matplotlib,seaborn语法更简洁,两者关系类似于numpy和pandas之间的关系,seabo
Python ConfigParser教程显示了如何使用ConfigParser在Python中使用配置文件。 文章目录 1 介绍1.1 Python ConfigParser读取文件1.2 Python ConfigParser中的节1.3 Python ConfigParser从字符串中读取数据
1. 处理Excel 电子表格笔记(第12章)(代码下载) 本文主要介绍openpyxl 的2.5.12版处理excel电子表格,原书是2.1.4 版,OpenPyXL 团队会经常发布新版本。不过不用担心,新版本应该在相当长的时间内向后兼容。如果你有新版本,想看看它提供了什么新功能,可以查看Open
1. 发送电子邮件和短信笔记(第16章)(代码下载) 1.1 发送电子邮件 简单邮件传输协议(SMTP)是用于发送电子邮件的协议。SMTP 规定电子邮件应该如何格式化、加密、在邮件服务器之间传递,以及在你点击发送后,计算机要处理的所有其他细节。。但是,你并不需要知道这些技术细节,因为Python 的
文章目录 12 绘图实例(4) Drawing example(4)1. Scatterplot with varying point sizes and hues(relplot)2. Scatterplot with categorical variables(swarmplot)3. Scat
文章目录 10 绘图实例(2) Drawing example(2)1. Grouped violinplots with split violins(violinplot)2. Annotated heatmaps(heatmap)3. Hexbin plot with marginal dist
文章目录 9 绘图实例(1) Drawing example(1)1. Anscombe’s quartet(lmplot)2. Color palette choices(barplot)3. Different cubehelix palettes(kdeplot)4. Distribution
Python装饰器教程展示了如何在Python中使用装饰器基本功能。 文章目录 1 使用教程1.1 Python装饰器简单示例1.2 带@符号的Python装饰器1.3 用参数修饰函数1.4 Python装饰器修改数据1.5 Python多层装饰器1.6 Python装饰器计时示例 2 参考 1 使
1. 用GUI 自动化控制键盘和鼠标第18章 (代码下载) pyautogui模块可以向Windows、OS X 和Linux 发送虚拟按键和鼠标点击。根据使用的操作系统,在安装pyautogui之前,可能需要安装一些其他模块。 Windows: 不需要安装其他模块。OS X: sudo pip3
文章目录 生成文件目录结构多图合并找出文件夹中相似图像 生成文件目录结构 生成文件夹或文件的目录结构,并保存结果。可选是否滤除目录,特定文件以及可以设定最大查找文件结构深度。效果如下: root:[z:/] |--a.py |--image | |--cat1.jpg | |--cat2.jpg |
文章目录 VENN DIAGRAM(维恩图)1. 具有2个分组的基本的维恩图 Venn diagram with 2 groups2. 具有3个组的基本维恩图 Venn diagram with 3 groups3. 自定义维恩图 Custom Venn diagram4. 精致的维恩图 Elabo
mxnet60分钟入门Gluon教程代码下载,适合做过深度学习的人使用。入门教程地址: https://beta.mxnet.io/guide/getting-started/crash-course/index.html mxnet安装方法:pip install mxnet 1 在mxnet中使
文章目录 1 安装2 快速入门2.1 基本用法2.2 输出图像格式2.3 图像style设置2.4 属性2.5 子图和聚类 3 实例4 如何进一步使用python graphviz Graphviz是一款能够自动排版的流程图绘图软件。python graphviz则是graphviz的python实