Matplotlib 3.0 Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

The following is the code to draw a contour plot of loss function in the given regression problem:

  1. Read the Loss, theta0, and theta1 values from saved files:
Loss = pd.read_excel('Loss.xlsx')
theta0_vals = pd.read_excel('theta0.xlsx')
theta1_vals = pd.read_excel('theta1.xlsx')
  1. Specify the figure size:
fig = plt.figure(figsize=(12,8))
  1. Create the mesh grid for the X and Y coordinates:
X, Y = np.meshgrid(theta0_vals, theta1_vals)
  1. Draw the contour plot and label contours with their corresponding loss values:
CS = plt.contour(X, Y, Loss, np.logspace(-2,3,20), cmap=cm.coolwarm)
plt.clabel(CS, inline=1, fontsize=10)
  1. Display the contour plot on the screen:
plt.show()