Example #1
0
plt.plot(X1, Y1, 'r-', label='line',linewidth=1)
plt.show()

input('Program paused. Press ENTER to continue')

# ============= Part 4: Visualizing J(theta_0, theta_1) =============
x_theta0=np.linspace(-10,10,100)
y_theta0=np.linspace(-1,4,100)

# 对x、y数据执行网格化
xx,yy=np.meshgrid(x_theta0,y_theta0)
z_J=np.zeros(xx.shape)
for i in range(0,x_theta0.size):
	for j in range(0,y_theta0.size):
		theta_temp=np.array([x_theta0[i],y_theta0[j]])
		z_J[i][j]=GD.compute_cost(X,Y,theta_temp)

z_J = np.transpose(z_J)

fig = plt.figure(1)
ax = fig.gca(projection='3d')  #get a Axes3D object
ax.plot_surface(xx, yy, z_J)

plt.figure(2)

lvls = np.logspace(-2, 3, 30) 
plt.contour(xx, yy, z_J, levels=lvls, norm=LogNorm())#plot contour
plt.plot(theta[0], theta[1], c='r', marker="x")
print(theta)
#plt.plot(theta[0], theta[1], 'rx', ms=10, lw=2)
plt.show()