It seems that the following code finds the gradient descent correctly:
def gradientDescent(x, y, theta, alpha, m, numIterations):
xTrans = x.transpose()
for i in range(0, numIterations):
hypothesis = np.dot(x, theta)
loss = hypothesis - y
cost = np.sum(loss ** 2) / (2 * m)
print("Iteration %d | Cost: %f" % (i, cost))
# avg gradient per example
gradient = np.dot(xTrans, loss) / m
# update
theta = theta - alpha * gradient
return theta
Now suppose we have the following sample data:
For the 1st row of sample data, we will have:
x = [2104, 5, 1, 45]
, theta = [1,1,1,1]
, y = 460
.
However, we are nowhere specifying in the lines :
hypothesis = np.dot(x, theta)
loss = hypothesis - y
which row of the sample data to consider. Then how come this code is working fine ?