Coursera机器学习编程作业Python实现(Andrew Ng)—— 1.1 Linear regression with one variable
1.1 Linear regression with one variable
import numpy as np
import matplotlib.pyplot as plt
data1 = np.loadtxt('ex1data1.txt', delimiter=',')
Plotting the Data
plt.scatter(data1[:,0], data1[:,1], c='red', marker='x')
plt.xlabel('Population of City in 10,000s')
plt.ylabel('Profit in $10,000s')
plt.show()
x0 = np.ones((len(data1),1))
x1 = data1[:,0]
x1 = x1.reshape([len(x1), 1])
X = np.hstack((x0, x1))
y = data1[:,1]
y = y.reshape([len(y), 1])
theta = np.zeros((2,1))
iterations = 1500
alpha = 0.01
def h(X, theta):
return np.dot(X, theta)
def computeCost(X, theta, y):
return 0.5 * np.mean(np.square(h(X, theta) - y))
def gradientDescent(X, theta, y, iterations, alpha):
Cost = []
Cost.append(computeCost(X, theta, y))
for i in range(iterations):
grad0 = np.mean(h(X, theta) - y)
grad1 = np.mean((h(X, theta) - y) * (X[:,1].reshape([len(X), 1])))
theta[0] = theta[0] - alpha * grad0
theta[1] = theta[1] - alpha * grad1
Cost.append(computeCost(X, theta, y))
return theta, Cost
theta_result, Cost_result = gradientDescent(X, theta, y, iterations, alpha)
theta_result
array([[-3.63029144],
[ 1.16636235]])
x_predict = [X[:,1].min(), X[:,1].max()]
y_predict = [theta_result[0]+theta_result[1]*(X[:,1].min()), theta_result[0]+theta_result[1]*(X[:,1].max())]
plt.plot(x_predict, y_predict, c='blue', label='predict')
plt.scatter(data1[:,0], data1[:,1], c='red', marker='x', label='train_data')
plt.xlabel('Population of City in 10,000s')
plt.ylabel('Profit in $10,000s')
plt.legend()
plt.show()
plt.plot(Cost_result)
plt.xlabel('Iterations')
plt.ylabel('Cost')
plt.show()