I am following Andrew NG course on coursera and I wanted to implement that same logic on python. I am trying to compute cost and theta with
scipy.optimize.fmin_ncg
Here's a code
import numpy as np
from scipy.optimize import fmin_ncg
def sigmoid(z):
return (1 / (1 + np.exp(-z))).reshape(-1, 1)
def compute_cost(theta, X, y):
m = len(y)
hypothesis = sigmoid(np.dot(X, theta))
cost = (1 / m) * np.sum(np.dot(-y.T, (np.log(hypothesis))) - np.dot((1 - y.T), np.log(1 - hypothesis)))
return cost
def compute_gradient(theta, X, y):
m = len(y)
hypothesis = sigmoid(np.dot(X, theta))
gradient = (1 / m) * np.dot(X.T, (hypothesis - y))
return gradient
def main():
data = np.loadtxt("data/data1.txt", delimiter=",") # 100, 3
X = data[:, 0:2]
y = data[:, 2:]
m, n = X.shape
initial_theta = np.zeros((n + 1, 1))
X = np.column_stack((np.ones(m), X))
mr = fmin_ncg(compute_cost, initial_theta, compute_gradient, args=(X, y), full_output=True)
print(mr)
if __name__ == "__main__":
main()
When I try to run this I get and error / exception like below
Traceback (most recent call last):
File "/file/path/without_regression.py", line 78, in <module>
main()
File "/file/path/without_regression.py", line 66, in main
mr = fmin_ncg(compute_cost, initial_theta, compute_gradient, args=(X, y), full_output=True)
File "/usr/local/anaconda3/envs/ml/lib/python3.6/site-packages/scipy/optimize/optimize.py", line 1400, in fmin_ncg
callback=callback, **opts)
File "/usr/local/anaconda3/envs/ml/lib/python3.6/site-packages/scipy/optimize/optimize.py", line 1497, in _minimize_newtoncg
dri0 = numpy.dot(ri, ri)
ValueError: shapes (3,1) and (3,1) not aligned: 1 (dim 1) != 3 (dim 0)
I don't understand this error. May be because I am beginner this to not verbose for me.
How to use scipy.optimize.fmin_ncg
or any other minimization technique such as scipy.optimize.minimize(...)
to compute cost and theta?