> For the complete documentation index, see [llms.txt](https://windmising.gitbook.io/liu-yu-bo-play-with-machine-learning/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://windmising.gitbook.io/liu-yu-bo-play-with-machine-learning/src/chapter9/9-1.md).

# 第九章：逻辑回归

## 什么是逻辑回归

解决分类问题。\
将样本的特征和样本发生的概率联系起来，概率是一个连续的数，因此可以当作回归问题来处理。

对于线性回归，y = f(x)\
对于逻辑回归，p = f(x)，当p>=0.5,y=1，当p<0.5,y=0

逻辑回归即可以看作是回归算法，也可以看作是分类算法。\
通常作为分类算法用，只可以解决二分类问题。

在线性回归中， ![](http://windmissing.github.io/images/2019/155.jpg)\
y的值域为(-inifinity, inifinity)，但概率值的值域\[0, 1]\
解决方法：\
![](http://windmissing.github.io/images/2019/156.jpg)

## Sigmoid函数

![](http://windmissing.github.io/images/2019/157.jpg)

```python
import numpy as np
import matplotlib.pyplot as plt

def sigmoid(t):
    return 1 / (1 + np.exp(-t))

x = np.linspace(-10, 10, 500)
y = sigmoid(x)

plt.plot(x, y)
plt.show()
```

![](http://windmissing.github.io/images/2019/158.png)

曲线的性质：\
左端趋近于但达不到0，右端趋近于但达不到1，即值域(0, 1)\
t>0时，p>0.5。t<0时，p<0.5

![](http://windmissing.github.io/images/2019/159.jpg)

问题：对于给定的样本数据集X,y，我们如何找到参数theta，使得用这样的方式可以最大程度获得样本数据集X对应的分类输出y？
