> For the complete documentation index, see [llms.txt](https://huataihuang.gitbook.io/cloud-atlas-draft/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://huataihuang.gitbook.io/cloud-atlas-draft/machine_learning/cs229/linear_regression_with_one_variable.md).

# 单变量线性回归

监督学习（给出正确的答案），在数据集中作出单变量的线性回归。

* regression(回归)
* linear regression(线性回归)

## 符号

* m = 训练案例的数量
* x's = 输入变量/输入特性（也称为特征量）
* y's = 输出变量/目标变量（也就是我们的预测结果）

我们用(x,y)表示表格中单独的一行对应于一个训练样本

(x上标(i),y上标(i))来表示第i个训练样本，这个上标i表示索引，也就是训练集中的第i行。通过这种方式的一对(x上标(i),y上标(i))来表示训练样本，一系列的训练样本m，称为训练集。

* h = hypothesis(假设)，h表示一个函数

输入变量x，经过h函数，输出预测结果y。我们的目标就是给定一个训练集，学习得到这个h函数。

![模式表达](/files/-M1TARMPZA3XlBdnfmsq)

h(x) = θ0 + Θx

> 单变量线性回归

## 代价函数（cost function）

代价函数（J）也称为平方误差函数，或者平方误差代价函数。通过代价函数来测量hypothesis函数（h函数）的精确性：

![代价函数](/files/-M1TARMSN_KBOv9hMbw1)

预测值和实际值的差的平方，按照数据集m累加起来，然后乘以 1/2m 。我们的目标是尽可能降低这个值，也就是减少预测值和实际值之间的差距。

平方误差代价函数可能是解决回归问题最常用的手段。

![代价函数目标](/files/-M1TARMWYw2cPwKVBy08)

简化的假设：假设 θ0 = 0 ，即回归函数直线经过0原点

轮廓图（contour plot) 是包含很多轮廓线

## 梯度下降算法（Gradient descent）

梯度下降算法来解决代价函数最小化

梯度下降算法的特点：起始点略有不同，会得到非常不同的局部最优解。

斜率等于0，导数项等于0，局部最优解。当斜率减小（导数减小），更新幅度要降低（移动幅度不断降低），直到接近局部最低点，导数值会越来越小，直到0。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://huataihuang.gitbook.io/cloud-atlas-draft/machine_learning/cs229/linear_regression_with_one_variable.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
