import numpy as np
import matplotlib.pyplot as plt
# 利用最小二乘法定义损失函数,w与b是预测函数的未知参数,ponits是坐标信息
def compute_cost(w, b, points):
# 实际值与估计值之差的平方和
total_cost = 0
M = len(points)
for i in range(M):
x = points[i, 0] # x坐标值
y = points[i, 1] # y坐标值
# 最小二乘法公式
total_cost += (y - w * x - b) ** 2
# 返回累加和的平均值
return total_cost / M
# 定义一个求均值的函数
def average(data):
sum = 0
num = len(data)
for i in range(num):
sum += data[i]
return sum / num
# 定义核心拟合函数
def fit(points):
M = len(points)
x_avg = average(points[:, 0])
sum_w_up = 0
sum_x2 = 0
sum_delta = 0
for i in range(M):
x = points[i, 0]
y = points[i, 1]
sum_w_up += y * (x - x_avg)
sum_x2 += x ** 2
# 根据公式计算w
w = sum_w_up / (sum_x2 - M * (x_avg ** 2))
for i in range(M):
x = points[i, 0]
y = points[i, 1]
sum_delta += (y - w * x)
# 根据公式计算b
b = sum_delta / M
return w, b
# 导入外部数据
points = np.genfromtxt('data.csv', delimiter=',')
w, b = fit(points)
cost = compute_cost(w, b, points)
x = points[:, 0]
y = points[:, 1]
# 将数据制作成散点图
plt.scatter(x, y)
pred_y = w * x + b # 自动与x内的每一个元素都进行一次操作,并赋值给pred_y
plt.plot(x, pred_y, c='r')
# 展示画出来的图
plt.show()
数据文件百度云自取,此代码写法是将其与.py
文件放置与同一目录下
链接:https://pan.baidu.com/s/1CanV_l_-968bukjz8VHr2w
提取码:lwoa