从pandas DataFrame创建术语密度矩阵的有效方法

7
我正在尝试从Pandas数据帧中创建一个术语密度矩阵,以便评估数据帧中出现的术语。我还想能够保留我的数据的“空间”方面(请参见帖子结尾处的评论,了解我的意思举例)。
我对Pandas和NLTK都不熟悉,因此我认为可以使用某些现有工具来解决我的问题。
我有一个包含两个感兴趣的列的数据帧:称之为“标题”和“页面”。
    import pandas as pd
    import re

    df = pd.DataFrame({'title':['Delicious boiled egg','Fried egg ','Split orange','Something else'], 'page':[1, 2, 3, 4]})
    df.head()

       page                 title
    0     1  Delicious boiled egg
    1     2            Fried egg 
    2     3          Split orange
    3     4        Something else

我的目标是清理文本,并将感兴趣的术语传递到TDM数据框中。我使用两个函数来帮助我清理字符串。

    import nltk.classify
    from nltk.tokenize import wordpunct_tokenize
    from nltk.corpus import stopwords
    import string   

    def remove_punct(strin):
        '''
        returns a string with the punctuation marks removed, and all lower case letters
        input: strin, an ascii string. convert using strin.encode('ascii','ignore') if it is unicode 
        '''
        return strin.translate(string.maketrans("",""), string.punctuation).lower()

    sw = stopwords.words('english')

    def tok_cln(strin):
        '''
        tokenizes string and removes stopwords
        '''
        return set(nltk.wordpunct_tokenize(strin)).difference(sw)

另外还有一个数据框操作的函数。

    def df2tdm(df,titleColumn,placementColumn,newPlacementColumn):
        '''
        takes in a DataFrame with at least two columns, and returns a dataframe with the term density matrix
        of the words appearing in the titleColumn
        Inputs: df, a DataFrame containing titleColumn, placementColumn among others
        Outputs: tdm_df, a DataFrame containing newPlacementColumn and columns with all the terms in df[titleColumn]
        '''
        tdm_df = pd.DataFrame(index=df.index, columns=[newPlacementColumn])
        tdm_df = tdm_df.fillna(0)
        for idx in df.index:
            for word in tok_cln( remove_punct(df[titleColumn][idx].encode('ascii','ignore')) ):
                if word not in tdm_df.columns:
                    newcol = pd.DataFrame(index = df.index, columns = [word])
                    tdm_df = tdm_df.join(newcol)
        tdm_df[newPlacementColumn][idx] = df[placementColumn][idx]
        tdm_df[word][idx] = 1
        return tdm_df.fillna(0,inplace = False)

    tdm_df = df2tdm(df,'title','page','pub_page')
    tdm_df.head()

这将返回

      pub_page boiled egg delicious fried orange split something else
    0        1      1   1         1     0      0     0         0    0
    1        2      0   1         0     1      0     0         0    0
    2        3      0   0         0     0      1     1         0    0
    3        4      0   0         0     0      0     0         1    1

但是,当解析大量数据集(数十万行,数千列的输出)时,速度非常缓慢。我的两个问题:

我能加速这个实现吗?

有没有其他工具可以使用来完成这项工作?

我希望能够保留数据的“空间”方面,例如如果“egg”在1-10页中经常出现,然后在500-520页中再次频繁出现,我想知道这一点。

2个回答

20
您可以使用scikit-learn的 CountVectorizer
In [14]: from sklearn.feature_extraction.text import CountVectorizer

In [15]: countvec = CountVectorizer()

In [16]: countvec.fit_transform(df.title)
Out[16]: 
<4x8 sparse matrix of type '<type 'numpy.int64'>'
    with 9 stored elements in Compressed Sparse Column format>

它返回稀疏表示的词项-文档矩阵,因为这种矩阵通常是巨大且稀疏的。

对于您的特定示例,我猜将其转换回DataFrame仍然可以正常工作:

In [17]: pd.DataFrame(countvec.fit_transform(df.title).toarray(), columns=countvec.get_feature_names())
Out[17]: 
   boiled  delicious  egg  else  fried  orange  something  split
0       1          1    1     0      0       0          0      0
1       0          0    1     0      1       0          0      0
2       0          0    0     0      0       1          0      1
3       0          0    0     1      0       0          1      0

[4 rows x 8 columns]

1
太好了,sklearn的实现比我之前的快80倍。谢谢! - nikosd
我在一个函数中实现了你的代码,但它给我的数据带来了非常大的内存开销。我已经开了一个新的线程讨论这个问题,请查看此链接。非常感谢您提供的任何进一步帮助!谢谢! - nikosd
在最后一步中,.toarray() 对我无效,但是 .todense() 有效。 - Ben Ogorek

0
提供了一种处理这个问题的方法,但我只是想指出使用Python集合创建术语密度数据结构是不可取的,因为集合是唯一对象的集合。您将无法捕获每个单词的计数,只能捕获给定行中单词的存在。
return set(nltk.wordpunct_tokenize(strin)).difference(sw)

为了去除停用词,您可以尝试这样做:
tokens_stripped = [token for token in tokens 
                   if token not in stopwords]

分词后。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接