Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

线性回归

回归是一种确定一个变量 (y) 与其他变量 (x) 之间关系的方法。

在统计学中,线性回归是一种对 y 和 x 之间的线性关系进行建模的方法。

在机器学习中,线性回归是一种监督学习算法。

散点图

这是散点图(来自上一章)

示例

const xArray = [50,60,70,80,90,100,110,120,130,140,150];
const yArray = [7,8,8,9,9,9,10,11,14,14,15];

// 定义数据
const data = [{
  x:xArray,
  y:yArray,
  mode: "markers"
}];

// 定义布局
const layout = {
  xaxis: {range: [40, 160], title: "平方米"},
  yaxis: {range: [5, 16], title: "价格(百万)"},
  title: "房价与面积关系"
};

Plotly.newPlot("myPlot", data, layout);
试试看 »

预测值

从上面的散点数据,我们如何预测未来的价格?

  • 使用手绘线性图
  • 对线性关系建模
  • 对线性回归建模


线性图

这是一个线性图,根据最低和最高价格预测价格

示例

const xArray = [50,60,70,80,90,100,110,120,130,140,150];
const yArray = [7,8,8,9,9,9,9,10,11,14,14,15];

const data = [
  {x:xArray, y:yArray, mode:"markers"},
  {x:[50,150], y:[7,15], mode:"line"}
];

const layout = {
  xaxis: {range: [40, 160], title: "平方米"},
  yaxis: {range: [5, 16], title: "价格(百万)"},
  title: "房价与面积关系"
};

Plotly.newPlot("myPlot", data, layout);
试试看 »

来自上一章

线性图可以写成y = ax + b

其中

  • y 是我们要预测的价格
  • a 是直线的斜率
  • x 是输入值
  • b 是截距

线性关系

这个模型使用价格和面积之间的线性关系来预测价格

示例

const xArray = [50,60,70,80,90,100,110,120,130,140,150];
const yArray = [7,8,8,9,9,9,10,11,14,14,15];

// 计算斜率
let xSum = xArray.reduce(function(a, b){return a + b;}, 0);
let ySum = yArray.reduce(function(a, b){return a + b;}, 0);
let slope = ySum / xSum;

// 生成值
const xValues = [];
const yValues = [];
for (let x = 50; x <= 150; x += 1) {
  xValues.push(x);
  yValues.push(x * slope);
}
试试看 »

在上面的示例中,斜率是计算的平均值,截距 = 0。


使用线性回归函数

这个模型使用线性回归函数来预测价格

示例

const xArray = [50,60,70,80,90,100,110,120,130,140,150];
const yArray = [7,8,8,9,9,9,10,11,14,14,15];

// 计算总和
let xSum=0, ySum=0 , xxSum=0, xySum=0;
let count = xArray.length;
for (let i = 0, len = count; i < count; i++) {
  xSum += xArray[i];
  ySum += yArray[i];
  xxSum += xArray[i] * xArray[i];
  xySum += xArray[i] * yArray[i];
}

// 计算斜率和截距
let slope = (count * xySum - xSum * ySum) / (count * xxSum - xSum * xSum);
let intercept = (ySum / count) - (slope * xSum) / count;

// 生成值
const xValues = [];
const yValues = [];
for (let x = 50; x <= 150; x += 1) {
  xValues.push(x);
  yValues.push(x * slope + intercept);
}
试试看 »

多项式回归

如果散点数据点不适合线性回归(穿过这些点的直线),则数据可能适合多项式回归。

多项式回归与线性回归类似,使用变量 x 和 y 之间的关系来找到穿过数据点的最佳线条。 多项式回归

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.