我有一个DF:提高对数据帧文本清理的性能
id text
1 This is a good sentence
2 This is a sentence with a number: 2015
3 This is a third sentence
我有一个文本清洗功能:
def clean(text):
lettersOnly = re.sub('[^a-zA-Z]',' ', text)
tokens = word_tokenize(lettersOnly.lower())
stops = set(stopwords.words('english'))
tokens = [w for w in tokens if not w in stops]
tokensPOS = pos_tag(tokens)
tokensLemmatized = []
for w in tokensPOS:
tokensLemmatized.append(WordNetLemmatizer().lemmatize(w[0], get_wordnet_pos(w[1])))
clean = " ".join(tokensLemmatized)
return clean
get_wordnet_pos()
是这样的:
def get_wordnet_pos(treebank_tag):
if treebank_tag.startswith('J'):
return wordnet.ADJ
elif treebank_tag.startswith('V'):
return wordnet.VERB
elif treebank_tag.startswith('N'):
return wordnet.NOUN
elif treebank_tag.startswith('R'):
return wordnet.ADV
else:
return wordnet.NOUN
我我正在将extractFeatures()
应用到熊猫专栏,并创建一个新结果列:
df['cleanText'] = df['text'].apply(clean)
得到的DF:
id cleanText
1 good sentence
2 sentence number
3 third sentence
循环时出现成倍增长。例如,使用%%timeit
,将其应用于五行,每个循环以17 ms运行。 300行以每个循环800毫秒运行。 500行以每循环1.26秒运行。
我通过实例化stops
和WordNetLemmatizer()
以外的函数来改变它,因为这些函数只需要调用一次。
stops = set(stopwords.words('english'))
lem = WordNetLemmatizer()
def clean(text):
lettersOnly = re.sub('[^a-zA-Z]',' ', text)
tokens = word_tokenize(lettersOnly.lower())
tokens = [w for w in tokens if not w in stops]
tokensPOS = pos_tag(tokens)
tokensLemmatized = []
for w in tokensPOS:
tokensLemmatized.append(lem.lemmatize(w[0], get_wordnet_pos(w[1])))
clean = " ".join(tokensLemmatized)
return clean
在apply
线运行%prun -l 10
导致该表:
672542 function calls (672538 primitive calls) in 2.798 seconds
Ordered by: internal time
List reduced from 211 to 10 due to restriction <10>
ncalls tottime percall cumtime percall filename:lineno(function)
4097 0.727 0.000 0.942 0.000 perceptron.py:48(predict)
4500 0.584 0.000 0.584 0.000 {built-in method nt.stat}
3500 0.243 0.000 0.243 0.000 {built-in method nt._isdir}
14971 0.157 0.000 0.178 0.000 {method 'sub' of '_sre.SRE_Pattern' objects}
57358 0.129 0.000 0.155 0.000 perceptron.py:250(add)
4105 0.117 0.000 0.201 0.000 {built-in method builtins.max}
184365 0.084 0.000 0.084 0.000 perceptron.py:58(<lambda>)
4097 0.057 0.000 0.213 0.000 perceptron.py:245(_get_features)
500 0.038 0.000 1.220 0.002 perceptron.py:143(tag)
2000 0.034 0.000 0.068 0.000 ntpath.py:471(normpath)
它看起来像恶搞感知是,可以预见,采取了大量的资源,但我不知道如何简化它。另外,我不确定nt.stat
或nt._isdir
在哪里被调用。
我该如何改变功能或应用方法来提高性能?这个函数是Cython还是Numba的候选人?
不能说没有你的数据和预期的输出。 –
增加样品输入数据和清洁功能的结果。我得到了正确的输出 - 问题更多的是如何更快地获得适当的输出。 –
有趣。言语的顺序是否重要?我猜是的? –