上のテキストのクリーンアップのパフォーマンスの向上しますextractFeatures()
をpandas列に適用し、結果が新しい列を作成しています:は私がDFを持つデータフレーム
df['cleanText'] = df['text'].apply(clean)
結果のDFは:
id cleanText
1 good sentence
2 sentence number
3 third sentence
ループ時間は指数関数的に増大するように見えます。たとえば、%%timeit
を使用して、5行に適用すると、1ループあたり17ミリ秒で実行されます。 300行はループごとに800ミリ秒で実行されます。 500行はループごとに1.26秒で実行されます。
stops
とWordNetLemmatizer()
は、一度だけ呼び出す必要があるため、関数外でインスタンス化して変更しました。 apply
ライン上%prun -l 10
を実行
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
は、この表の結果:
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の候補ですか?私はここを参照してください改善の
あなたのデータと予想される出力がないと言うことはできません。 –
サンプル入力データとクリーニング機能の結果を追加しました。私は適切な出力を得ています。質問はその適切な出力をより早く得る方法に関するものです。 –
興味深い。単語の順序は重要ですか?はい、私は推測していますか? –