errorsFig = plt.figure() ax = errorsFig.add_subplot(111) ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.4f')) ax.plot(range((len(errors))), errors) ax.set_xlabel('Number of iterations') ax.set_ylabel('Cost J') plt.show() size = 100 theta0Vals = np.linspace(-10, 10, size) theta1Vals = np.linspace(-2, 4, size) JVals = np.zeros((size, size)) for i in range(size): for j in range(size): col = np.matrix([[theta0Vals[i]], [theta1Vals[j]]]) JVals[i, j] = re.J(col, X, y) theta0Vals, theta1Vals = np.meshgrid(theta0Vals, theta1Vals) JVals = JVals.T contourSurf = plt.figure() ax = contourSurf.gca(projection='3d') ax.plot_surface(theta0Vals, theta1Vals, JVals, rstride=2, cstride=2, alpha=0.3, cmap='rainbow', linewidth=0, antialiased=False) ax.plot(thetas[0], thetas[1], 'rx') ax.set_xlabel(r'$\theta_0$') ax.set_ylabel(r'$\theta_1$') ax.set_zlabel(r'$J(\theta)$') plt.show()
ax.plot(range(len(errors)), errors) ax.set_xlabel('Number of iterations') ax.set_ylabel('Cost J') plt.show() # 3D cost function size = 100 theta0Vals = np.linspace(-10, 10, size) theta1Vals = np.linspace(-2, 4, size) JVals = np.zeros((size, size)) for i in range(size): for j in range(size): col = np.matrix([[theta0Vals[i]], [theta1Vals[j]]]) JVals[i, j] = regression.J(col, X, y) theta0Vals, theta1Vals = np.meshgrid(theta0Vals, theta1Vals) JVals = JVals.T contourSurf = plt.figure() ax = contourSurf.gca(projection='3d') ax.plot_surface(theta0Vals, theta1Vals, JVals, rstride=2, cstride=2, alpha=0.3, cmap=cm.rainbow, linewidth=0, antialiased=False)
fittingLine, = ax.plot(xCopy[:,1], yHat, color='g') ax.set_xlabel('Population of City in 10,000s') ax.set_ylabel('Profit in $10,000s') plt.legend([trainingSet, fittingLine], ['Training Set', 'Linear Regression']) plt.show() # 绘制能量函数的轮廓 theta1Vals = np.linspace(min(thetas[1]), max(thetas[1]), 100) theta2Vals = np.linspace(min(thetas[2]), max(thetas[2]), 100) JVals = np.zeros((100, 100)) for i in range(100): for j in range(100): theta = np.matrix([[0], [theta1Vals[i]], [theta2Vals[j]]]) JVals[i,j] = regression.J(theta, X, y) contourFig = plt.figure() ax = contourFig.add_subplot(111) ax.contour(theta1Vals, theta2Vals, JVals, np.logspace(-2,3,20)) plt.show() # 打印误差曲线 errorsFig = plt.figure() ax = errorsFig.add_subplot(111) ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.4f')) ax.plot(range(len(errors)), errors) ax.set_xlabel('Number of iterations') ax.set_ylabel('Cost J')