一元线性回归(最小二乘法)

刚开始学习机器学习,记录一下学的算法程序,以后好回顾

算法分析

假设y = w*x + b, 欲求w、b,最小二乘法就是试图找到一条直线,使所有样本离直线的欧式距离之和最小,按照下图公式
在这里插入图片描述
也就是要使平方误差最小,通过对w、b的求偏导
在这里插入图片描述
令偏导数都为零,解得最小二乘法公式
在这里插入图片描述

程序实现

1、导包

import numpy as np
import matplotlib.pyplot as plt

2、读入数据(这里只有几条数据大致表示一下)

# 数据中只有两列,一列表示x,一列表示y
points = np.genfromtxt('data.csv', delimiter=',')
points

# 提取points中的两列数据, 其中points[:, 0] 表示任意行的第0列
x = points[:, 0]
y = points[:, 1]

# 用plt画出散点图
plt.scatter(x, y)
plt.show()

在这里插入图片描述
3、计算损失函数(此处用均方误差来表示)

# 损失函数是系数的函数, 另外还要传入数据
def compute_cost(w, b, points):
    total_cost = 0
    M = len(points)
    
    # 逐点计算平方误差,然后求平均数
    for i in range(M):
        x = points[i, 0]
        y = points[i, 1]
        total_cost += (y - w*x -b) ** 2;
        
    return total_cost / M  

4、计算最小二乘法算法(将公式转化成程序)

# 先定义一个求均值的函数
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_bar = average(points[:, 0])
    
    sum_yx = 0
    sum_x2 = 0
    for i in range(M):
        x = points[i, 0]
        y = points[i, 1]
        
        sum_yx += y * (x - x_bar)
        sum_x2 += x ** 2
    w = sum_yx / (sum_x2 - M * (x_bar**2))
    
    sum_b = 0
    for i in range(M):
        x = points[i, 0]
        y = points[i, 1]
        
        sum_b += y - w * x
    b = sum_b / M
    
    return w, b

到这一步就计算出w,、b了, 接下来测试一下拟合结果
5、测试

# 测试
w, b = fit(points)

print("w is ", w)
print("b is ", b)

cost = compute_cost(w, b, points)

print("cost is ", cost)
#%%
# 画出拟合曲线
plt.scatter(x, y)
pred_y = w * x + b
plt.plot(x, pred_y, c='r')
plt.show()

在这里插入图片描述
在这里插入图片描述数据太少了,不过大致可以看出来效果,重在分析

另外sklearn机器学习库中有现成的方法可以实现

直接调库

这里放完整代码

import numpy as np
import matplotlib.pyplot as plt

points = np.genfromtxt('data.csv', delimiter=',')
points

# 提取points中的两列数据,分别作为x, y
x = points[:, 0]
y = points[:, 1]

# 用plt画出散点图
plt.scatter(x, y)
plt.show()

# 损失函数是系数的函数, 另外还要传入数据
def compute_cost(w, b, points):
    total_cost = 0
    M = len(points)
    
    # 逐点计算平方误差,然后求平均数
    for i in range(M):
        x = points[i, 0]
        y = points[i, 1]
        total_cost += (y - w*x -b) ** 2;
        
    return total_cost / M  
   
# 线性回归
from sklearn.linear_model import LinearRegression
lr = LinearRegression()


x_new = x.reshape(-1, 1)
y_new = y.reshape(-1, 1)
lr.fit(x_new, y_new) # 传入x, y拟合得结果
#%%
# 从训练模型中提取系数和截距
w = lr.coef_
b = lr.intercept_

print("w is ", w)
print("b is ", b)

cost = compute_cost(w, b, points)

print("cost is ", cost)

w = lr.coef_[0][0]
b = lr.intercept_[0]

pred_y = w * x + b
plt.scatter(x, y)
plt.plot(x, pred_y, c='r')
plt.show()

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值