import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# 0.绘制样本数据(观察一下)
def plot_sample(data):
x = data[:,0] # 第一列数据
y = data[:,1] # 第二列数据
plt.scatter(x,y)
plt.show()
# 1.计算cost function
def cost_function(data,theta0,theta1):
x = data[:,0]
y = data[:,1]
m = data.shape[0] # 样本数据的数量
cost = np.sum((theta0 + theta1 * x - y) ** 2) / (2 * m)
return cost
# 2.梯度下降
def batch_gredient_descend(data,theta0,theta1,alpha):
x = data[:,0]
y = data[:,1]
m = data.shape[0]
theta0 = theta0 - alpha * np.sum(theta0 + theta1 * x - y) / m
theta1 = theta1 - alpha * np.sum((theta0 + theta1 * x - y) * x) / m
return theta0,theta1
# 3.多次迭代(梯度下降)
def multi_BGD(data,theta0,theta1,alpha,count,cost_list):
for i in range(count):
theta0,theta1 = batch_gredient_descend(data,theta0,theta1,alpha)
if i % 100 == 0:
# 每100次输出一下
print(f'BGD {i}: θ0 = {theta0:.4f} θ1 = {theta1:.4f} cost = {cost_function(data,theta0,theta1):.4f}')
cost = cost_function(data,theta0,theta1)
cost_list.append(cost) # 将每一次的cost记录下来,便于后续绘图观察有无收敛
return theta0,theta1
# 4.绘制拟合曲线
def plot_linear(data,theta0,theta1):
x = data[:,0]
y = data[:,1]
plt.title('Results Of Iterations')
plt.xlabel('x'); plt.ylabel('y')
plt.scatter(x,y)
plt.plot(x,theta0+theta1*x,'r')
plt.show()
def linear_regression():
data = np.loadtxt('data.csv',dtype=np.float32,delimiter=',') # loadtxt读取csv文件
plot_sample(data)
# 参数初始化
theta0 = theta1 = 10
alpha = 0.01
count = 10000 # 梯度下降1000次
cost_list = []
# 输出最开始的参数
print(f'Begin para:θ0 = {theta0:.4f} θ1 = {theta1:.4f} alpha = {alpha:.4f}')
theta0,theta1 = multi_BGD(data,theta0,theta1,alpha,count,cost_list)
# 输出变化后的参数
print(f'End para:θ0 = {theta0:.4f} θ1 = {theta1:.4f} alpha = {alpha:.4f}')
plt.plot(cost_list) #绘制cost变化
plt.show()
plot_linear(data,theta0,theta1)
if __name__ == '__main__':
linear_regression()
data.csv
1,3 1.2,3 1.2,4 1.5,4.5 1.6,4.3 6.5,12 3.6,7.1 2.5,9 5.7,14 6,11 9,17 8.9,17 7.1,15 7,14 2.5,4 0.8,2 0.5,2 3.4,7 3.6,9 5.6,12 6.7,15 6.9,15 7.1,14 7.5,17 7.8,16 8.1,15 8.3,15 8.5,15 8.7,16 8.7,17 8.8,18 8.8,20 8,16 9,19 9.2,18 10.1,20 1.1,3.2 1.6,4.2 4,9 12,25 9.5,20



