损失函数
梯度下降函数
维度
X(m,n)y(m,1) theta(n,1)
代码
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# 线性回归(单变量)
# 预测food truck的收益值。ex1data1.txt:数据集,第一列表示城市人数,第二列表示该城市的food truck收益
path = 'ex1data1.txt'
data = pd.read_csv(path, header=None, names=['Population', 'Profit'])
data.head()
data.describe()
data.plot(kind='scatter', x='Population', y='Profit', figsize=(12, 8))
plt.show()
# 代价函数
# 新增一例,x0
data.insert(0, 'Ones', 1)
data.head()
cols = data.shape[1]
X = data.iloc[:, 0:cols - 1]
Y = data.iloc[:, cols - 1:cols] # 步长
X.head()
Y.head()
X = np.matrix(X.values)
Y = np.matrix(Y.values)
theta = np.matrix(np.array([0, 0]))
X.shape, Y.shape, theta.shape
# 代价函数
def computeCost(X, Y, theta): # theta.T表示对参数矩阵theta进行转置操作
inner = np.power((X * theta.T) - Y, 2)
return np.sum(inner) / (2 * len(X))
computeCost(X, Y, theta)
# difference = computeCost(X, Y, theta)
# print(difference)
# 梯度下降
def gradientDescent(X, Y, theta, alpha, iters):
temp = np.matrix(np.zeros(theta.shape))
parameters = int(theta.shape[1])
cost = np.zeros(iters)
for i in range(iters):
error = X * theta.T - Y
for j in range(parameters):
term = np.multiply(error, X[:, j])
temp[0, j] = temp[0, j] - alpha / len(X) * np.sum(term)
theta = temp
cost[i] = computeCost(X, Y, theta)
return theta, cost
alpha = 0.01 # 学习率
iters = 1000 # 是指完成一个epoch所需的迭代次数
g, cost = gradientDescent(X, Y, theta, alpha, iters)
g
# print(g)
# 计算训练模型的误差
computeCost(X, Y, theta)
# 画出拟合图像
x = np.linspace(data.Population.min(), data.Population.max(), 100)
f = g[0, 0] + g[0, 1] * x
plt.figure(figsize=(12, 8))
plt.xlabel('Population')
plt.ylabel('Profit')
l1 = plt.plot(x, f, label='Prediction', color='red')
l2 = plt.scatter(data.Population, data.Profit, label='Traing Data', )
plt.legend(loc='best')
plt.title('Predicted Profit vs Population Size')
plt.show()
# 画出cost的走势
plt.figure(figsize=(12, 8))
plt.xlabel('Iterations')
plt.ylabel('Cost')
plt.title('Error vs Training Epoch')
plt.plot(np.arange(iters), cost, 'r')
plt.show()
图像展示
附
各阶段数据集可以自己打印查看