0
流動的代碼是一個簡單的python循環。爲什麼這個循環慢慢地運行?
def getBestWeightsByRandomGradientAscent(featureDatasList, classTypes, maxCycles=1):
"""
:param featureDatasList:
:param classTypes:
:param maxCycles: the loop time
:return:
"""
import random
featureDatas = np.array(featureDatasList)
m, n = np.shape(featureDatas)
weights = np.ones(n)
# the loop goes here... #
for j in range(maxCycles):
featureIndexs = range(m)
featureLen = len(featureIndexs)
for i in range(m):
delta = 4/(1.0 + i + j) + 0.01
randIndex = int(random.uniform(0, featureLen))
sigmodInput = sum(featureDatas[randIndex] * weights)
estimateClass = calculateSigmodEstimateClassType(sigmodInput)
error = classTypes[randIndex] - estimateClass
weights += (error * delta) * featureDatas[randIndex]
del (featureIndexs[randIndex])
return weights
我發現,當我運行這個循環1000次以上,它在開始跑得快,但它運行,並最終保持低速它會越來越慢......好奇,我不爲什麼。這是由變量的範圍還是我的硬件問題造成的?我該如何解決這個問題?非常感謝!
的方式來分析你的代碼你正在更新'權重'數組嗎? –