我在 Numpy 中实现了这个梯度下降:
def gradientDescent(X, y, theta, alpha, iterations):
m = len(y)
for i in range(iterations):
h = np.dot(X,theta)
loss = h-y
theta = theta - (alpha/m)*np.dot(X.T, loss) #update theta
return theta
虽然代码的其他部分在这里完全矢量化,但仍有一个 for 循环在我看来是不可能消除的;特别要求在每一步更新 theta 我不知道如何对其进行矢量化或以更有效的方式编写它。
感谢您的帮助