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)
def computerCost(x,y,thet