# python深度总结线性回归

## 例子

 工资 年龄 额度 4000 25 20000 8000 30 70000 5000 28 35000 7500 33 50000 12000 40 85000

### 通俗解释

X1, X2 代表我们的两个特征: 年龄和工资. Y 代表银行最终会借给我们多少钱.

## 评估方法

R^2 的取值越接近于 1 我们认为模型拟合的越好.

## 案例一

```from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression, SGDRegressor
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

def mylinear():
"""
线性回归直接预测房子价格
:return: None
"""

# 获取数据

# 分割数据记到训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(lb.data, lb.target, test_size=0.25)

# 进行标准化处理, 目标值处理
# 特征值和目标是都必须进行标准化处理, 实例化两个标准化API
std_x = StandardScaler()

x_train = std_x.fit_transform(x_train)
x_test = std_x.fit_transform(x_test)

# 目标值
std_y = StandardScaler()

y_train = std_y.fit_transform(y_train.reshape(-1, 1))
y_test = std_y.fit_transform(y_test.reshape(-1, 1))

# estimator预测
# 正规方程求解方式预测结果
lr = LinearRegression()

lr.fit(x_train, y_train)
print(lr.coef_)

# 预测测试集房子价格
y_lr_predict = std_y.inverse_transform(lr.predict(x_test))
print("正规方程式测试集里面每个房子的预测价格: ", y_lr_predict)
print("正规方程的均方误差: ", mean_squared_error(std_y.inverse_transform(y_test), y_lr_predict))

# 梯度下降去进行房价预测
sgd = SGDRegressor()

sgd.fit(x_train, y_train)
print(sgd.coef_)

# 预测测试集的房子价格
y_sgd_predict = std_y.inverse_transform(sgd.predict(x_test))
print("梯度下降式测试集里面每个房子的预测价格: ", y_sgd_predict)
print("梯度下降的均方误差: ", mean_squared_error(std_y.inverse_transform(y_test), y_sgd_predict))

return None

if __name__ == "__main__":
mylinear()

[[-0.12225698  0.12791281 -0.00206122  0.05700013 -0.2608399   0.28139416
0.01481249 -0.33807474  0.3299154  -0.23182836 -0.21123181  0.09206512
-0.39973041]]

[25.61614205]
[24.20558764]
[19.30978406]
[35.89982059]
[29.03187299]
[26.34111014]
[19.46710495]
[20.6689787 ]
[29.93653292]
[25.11165216]
[32.91673513]
[19.84546548]
[23.5563843 ]
[21.79474763]
[15.75074992]
[19.80615694]
[12.98286759]
[27.59995691]
[19.00192788]
[36.16248095]
[19.2767701 ]
[16.52561836]
[23.05284655]
[16.59241324]
[25.66405442]
[30.7677223 ]
[19.86797053]
[ 9.39422797]
[27.10530759]
[27.17712717]
[39.44877655]
[10.03000383]
[15.42825832]
[23.13702928]
[14.52254261]
[19.38595173]
[29.06816506]
[36.30187936]
[22.5685246 ]
[ 9.88826283]
[21.33573342]
[31.3551175 ]
[16.18170604]
[27.59483437]
[31.66145736]
[14.31706514]
[24.46295319]
[17.51893204]
[19.35269608]
[24.26523283]
[24.86190305]
[25.11947262]
[28.93202524]
[15.75107827]
[13.3417495 ]
[22.59649735]
[29.00114487]
[12.20666867]
[30.63609004]
[21.96199386]
[27.06032461]
[25.1791211 ]
[17.97595194]
[41.57497749]
[21.43625394]
[24.28803424]
[16.5167138 ]
[19.38589021]
[ 8.06164985]
[23.7550887 ]
[12.10636177]
[23.67230518]
[31.52266655]
[19.30684626]
[20.31342004]
[25.13624205]
[18.6725454 ]
[34.44267213]
[19.76331507]
[33.68001958]
[17.21843608]
[11.93697393]
[20.10130687]
[20.60069168]
[33.02551169]
[12.20848437]
[11.34921413]
[36.81923651]
[43.09091788]
[24.5904135 ]
[27.19519096]
[13.42695648]
[21.31070858]
[18.78980458]
[26.7739455 ]
[21.04064808]
[19.37399749]
[20.61932093]
[12.70789542]
[27.30728839]
[29.19812469]
[18.2215341 ]
[14.88442393]
[13.08985585]
[37.26784993]
[23.0054703 ]
[45.03638993]
[24.43103986]
[ 9.70593527]
[ 7.20755399]
[24.11659246]
[16.87989582]
[23.8839    ]
[36.74286927]
[17.52801739]
[21.14217981]
[ 8.33442145]
[20.77366903]
[25.11687425]
[34.79817667]
[17.48069049]
[ 7.79217297]
[21.46168783]
[12.12750804]
[23.37886385]
[13.03642996]]

[-0.10382102  0.09549223 -0.0575206   0.06192685 -0.17919477  0.31416038
-0.0060828  -0.2718829   0.16557575 -0.09171927 -0.19702721  0.09358103
-0.38969764]

26.40901657 19.91790232 21.08280077 30.8745518  25.04025974 32.61880171
20.06776623 23.27211209 21.49391276 15.07364423 19.3604463  13.24307268
27.91816594 18.46564888 36.5121198  18.60090036 17.07584378 23.61453885
15.44119731 26.55848283 30.95932966 20.48910926  8.92774087 25.64122283
26.5405097  39.56312391  9.60876044 16.194631   21.86126606 14.3384503
19.6672515  28.37094255 37.13748452 22.56961348 10.95474568 21.31897902
31.99623025 16.32155785 27.56422641 31.91738771 16.07941322 25.21406318
17.07667764 18.61941274 23.61541029 25.09956295 24.16633871 29.24889477
16.17014144 13.52204965 21.76470038 28.75088192 11.39083277 29.94854783
21.97184713 26.76638021 25.37366415 17.75713168 42.17712979 21.44617697
24.65166416 15.74898705 19.28498974  7.18254411 23.64316345 12.17079475
23.22062874 30.81709679 19.39958374 20.53408606 25.34565728 18.55272456
33.84685681 19.4801645  33.86657711 17.02691146 11.07262797 20.44699002
20.83170047 32.66795247 11.2561216  11.94847677 35.85096014 42.30377951
24.56324407 27.96815655 13.30901928 22.23063794 19.1259557  27.02051826
21.39186325 20.33181273 21.29435341 11.25823767 27.67529642 30.095733
18.76124598 13.85728059 14.68490838 37.53663617 22.46940546 45.09885288
24.49884024 10.51414764  7.91453997 23.66015594 17.30342205 24.23971059
36.76137912 16.98059079 21.46394599  7.28066947 20.76359414 24.55927982
35.63307238 16.9695351   7.33008978 21.71197098 12.31280728 22.41710171
13.31011409]

```