Code below:
def computerCost(x,y,theta):
m = len(y)
J = np.sum((np.dot(x,theta) - y)**2) /(2*m)
return J
m = 100
x = np.linspace(-5,10,m)
y = np.linspace(1,100,m)
x, y = x.reshape(m,1), y.reshape(m,1)
theta_0 = np.linspace(-10,10,100)
theta_1 = np.linspace(-1,4,100)
X,Y = np.meshgrid(theta_0,theta_1)
###### Here I want to initialize a numpy array with generator.
J_vals = np.array(computerCost(x,y,np.array([a,b])) for a,b in zip(np.ravel(X), np.ravel(Y)) )
print('out:',J_vals)
Running this code in Python 3.5 gives:
out:<generator object <genexpr> at 0x0000028ACF28B258>
The console prints that J_vals is a generator. Is there some way to change the generator into a np.ndarrray?
zip()andfromiter().np.dot(x, theta)wherexis an array of shape(100, 1)andthetais of shape(2,). This will not work. Can you try to explain the math you are trying to do here?forloop and thezip.