> 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/chapter5-2/5-10.md).

# 5-10 线性回归的可解释性和更多思考

```python
import matplotlib.pyplot as plt
from sklearn import datasets

boston = datasets.load_boston()
X = boston.data
y = boston.target
X = X[y < 50.0]
y = y[y < 50.0]

from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(X, y)

lin_reg.coef_
```

输出结果： ![](http://windmissing.github.io/images/2019/65.png)

怎么解释这些数字？

* 系数的正负代表这个特征与房价是正相关还是负相关 &#x20;
* 系数的绝对值大小代码这个特征对房价的影响程度 &#x20;

  输入：`boston.feature_names[np.argsort(lin_reg.coef_)]` &#x20;

  输出：

  ```
  array(['NOX', 'DIS', 'PTRATIO', 'LSTAT', 'CRIM', 'INDUS', 'AGE', 'TAX',
       'B', 'ZN', 'RAD', 'CHAS', 'RM'], dtype='<U7')
  ```

即使使用线性回归法预测的模型不够好，观察的它的系数对分析问题也是有帮助的。

## 线性回归算法的总结

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

| 线性回归算法  | KNN算法                                    |          |
| ------- | ---------------------------------------- | -------- |
| 模型参数    | 典型的参数学习                                  | 非参数学习    |
| 分类问题    | 是很多分类算法的基础                               | 可以解决分类问题 |
| 回归问题    | 只能解决回归问题                                 | 可以解决回归问题 |
| 对数据的假设性 | 有                                        | 没有       |
| 对数据的解释性 | 有                                        | 没有       |
| 时间复杂度   | <p>使用正规方程解，在训练模型时复杂度高。<br>解决方法：梯度下降下</p> | 预测时复杂度高  |
