I am implementing Andrew ng's machine learning course in python. In programming exercise 2, on the first question, I am getting write answers for cost function and gradient but when calculation optimised theta, I am getting a disastrous answer!
I have already tried my best but not able to find the error
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def cost_compute( theta,x, y):
J = (-1/m) * np.sum(np.multiply(Y, np.log(sigmoid(X @ theta)))
+ np.multiply((1-Y), np.log(1 - sigmoid(X @ theta))))
return J
[m, n] = X.shape
X = np.hstack( (np.ones((m,1)) , X) )
Y = Y[:, np.newaxis]
theta = np.zeros((n+1,1))
def grad( theta, X, Y):
temp = (1/m) * X.T @ (sigmoid(X @ theta) - Y)
return temp
temp = opt.fmin_tnc(func = cost_compute, x0 = theta.flatten() , fprime = grad , args = (X, Y.flatten()))
print(temp)
the expected cost is 0.693 and I am getting it. The expected grad is also exactly the same as actual answer. But the optimized theta i am getting is array([4.42735730e-05, 5.31690927e-03, 4.98646266e-03], giving me the new cost around 60! (instead of 0.203)