def plot_fit(min_x, max_x, mu, sigma, theta, p): x = np.arange(min_x - 15, max_x + 25, 0.05) x_poly = poly_features(x, p) for i in range(np.shape(x_poly)[0]): x_poly[i] = (x_poly[i] - mu) / sigma x_poly = np.append(np.ones((x_poly.shape[0], 1)), x_poly, axis=1) plt.plot(x, np.dot(x_poly, theta), '--')
def plot_fit(min_x, max_x, mu, sigma, theta, p): """ plot a polynomial fit """ x = np.arange(min_x - 15, max_x + 25, 0.05) X_poly = poly_features(x, p) X_poly -= mu X_poly /= sigma X_poly = np.c_[np.ones(x.size), X_poly] plt.plot(x, np.dot(X_poly, theta))
def plot_fit(min_x, max_x, mu, sigma, theta, p): # We plot a range slightly bigger than the min and max values to get # an idea of how the fit will vary outside the range of the data points x = np.arange(min_x - 15, max_x + 25, 0.05).T # Map the X values X_poly = poly_features(x, p) X_poly = X_poly - mu X_poly = X_poly / sigma # Add ones X_poly = np.column_stack((np.ones(x.shape[0]), X_poly)) # Plot plt.plot(x, X_poly.dot(theta), '--', lw=2)
def plot_fit(min_x, max_x, mu, sigma, theta, p, lmd): x = np.arange(min_x - 15, max_x + 15, 0.1) x = x.reshape((len(x), 1)) X_poly = pf.poly_features(x, p) X_poly -= mu X_poly /= sigma X_poly = np.c_[np.ones(x.size), X_poly] plt.plot(x, np.dot(X_poly, theta)) plt.xlabel('Change in water level (x)') plt.ylabel('Water folowing out of the dam (y)') plt.ylim([-20, 60]) plt.title('Polynomial Regression Fit (lambda = {})'.format(lmd)) plt.show()
plt.ylabel('Error') plt.axis([0, 13, 0, 150]) #plt.xticks(list(range(0,13,2))) #plt.yticks(list(range(0,200,50))) input('Program paused. Press ENTER to continue') # ===================== Part 6 : Feature Mapping for Polynomial Regression ===================== # One solution to this is to use polynomial regression. You should now # complete polyFeatures to map each example into its powers # p = 8 # Map X onto Polynomial Features and Normalize X_poly = pf.poly_features(X, p) X_poly, mu, sigma = fn.feature_normalize(X_poly) X_poly = np.c_[np.ones(m), X_poly] # Map X_poly_test and normalize (using mu and sigma) X_poly_test = pf.poly_features(Xtest, p) X_poly_test -= mu X_poly_test /= sigma X_poly_test = np.c_[np.ones(X_poly_test.shape[0]), X_poly_test] # Map X_poly_val and normalize (using mu and sigma) X_poly_val = pf.poly_features(Xval, p) X_poly_val -= mu X_poly_val /= sigma X_poly_val = np.c_[np.ones(X_poly_val.shape[0]), X_poly_val]