私はHMMとビタビアルゴリズムを使って誤植を提案するコードを書いています。テキストの各単語のある時点で、私は次のことをしなければならない。あなたはそれは計算に関与していると私は実行すると、それが実行するのに時間がかかりすぎる見ることができるようにPython:非常に遅い実行ループ
#FYI Windows 10, 64bit, interl i7 4GRam, Python 2.7.3
import numpy as np
import pandas as pd
for k in range(10000):
tempWord = corruptList20[k] #Temp word read form the list which has all of the words
delta = np.zeros(26, len(tempWord)))
sai = np.chararray(26, len(tempWord)))
sai[:] = '@'
# INITIALIZATION DELTA
for i in range(26):
delta[i][0] = #CALCULATION matrix read and multiplication each cell is different
# INITILIZATION END
# 6.DELTA CALCULATION
for deltaIndex in range(1, len(tempWord)):
for j in range(26):
tempDelta = 0.0
maxDelta = 0.0
maxState = ''
for i in range(26):
# CALCULATION to fill each cell involve in:
# 1-matrix read and multiplication
# 2 Finding Column Max
# logical operation and if-then-else operations
# 7. SAI BACKWARD TRACKING
delta2 = pd.DataFrame(delta)
sai2 = pd.DataFrame(sai)
proposedWord = np.zeros(len(tempWord), str)
editId = 0
for col in delta2.columns:
# CALCULATION to fill each cell involve in:
# 1-matrix read and multiplication
# 2 Finding Column Max
# logical operation and if-then-else operations
editList20.append(''.join(editWord))
#END OF LOOP
(私は10,000語を持っていると仮定します)。 現在、
のPython 2.7.3を私のラップトップが盗まれたと私は、Windows 10、64、4GRamでこれを実行して私の質問:誰もが、私は最適化に使用できる任意のポイントを見ることができますか?ループを次のラウンドに移してメモリを解放する前に、ループで作成した行列を削除する必要がありますか?これは自動的に行われますか?
以下のコメント後とxrange
代わりにrange
を使用して性能が30%によってほとんど増加しました。今回の変更後、ここにスクリーンショットを追加しています。反復処理するために番号が取り込まリスト全体を構築range
コメントで述べたように、range
の挙動は2でのPython 2および3
間で変化
何のPythonのバージョンであるのですか?私は識別部分を見ることができません。私は正確に覚えていませんが、 'range'は反復範囲オブジェクトではなくリストを作成しているかもしれません。 – Carcigenicate
質問してくれてありがとう:Python 2.7.3 – Rebin
そのバージョンの範囲の動作を調べます。私は私がちょうどrange'が実際に反復する前にリストを構築する必要性を回避xrange'代わりに ''の使用はPython 2ではPython2 – Carcigenicate