最小二乘法推导及实现

一、公式推导

直线:
y = w x + b y = wx+b y=wx+b
损失函数:
L = ∑ i = 1 n ( w x i + b − y i ) 2 L = \sum_{i=1}^{n}(wx_i+b-y_i)^2 L=i=1n(wxi+byi)2
最小二乘法的核心思想就是让损失函数最小,即找出损失函数的最小值。

在高等数学中,要求一个函数的最小值的思路为:1.对函数求导 2.让导数等于0

求某一个w,b使得L最小

  1. 先对L求偏导
    ∂ L ∂ b = ∂ ∑ i = 1 n ( w x i + b − y i ) 2 ∂ b \frac{\partial{L}}{\partial{b}} = \frac{\partial{\sum_{i=1}^{n}(wx_i+b-y_i)^2}}{\partial{b}} bL=bi=1n(wxi+byi)2

    ∂ L ∂ b = 2 ∑ i = 1 n ( w x i + b − y i ) \frac{\partial{L}}{\partial{b}}=2\sum_{i=1}^{n}(wx_i+b-y_i) bL=2i=1n(wxi+byi)

    ∂ L ∂ b = 2 ( ∑ i = 1 n w x i + ∑ i = 1 n b − ∑ i = 1 n y i ) \frac{\partial{L}}{\partial{b}} = 2(\sum_{i=1}^{n}wx_i+\sum_{i=1}^{n}b-\sum_{i=1}^{n}y_i) bL=2(i=1nwxi+i=1nbi=1nyi)


    x ˉ = ∑ i = 1 n x i n \bar{x} = \frac{\sum_{i=1}^{n}x_i}{n} xˉ=ni=1nxi

    ∂ L ∂ b = 2 ( w n x ˉ + n b − n y ˉ ) \frac{\partial{L}}{\partial{b}} = 2(wn\bar{x} + nb - n\bar{y}) bL=2(wnxˉ+nbnyˉ)

    ∂ L ∂ b = 2 n ( w x ˉ + b − y ˉ ) \frac{\partial{L}}{\partial{b}} = 2n(w\bar{x}+b-\bar{y}) bL=2n(wxˉ+byˉ)

    要使
    ∂ L ∂ b = 0 \frac{\partial{L}}{\partial{b}} = 0 bL=0

    b = y ˉ − w x ˉ b = \bar{y} - w\bar{x} b=yˉwxˉ
    2.L对w求偏导
    ∂ L ∂ w = ∑ i = 1 n 2 ( w x i + b − y i ) x i \frac{\partial{L}}{\partial{w}} = \sum_{i=1}^{n}2(wx_i+b-y_i)x_i wL=i=1n2(wxi+byi)xi

∂ L ∂ w = 2 ∑ i = 1 n ( w x i 2 + b x i − x i y i ) \frac{\partial{L}}{\partial{w}} = 2\sum_{i=1}^{n}(wx_i^2+bx_i-x_iy_i) wL=2i=1n(wxi2+bxixiyi)

∂ L ∂ w = 2 ( ∑ i = 1 n w x i 2 + ∑ i = 1 n b x i − ∑ i = 1 n x i y i ) \frac{\partial{L}}{\partial{w}} = 2(\sum_{i=1}^{n}wx_i^2 + \sum_{i=1}^{n}bx_i-\sum_{i=1}^{n}x_iy_i) wL=2(i=1nwxi2+i=1nbxii=1nxiyi)

由于
b = y ˉ − w x ˉ b = \bar{y} - w\bar{x} b=yˉwxˉ
所以
∂ L ∂ w = 2 ( ∑ i = 1 n w x i 2 + ∑ i = 1 n ( y ˉ − w x ˉ ) x i − ∑ i = 1 n x i y i ) \frac{\partial{L}}{\partial{w}} = 2(\sum_{i=1}^{n}wx_i^2 + \sum_{i=1}^{n}(\bar{y}-w\bar{x})x_i-\sum_{i=1}^{n}x_iy_i) wL=2(i=1nwxi2+i=1n(yˉwxˉ)xii=1nxiyi)

∂ L ∂ w = 2 ( ∑ i = 1 n w x i 2 + n x ˉ ( y ˉ − w x ˉ ) − ∑ i = 1 n x i y i ) \frac{\partial{L}}{\partial{w}} = 2(\sum_{i=1}^{n}wx_i^2 + n\bar{x}(\bar{y}-w\bar{x})-\sum_{i=1}^{n}x_iy_i) wL=2(i=1nwxi2+nxˉ(yˉwxˉ)i=1nxiyi)

∂ L ∂ w = 2 ( ∑ i = 1 n w x i 2 + n x ˉ y ˉ − n w x ˉ 2 − ∑ i = 1 n x i y i ) \frac{\partial{L}}{\partial{w}} = 2(\sum_{i=1}^{n}wx_i^2 + n\bar{x}\bar{y}-nw\bar{x}^2-\sum_{i=1}^{n}x_iy_i) wL=2(i=1nwxi2+nxˉyˉnwxˉ2i=1nxiyi)

∂ L ∂ w = 2 ( w ( ∑ i = 1 n x i 2 − n x ˉ 2 ) + n x ˉ y ˉ − ∑ i = 1 n x i y i ) ) \frac{\partial{L}}{\partial{w}} = 2(w(\sum_{i=1}^{n}x_i^2-n\bar{x}^2)+n\bar{x}\bar{y}-\sum_{i=1}^{n}x_iy_i)) wL=2(w(i=1nxi2nxˉ2)+nxˉyˉi=1nxiyi))

要使
∂ L ∂ w = 0 \frac{\partial{L}}{\partial{w}} = 0 wL=0

w = ∑ i = 1 n x i y i − n x ˉ y ˉ ∑ i = 1 n x i 2 − n x ˉ 2 w = \frac{\sum_{i=1}^{n}x_iy_i-n\bar{x}\bar{y}}{\sum_{i=1}^{n}x_i^2-n\bar{x}^2} w=i=1nxi2nxˉ2i=1nxiyinxˉyˉ

w = ∑ i = 1 n ( x − x ˉ ) ( y − y ˉ ) ∑ i = 1 n ( x − x ˉ ) 2 w = \frac{\sum_{i=1}^{n}(x-\bar{x})(y-\bar{y})}{\sum_{i=1}^{n}(x-\bar{x})^2} w=i=1n(xxˉ)2i=1n(xxˉ)(yyˉ)

结论:
w = ∑ i = 1 n ( x i − x ˉ ) ( y i − y ˉ ) ∑ i = 1 n ( x i − x ˉ ) 2 w = \frac{\sum_{i=1}^{n}(x_i-\bar{x})(y_i-\bar{y})}{\sum_{i=1}^{n}(x_i-\bar{x})^2} w=i=1n(xixˉ)2i=1n(xixˉ)(yiyˉ)

b = y ˉ − w x ˉ b = \bar{y} - w\bar{x} b=yˉwxˉ

二、代码实现

import numpy as np
x = np.linspace(0, 100, 200)
noise = np.random.normal(loc=10, scale=20, size=200)
y = 3*x + 10 + noise

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

plt.scatter(x, y, c='c')
plt.title('X-Y数据图')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

代码实现

# 先写一个计算loss的函数
def cal_L(w, b, x, y):
    L = 0
    for i in range(len(x)):
        L += (w*x[i] + b - y[i])**2
    return L   

def find_wb(x, y):
    x_mean = np.mean(x)
    y_mean = np.mean(y)
    w = sum((x - x_mean)*(y-y_mean)) / sum(((x - x_mean)**2))
    b = y_mean - w*x_mean
    return w,b



w, b = find_wb(x, y)

cal_L(w, b, x, y)

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

最小二乘法推导及实现_第1张图片

你可能感兴趣的:(最小二乘法,python,机器学习)