线性建模:最小二乘法(机器学习基础教程)

栏目: 数据库 · 发布时间: 7年前

内容简介:问题转化为求 最小时, 的值,即求偏导然后联立求解令:

问题转化为求 最小时, 的值,即

求偏导然后联立求解

令:

求得:

更复杂的线性关系模型

利用矩阵推导更一般的模型

令:

得到:

平均损失函数可以表示为:

求偏导:

得到:

即:

简单线性模型拟合实现

推导出来结果后,代码比较简单了,我是用js写的

// 模型
// y = w0 + w1*x
export class Liner {
  constructor(public inputs: number[] = [], public outputs: number[] = []) {}
//   求w1
  getW1(): number {
    const xt = this.inputs.map((item: number, index: number) => {
      return item * this.outputs[index];
    });
    const xx = this.inputs.map(item => item * item);
    const xtMean = Liner.mean(xt);
    const _x = Liner.mean(this.inputs);
    const _y = Liner.mean(this.outputs);
    return (xtMean - _x * _y) / (Liner.mean(xx) - _x * _x);
  }
  // 求w0
  getW0() {
    return Liner.mean(this.outputs) - this.getW1() * Liner.mean(this.inputs);
  }
  // 预测值
  get(input: number) {
    return this.getW0() + this.getW1() * input;
  }
  // 求平均值
  static mean(arr: number[]): number {
    return arr.reduce((a, b) => a + b, 0) / arr.length;
  }
}
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Spring Into HTML and CSS

Spring Into HTML and CSS

Molly E. Holzschlag / Addison-Wesley Professional / 2005-5-2 / USD 34.99

The fastest route to true HTML/CSS mastery! Need to build a web site? Or update one? Or just create some effective new web content? Maybe you just need to update your skills, do the job better. Welco......一起来看看 《Spring Into HTML and CSS》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具