{% raw %}
# 1.1. 广义线性模型

校验者:
        [@专业吹牛逼的小明](https://github.com/apachecn/scikit-learn-doc-zh)
        [@Gladiator](https://github.com/apachecn/scikit-learn-doc-zh)
        [@Loopy](https://github.com/loopyme)
        [@qinhanmin2014](https://github.com/qinhanmin2014)
翻译者:
        [@瓜牛](https://github.com/apachecn/scikit-learn-doc-zh)
        [@年纪大了反应慢了](https://github.com/apachecn/scikit-learn-doc-zh)
        [@Hazekiah](https://github.com/apachecn/scikit-learn-doc-zh)
        [@BWM-蜜蜂](https://github.com/apachecn/scikit-learn-doc-zh)

本章主要讲述一些用于回归的方法,其中目标值 y 是输入变量 x 的线性组合。 数学概念表示为:如果 ![\hat{y}](img/047826f1c2e6f2687b304cb5217be8d8.jpg) 是预测值,那么有:

![\hat{y}(w, x) = w_0 + w_1 x_1 + ... + w_p x_p](img/4ee9f6c666393981b6458e54c3ec89d0.jpg)

在整个模块中,我们定义向量 ![w = (w_1,..., w_p)](img/b003858334d1ad594207911e84219151.jpg) 作为 `coef_` ,定义 ![w_0](img/57e15e43b846791e47a202e1a9a5d8ce.jpg) 作为 `intercept_` 。

如果需要使用广义线性模型进行分类,请参阅 [logistic 回归](#1111-logistic-回归) 。

## 1.1.1. 普通最小二乘法

[`LinearRegression`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html#sklearn.linear_model.LinearRegression) 拟合一个带有系数 ![w = (w_1, ..., w_p)](img/3f5adc0c9b0e51a0759ed6ac49f94431.jpg) 的线性模型,使得数据集实际观测数据和预测数据(估计值)之间的残差平方和最小。其数学表达式为:

![\underset{w}{min\,} {|| X w - y||_2}^2](img/1b6228a71a038f66ac7b8a2743adf4e7.jpg)

[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_ols_0011.png](img/af7b81123e6cdf0b42acec802041beef.jpg)](https://scikit-learn.org/stable/auto_examples/linear_model/plot_ols.html)

[`LinearRegression`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html#sklearn.linear_model.LinearRegression) 会调用 `fit` 方法来拟合数组 X, y,并且将线性模型的系数 ![w](img/8a58e8df6a985a3273e39bac7dd72b1f.jpg) 存储在其成员变量 `coef_` 中:

```py
>>> from sklearn import linear_model
>>> reg = linear_model.LinearRegression()
>>> reg.fit ([[0, 0], [1, 1], [2, 2]], [0, 1, 2])
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
>>> reg.coef_
array([ 0.5,  0.5])

```