# Train set # Plot the cost for all iterations fig, ax1 = plt.subplots() plot_cost(cost_vector, ax1) plot_filename = os.path.join(os.getcwd(), 'figures', 'ex2_train_cost.png') plt.savefig(plot_filename) min_cost = np.min(cost_vector) argmin_cost = np.argmin(cost_vector) print('Final training cost: {:.5f}'.format(cost_vector[-1])) print('Minimum training cost: {:.5f}, on iteration #{}'.format( min_cost, argmin_cost + 1)) # plot our data and decision boundary fig, ax1 = plt.subplots() ax1 = plot_data_function(X_train_normalized, y_train, ax1) plot_boundary(X_train_normalized, theta_final, ax1) # save the plotted decision boundary as a figure plot_filename = os.path.join(os.getcwd(), 'figures', 'ex2_decision_boundary_train.png') plt.savefig(plot_filename) ################################################### # Test set cost_test = compute_cost(X_test_normalized, y_test, theta_final) print('Final test cost: {:.5f}'.format(cost_test)) fig, ax1 = plt.subplots() ax1 = plot_data_function(X_test_normalized, y_test, ax1) plot_boundary(X_test_normalized, theta_final, ax1)
from load_data_ex1 import * from normalize_features import * import matplotlib.pyplot as plt from plot_data_function import * # this loads our data X, y = load_data_ex1() # this normalizes our data X_normalized, mean_vec, std_vec = normalize_features(X) # After normalizing, we append a column of ones to X_normalized, as the bias term column_of_ones = np.ones((X_normalized.shape[0], 1)) # append column to the dimension of columns (i.e., 1) X_normalized = np.append(column_of_ones, X_normalized, axis=1) fig, ax1 = plt.subplots() ax1 = plot_data_function(X_normalized, y, ax1) # enter non-interactive mode of matplotlib, to keep figures open plt.ioff() plt.show()
from load_data_ex1 import * from normalize_features import * import matplotlib.pyplot as plt from plot_data_function import * # this loads our data X, y = load_data_ex1() # this normalizes our data #X_normalized, mean_vec, std_vec = normalize_features(X) # After normalizing, we append a column of ones to X_normalized, as the bias term column_of_ones = np.ones((X.shape[0], 1)) # append column to the dimension of columns (i.e., 1) X = np.append(column_of_ones, X, axis=1) fig, ax1 = plt.subplots() ax1 = plot_data_function(X, y, ax1) # enter non-interactive mode of matplotlib, to keep figures open plt.ioff() plt.show()