线性图
机器学习经常使用折线图来显示关系。
折线图显示线性函数的值:y = ax + b
重要关键词
- 线性(直线)
- 斜率(角度)
- 截距(起始值)
线性
线性意味着直线。线性图是一条直线。
图包含两个轴:x轴(水平)和y轴(垂直)。
示例
const xValues = [];
const yValues = [];
// 生成值
for (let x = 0; x <= 10; x += 1) {
xValues.push(x);
yValues.push(x);
}
// 定义数据
const data = [{
x: xValues,
y: yValues,
mode: "lines"
}];
// 定义布局
const layout = {title: "y = x"};
// 使用 Plotly 显示
Plotly.newPlot("myPlot", data, layout);
自己动手试一试 »
斜率
斜率是图的角度。
斜率是线性图中的a值
y = ax
在此示例中,斜率 = 1.2
示例
let slope = 1.2;
const xValues = [];
const yValues = [];
// 生成值
for (let x = 0; x <= 10; x += 1) {
xValues.push(x);
yValues.push(x * slope);
}
// 定义数据
const data = [{
x: xValues,
y: yValues,
mode: "lines"
}];
// 定义布局
const layout = {title: "斜率=" + slope};
// 使用 Plotly 显示
Plotly.newPlot("myPlot", data, layout);
自己动手试一试 »
截距
截距是图的起始值。
截距是线性图中的b值
y = ax + b
在此示例中,斜率 = 1.2 且截距 = 7
示例
let slope = 1.2;
let intercept = 7;
const xValues = [];
const yValues = [];
// 生成值
for (let x = 0; x <= 10; x += 1) {
xValues.push(x);
yValues.push(x * slope + intercept);
}
// 定义数据
const data = [{
x: xValues,
y: yValues,
mode: "lines"
}];
// 定义布局
const layout = {title: "斜率=" + slope + " 截距=" + intercept};
// 使用 Plotly 显示
Plotly.newPlot("myPlot", data, layout);
自己动手试一试 »