ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python – 为什么在处理DataFrame时我的NLTK功能会变慢?

2019-09-16 18:56:38  阅读:872  来源: 互联网

标签:python optimization nltk


我试图在数据集中运行我的百万行的函数.

>我在数据帧中读取CSV中的数据
>我使用drop list删除我不需要的数据
>我通过for循环中的NLTK函数传递它.

码:

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

现在我使用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.punctuation

这些应该是全球性的.

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()]

你可以使用DataFrame.apply

data['Anylize_Text'].apply(preprocess)

标签:python,optimization,nltk
来源: https://codeday.me/bug/20190916/1807827.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有