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]])
predict1 = np.dot(np.array([1, 3.5]), theta_result)
predict1
array([0.45197679])