######################################################################## ##============= Part 1: Training a univariate model ===================# ######################################################################## # Predict median home value from percentage of lower economic status in a census tract # add the column of ones to X to represent the intercept term XX = np.vstack([np.ones((X.shape[0],)),X]).T from linear_regressor import LinearRegressor, LinearReg_SquaredLoss # set up a linear regression model linear_reg1 = LinearReg_SquaredLoss() # run gradient descent J_history1 = linear_reg1.train(XX,y,learning_rate=0.005,num_iters=10000,verbose=True) # print the theta found print 'Theta found by gradient_descent: ',linear_reg1.theta # plot the linear fit and save it in fig2.pdf plt.plot(X, np.dot(XX,linear_reg1.theta), 'g-',linewidth=3) plt.savefig('fig2.pdf') # Plot the convergence graph and save it in fig4.pdf