• python:最小二乘法拟合原理及代码实现


    这里写目录标题

    原理

    最小二乘法适用于对处理的一堆数据,不必精确的经过每一点,而是根据图像到每个数据点的距离和最小确定函数。需要注意的是,最小二乘是对全局进行拟合优化,对噪声比较敏感,所以如果有噪声比较大的观测值会影响拟合结果,此时建议用RANSAC算法拟合。

    最小二乘法逼近的最简单的例子是根据一组观测值对(x1,y1),(x2,y2)…(xn,yn)来拟合一条直线。
    在这里插入图片描述
    对于 y = a 0 + a 1 x + e y=a_{0} + a_{1}x + e y=a0+a1x+e,在一组观测数据中拟合最好的模型,即使得残差 e e e的平方和最小。分别对 a 0 、 a 1 a_{0}、a_{1} a0a1求偏导,可得:
    ∑ i = 1 n a 0 + ∑ i = 1 n x i a 1 = ∑ i = 1 n y i ∑ i = 1 n x i a 0 + ∑ i = 1 n x i 2 a 1 = ∑ i = 1 n x i y i \begin {matrix} \displaystyle\sum_{i=1}^na_{0} + \displaystyle\sum_{i=1}^nx_{i}a_{1} = \displaystyle\sum_{i=1}^ny_{i} \\ \displaystyle\sum_{i=1}^nx_{i}a_{0} + \displaystyle\sum_{i=1}^nx_{i}^2a_{1} = \displaystyle\sum_{i=1}^nx_{i}y_{i} \end {matrix} i=1na0+i=1nxia1=i=1nyii=1nxia0+i=1nxi2a1=i=1nxiyi
    则:
    a 1 = n ∑ i = 1 n x i y i − ∑ i = 1 n x i ∑ i = 1 n y i n ∑ i = 1 n x i 2 − ( ∑ i = 1 n x i ) 2 a_{1} = \cfrac {n\sum_{i=1}^nx_{i}y_{i} - \sum_{i=1}^n x_{i}\sum_{i=1}^n y_{i}}{n \sum_{i=1}^n x_{i}^2 - (\sum_{i=1}^n x_{i})^2}\\ a1=ni=1nxi2(i=1nxi)2ni=1nxiyii=1nxii=1nyi
    a 0 = y ˉ − a 1 x ˉ a_{0} = \text{\={y}} - a_{1}\text{\={x}} a0=yˉa1xˉ
    对n次多项式同样适用。

    代码实现

    def leastsq_fit(points:np.ndarray):
        points_size = len(points)
        points_sum = np.sum(points, axis=0)
        sum_xy = np.sum(points[:, 0] * points[:, 1], axis=0)
        sum_x = points_sum[0]
        sum_y = points_sum[1]
        sum_sqare_x = np.sum(points ** 2, axis=0)[0]
        average_x = sum_x / points_size
        average_y = sum_y / points_size
        k = (points_size*sum_xy - sum_x*sum_y)/(points_size*sum_sqare_x - sum_x*sum_x)
        b = average_y - average_x*k
        best_model = np.zeros((2, 1), dtype=np.float32)
        best_model[0, 0] = b
        best_model[1, 0] = k
    
        return best_model
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    矩阵形式参考我另外的博客链接1链接2

    def leastsq_mutifunc(x, y, m):
        """
        多项式最小二乘法实现, 矩阵形式
        :param x:输入
        :param y:目标输出
        :param m:多项式阶数
        :return:多项式系数
        """
        x = np.array(x)
        y = np.array(y)
    
        assert m <= x.shape[0], f"the number of m({m}) need less than x's size({x.shape[0]})"
        assert x.shape[0] == y.shape[0], f"the size of x({x.shape[0]}) must equal to y's size({y.shape[0]}"
        x_mat = np.zeros((x.shape[0], m+1))
        for i in range(x.shape[0]):
            x_mat_h = np.zeros((1, m+1))
            for j in range(m+1):
                x_mat_h[0][j] = x[i] ** (m-j)
            x_mat[i] = x_mat_h
        theta = np.dot(np.dot(np.linalg.inv(np.dot(x_mat.T, x_mat)), x_mat.T), y.T)
        return theta
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    参考链接1
    参考链接2
    参考链接3

  • 相关阅读:
    跨境商城源码可以支持多店铺管理模式吗?
    7 进制数字转换
    被骂怕了?微软将允许Windows 11用户一键设置默认浏览器
    边缘计算AI智能安防监控视频平台车辆违停算法详解与应用
    【滤波估计】基于双卡尔曼滤波实现soc和soh联合估计附matlab代码
    集群保持集群负载均衡和hash一致性
    Unity RTS 策略游戏等建造系统仿照COC游戏的插件 - City Building Perfect Kit
    WPF 全局样式资源管理
    构建spotify的electron版本
    如何用小程序端进行测试?
  • 原文地址:https://blog.csdn.net/qq_39506862/article/details/128183684