Python 为什么我的NLTK函数在处理数据帧时很慢?

Python 为什么我的NLTK函数在处理数据帧时很慢?,python,optimization,nltk,Python,Optimization,Nltk,我试图在一个数据集中用我的百万行运行一个函数 我在数据框中从CSV读取数据 我使用删除列表删除不需要的数据 我通过for循环中的NLTK函数传递它 代码: 现在我使用for循环调用上面的函数,以遍历一百万条记录。即使我在一个有24核cpu和88 GB Ram的重型服务器上,我看到循环占用了太多时间,没有使用现有的计算能力 我这样调用上面的函数 data = pd.read_excel(scrPath + "UserData_Full.xlsx", encoding='utf-8') dropli

我试图在一个数据集中用我的百万行运行一个函数

  • 我在数据框中从CSV读取数据
  • 我使用删除列表删除不需要的数据
  • 我通过for循环中的NLTK函数传递它
  • 代码:

    现在我使用for循环调用上面的函数,以遍历一百万条记录。即使我在一个有24核cpu和88 GB Ram的重型服务器上,我看到循环占用了太多时间,没有使用现有的计算能力

    我这样调用上面的函数

    data = pd.read_excel(scrPath + "UserData_Full.xlsx", encoding='utf-8')
    droplist = ['Submitter', 'Environment']
    data.drop(droplist,axis=1,inplace=True)
    
    #Merging the columns company and detailed description
    
    data['Anylize_Text']= data['Company'].astype(str) + ' ' + data['Detailed_Description'].astype(str)
    
    finallist =[]
    
    for eachlist in data['Anylize_Text']:
        z = nlkt(eachlist)
        finallist.append(z)
    
    当我们有几百万条记录时,上面的代码工作得非常好,只是太慢了。这只是excel中的一个示例记录,但实际数据将以DB为单位,并将以几亿的速度运行。是否有任何方法可以加快操作速度以更快地通过函数传递数据-使用更高的计算能力?

    您原来的
    nlkt()
    每行循环3次

    def nlkt(val):
        val=repr(val)
        clean_txt = [word for word in val.split() if word.lower() not in stopwords.words('english')]
        nopunc = [char for char in str(clean_txt) if char not in string.punctuation]
        nonum = [char for char in nopunc if not char.isdigit()]
        words_string = ''.join(nonum)
        return words_string
    
    此外,每次调用
    nlkt()
    ,您都会一次又一次地初始化它们

    • stopwords.words('english')
    • string.标点符号
    这些应该是全球性的

    stoplist = stopwords.words('english') + list(string.punctuation)
    
    逐行检查:

    val=repr(val)
    
    我不知道你为什么要这么做。但您可以轻松地将列强制转换为
    str
    类型。这应该在预处理函数之外完成

    希望这是不言自明的:

    >>> import pandas as pd
    >>> df = pd.DataFrame([[0, 1, 2], [2, 'xyz', 4], [5, 'abc', 'def']])
    >>> df
       0    1    2
    0  0    1    2
    1  2  xyz    4
    2  5  abc  def
    >>> df[1]
    0      1
    1    xyz
    2    abc
    Name: 1, dtype: object
    >>> df[1].astype(str)
    0      1
    1    xyz
    2    abc
    Name: 1, dtype: object
    >>> list(df[1])
    [1, 'xyz', 'abc']
    >>> list(df[1].astype(str))
    ['1', 'xyz', 'abc']
    
    现在转到下一行:

    clean_txt = [word for word in val.split() if word.lower() not in stopwords.words('english')]
    
    使用
    str.split()。否则,您的标点符号可能会与前面的单词卡住,例如

    >>> from nltk.corpus import stopwords
    >>> from nltk import word_tokenize
    >>> import string
    >>> stoplist = stopwords.words('english') + list(string.punctuation)
    >>> stoplist = set(stoplist)
    
    >>> text = 'This is foo, bar and doh.'
    
    >>> [word for word in text.split() if word.lower() not in stoplist]
    ['foo,', 'bar', 'doh.']
    
    >>> [word for word in word_tokenize(text) if word.lower() not in stoplist]
    ['foo', 'bar', 'doh']
    
    还应同时检查
    .isdigit()

    >>> text = 'This is foo, bar, 234, 567 and doh.'
    >>> [word for word in word_tokenize(text) if word.lower() not in stoplist and not word.isdigit()]
    ['foo', 'bar', 'doh']
    
    将所有这些放在一起,您的
    nlkt()
    应该如下所示:

    def preprocess(text):
        return [word for word in word_tokenize(text) if word.lower() not in stoplist and not word.isdigit()]
    
    您可以使用:


    “拉丁美洲和加勒比海地区”是一种地区主义,在印度文化圈之外并不被普遍理解。不清楚你在用
    finallist
    做什么。它真的需要包含所有的句子吗,或者你能一次处理一个句子吗?
    nlkt
    函数包含大量临时变量,因此它在处理一个调用时会消耗大约10倍的内存,但在完成调用时会被释放。finallist用于标记化,然后通过神经网络模型。。。。。删除lacs单词并相应编辑#准备标记器t=tokenizer()t.fit_on_text(finallist)vocab_size=len(t.word_index)+1#整数编码文档编码(t.texts=t.text_to_sequences(finallist)padded_docs=pad_sequences(encoded(docs,maxlen=max_len,padding='post'))我现在不担心记忆力。我主要关心的是如何加快传递nlkt函数行的过程并加速循环。我的服务器目前几乎没有使用1%的CPU或内存,而通过for loopSee也非常有用,解释得很好。非常感谢。
    def preprocess(text):
        return [word for word in word_tokenize(text) if word.lower() not in stoplist and not word.isdigit()]
    
    data['Anylize_Text'].apply(preprocess)