Пример #1
0
# 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

plot_utils.plot_data(range(len(J_history1)),J_history1,'Number of iterations','Cost J')
plt.savefig('fig4.pdf')