代码之家  ›  专栏  ›  技术社区  ›  Christopher

提高预处理速度

  •  0
  • Christopher  · 技术社区  · 6 年前

    以下代码用于使用自定义lemmatizer函数预处理文本:

    %%time
    import pandas as pd
    import numpy as np
    from sklearn.model_selection import train_test_split
    from gensim.utils import simple_preprocess, lemmatize
    from gensim.parsing.preprocessing import STOPWORDS
    STOPWORDS = list(STOPWORDS)
    
    def preprocessor(s):
        result = []
        for token in lemmatize(s, stopwords=STOPWORDS, min_length=2):
            result.append(token.decode('utf-8').split('/')[0])
        return result
    
    data = pd.read_csv('https://pastebin.com/raw/dqKFZ12m')
    
    %%time
    X_train, X_test, y_train, y_test = train_test_split([preprocessor(x) for x in data.text],
                                                        data.label, test_size=0.2, random_state=0)
    #10.8 seconds
    

    金属化过程的速度能提高吗?

    lemmatize() 函数似乎是主要的瓶颈,如gensim函数 simple_preprocess 相当快。

    谢谢你的帮助!

    1 回复  |  直到 6 年前
        1
  •  1
  •   gojomo    6 年前

    您可能需要重构代码,以便更容易分别对每个部分进行计时。 lemmatize() 可能是瓶颈的一部分,但其他重要贡献者 也可以是:(1)通过列表一次一个令牌地编写大型文档 .append() (2)utf-8解码。

    另外,gensim 依赖于 parse() 功能来自 Pattern

    最后,由于元素化可能是一个固有的代价高昂的操作,而且可能是同一源数据在您的管道中被多次处理的情况,所以您可能希望设计您的过程,以便将结果重新写入磁盘,然后在后续运行中重新使用,而不是总是“在线”完成。