线性回归

波士顿房价预测案例

步骤

  • 导入数据
  • 数据分割
  • 数据标准化
  • 正规方程预测
  • 梯度下降预测
# 导入模块
import pandas as pd # 导入数据
from sklearn.model_selection import train_test_split # 数据分割
from sklearn.preprocessing import StandardScaler # 数据标准化
from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge # 正规方程,梯度下降, 岭回归
from sklearn.metrics import mean_squared_error # 均方差
import numpy as np
# 读取Boston房价数据
boston = pd.read_csv("./boston_house_prices.csv")
y = boston["MEDV"] # MEDV为离散型目标值
x = boston.drop(["MEDV"],axis=1) # 其他数据为特征值
x
.dataframe tbody tr th:only-of-type { vertical-align: middle }
\3cpre>\3ccode>.dataframe tbody tr th { vertical-align: top }
.dataframe thead th { text-align: right }

CRIM ZN INDUS CHAS NOX RM AGE DIS RAD TAX PTRATIO B LSTAT
0 0.00632 18.0 2.31 0 0.538 6.575 65.2 4.0900 1 296 15.3 396.90 4.98
1 0.02731 0.0 7.07 0 0.469 6.421 78.9 4.9671 2 242 17.8 396.90 9.14
2 0.02729 0.0 7.07 0 0.469 7.185 61.1 4.9671 2 242 17.8 392.83 4.03
3 0.03237 0.0 2.18 0 0.458 6.998 45.8 6.0622 3 222 18.7 394.63 2.94
4 0.06905 0.0 2.18 0 0.458 7.147 54.2 6.0622 3 222 18.7 396.90 5.33
... ... ... ... ... ... ... ... ... ... ... ... ... ...
501 0.06263 0.0 11.93 0 0.573 6.593 69.1 2.4786 1 273 21.0 391.99 9.67
502 0.04527 0.0 11.93 0 0.573 6.120 76.7 2.2875 1 273 21.0 396.90 9.08
503 0.06076 0.0 11.93 0 0.573 6.976 91.0 2.1675 1 273 21.0 396.90 5.64
504 0.10959 0.0 11.93 0 0.573 6.794 89.3 2.3889 1 273 21.0 393.45 6.48
505 0.04741 0.0 11.93 0 0.573 6.030 80.8 2.5050 1 273 21.0 396.90 7.88

506 rows × 13 columns

# 数据标准化需要传入二维数组,所以需要改变目标值的形状
y = np.array(y).reshape(-1, 1)
# 划分测试集和训练集
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)
# 特征值标准化
std_x = StandardScaler().fit(x_train)
x_train = std_x.transform(x_train)
x_test = std_x.transform(x_test)
# 因为特征值标准化后,传入模型的系数会增大,所以目标值也需要进行标准化
std_y = StandardScaler().fit(y_train)
y_train = std_y.transform(y_train)
y_test = std_y.transform(y_test)
# 实例化线性回归
lr = LinearRegression()
# 传入测试集训练模型
lr.fit(x_train,y_train)

#sk-container-id-1 { color: rgba(0, 0, 0, 1); background-color: rgba(255, 255, 255, 1) }
#sk-container-id-1 pre { padding: 0 }
#sk-container-id-1 div.sk-toggleable { background-color: rgba(255, 255, 255, 1) }
#sk-container-id-1 label.sk-toggleable__label { cursor: pointer; display: block; width: 100%; margin-bottom: 0; padding: 0.3em; box-sizing: border-box; text-align: center }
#sk-container-id-1 label.sk-toggleable__label-arrow:before { content: "▸"; float: left; margin-right: 0.25em; color: rgba(105, 105, 105, 1) }
#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before { color: rgba(0, 0, 0, 1) }
#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before { color: rgba(0, 0, 0, 1) }
#sk-container-id-1 div.sk-toggleable__content { max-height: 0; max-width: 0; overflow: hidden; text-align: left; background-color: rgba(240, 248, 255, 1) }
#sk-container-id-1 div.sk-toggleable__content pre { margin: 0.2em; color: rgba(0, 0, 0, 1); border-radius: 0.25em; background-color: rgba(240, 248, 255, 1) }
#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content { max-height: 200px; max-width: 100%; overflow: auto }
#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before { content: "▾" }
#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label { background-color: rgba(212, 235, 255, 1) }
#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label { background-color: rgba(212, 235, 255, 1) }
#sk-container-id-1 input.sk-hidden--visually { border: 0; clip: rect(1px, 1px, 1px, 1px); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute; width: 1px }
#sk-container-id-1 div.sk-estimator { font-family: monospace; background-color: rgba(240, 248, 255, 1); border: 1px dotted rgba(0, 0, 0, 1); border-radius: 0.25em; box-sizing: border-box; margin-bottom: 0.5em }
#sk-container-id-1 div.sk-estimator:hover { background-color: rgba(212, 235, 255, 1) }
#sk-container-id-1 div.sk-parallel-item::after { content: ""; width: 100%; border-bottom: 1px solid rgba(128, 128, 128, 1); flex-grow: 1 }
#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label { background-color: rgba(212, 235, 255, 1) }
#sk-container-id-1 div.sk-serial::before { content: ""; position: absolute; border-left: 1px solid rgba(128, 128, 128, 1); box-sizing: border-box; top: 0; bottom: 0; left: 50%; z-index: 0 }
#sk-container-id-1 div.sk-serial { display: flex; flex-direction: column; align-items: center; background-color: rgba(255, 255, 255, 1); padding-right: 0.2em; padding-left: 0.2em; position: relative }
#sk-container-id-1 div.sk-item { position: relative; z-index: 1 }
#sk-container-id-1 div.sk-parallel { display: flex; align-items: stretch; justify-content: center; background-color: rgba(255, 255, 255, 1); position: relative }
#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before { content: ""; position: absolute; border-left: 1px solid rgba(128, 128, 128, 1); box-sizing: border-box; top: 0; bottom: 0; left: 50%; z-index: -1 }
#sk-container-id-1 div.sk-parallel-item { display: flex; flex-direction: column; z-index: 1; position: relative; background-color: rgba(255, 255, 255, 1) }
#sk-container-id-1 div.sk-parallel-item:first-child::after { align-self: flex-end; width: 50% }
#sk-container-id-1 div.sk-parallel-item:last-child::after { align-self: flex-start; width: 50% }
#sk-container-id-1 div.sk-parallel-item:only-child::after { width: 0 }
#sk-container-id-1 div.sk-dashed-wrapped { border: 1px dashed rgba(128, 128, 128, 1); margin: 0 0.4em 0.5em; box-sizing: border-box; padding-bottom: 0.4em; background-color: rgba(255, 255, 255, 1) }
#sk-container-id-1 div.sk-label label { font-family: monospace; font-weight: bold; display: inline-block; line-height: 1.2em }
#sk-container-id-1 div.sk-label-container { text-align: center }
#sk-container-id-1 div.sk-container { display: inline-block !important; position: relative }
#sk-container-id-1 div.sk-text-repr-fallback { display: none }

LinearRegression()

In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

LinearRegression
LinearRegression()
# 查看线性回归的回归系数
lr.coef_
array([[-0.11432612,  0.12922939,  0.05168773,  0.0306429 , -0.27800333,
0.26465189, 0.02894241, -0.34962992, 0.31569604, -0.24717234,
-0.26784233, 0.11032066, -0.41354896]])
# 线性回归预测测试集的目标值,std_y.inverse_transform:返回标准化之前的值(反标准化)
y_lr_predict = std_y.inverse_transform(lr.predict(x_test))
y_lr_predict
array([[16.88302519],
[25.67464426],
[24.11685261],
[23.56287231],
[33.21442377],
[17.44428398],
[25.08538719],
[14.36188824],
[23.8507796 ],
[33.90875038],
[30.19255243],
[13.30811675],
[28.60383216],
[34.6094617 ],
[27.32666762],
[24.88310221],
[21.97377504],
[14.36080511],
[15.19834144],
[18.91688837],
[14.39284881],
[37.4279415 ],
[28.85628069],
[23.47343089],
[30.65979144],
[20.77177982],
[21.29899429],
[13.81410752],
[24.36591359],
[26.91067836],
[19.39456288],
[32.1620506 ],
[19.55908532],
[24.32677646],
[31.64841534],
[30.24445789],
[32.6601561 ],
[25.45770231],
[24.36812628],
[24.89892187],
[39.51204317],
[18.25845589],
[30.78050699],
[32.2023306 ],
[43.40712056],
[25.5830554 ],
[24.18175285],
[22.22948918],
[16.30284868],
[27.20443307],
[ 4.3558633 ],
[18.24971547],
[17.84402513],
[14.26170574],
[13.64455453],
[34.67825232],
[ 8.26805278],
[23.65092602],
[ 6.3965518 ],
[21.25451713],
[15.71560149],
[29.29210802],
[29.4266973 ],
[19.91658528],
[14.95841515],
[20.88449625],
[28.59263417],
[23.78937845],
[23.4489951 ],
[11.0440392 ],
[19.4491492 ],
[15.48416226],
[18.68260651],
[24.20199734],
[15.78191346],
[14.11243619],
[22.94901405],
[24.02549373],
[21.11185284],
[28.57665473],
[ 7.45548609],
[22.77052456],
[ 3.44149312],
[15.93067248],
[25.72200382],
[22.56825235],
[32.70873719],
[17.86289514],
[24.49691931],
[35.25395986],
[26.98360999],
[17.51000169],
[28.08531514],
[21.15268973],
[24.73138251],
[-4.82364972],
[21.34031184],
[21.89560028],
[16.35765837],
[35.32764197],
[40.95997005],
[23.59853443],
[19.92593809],
[34.43871021],
[21.37340243],
[20.48191389],
[23.77537201],
[28.67150943],
[40.73850694],
[29.38542779],
[21.25032737],
[22.15530128],
[31.1447006 ],
[17.18008197],
[38.09276107],
[18.17714902],
[26.01850231],
[13.73181577],
[12.47399654],
[27.01659936],
[18.62962667],
[11.26915964],
[19.48824649],
[23.64510406],
[18.88328087],
[19.49037977],
[13.58238162]])
# 线性回归预测的均方差(损失值)
loss_lr = mean_squared_error(std_y.inverse_transform(y_test), y_lr_predict)
loss_lr
27.89401984711536
# 实例化梯度下降回归
sgd = SGDRegressor()
sgd.fit(x_train, y_train)
D:\DeveloperTools\Anaconda\lib\site-packages\sklearn\utils\validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)

#sk-container-id-2 { color: rgba(0, 0, 0, 1); background-color: rgba(255, 255, 255, 1) }
#sk-container-id-2 pre { padding: 0 }
#sk-container-id-2 div.sk-toggleable { background-color: rgba(255, 255, 255, 1) }
#sk-container-id-2 label.sk-toggleable__label { cursor: pointer; display: block; width: 100%; margin-bottom: 0; padding: 0.3em; box-sizing: border-box; text-align: center }
#sk-container-id-2 label.sk-toggleable__label-arrow:before { content: "▸"; float: left; margin-right: 0.25em; color: rgba(105, 105, 105, 1) }
#sk-container-id-2 label.sk-toggleable__label-arrow:hover:before { color: rgba(0, 0, 0, 1) }
#sk-container-id-2 div.sk-estimator:hover label.sk-toggleable__label-arrow:before { color: rgba(0, 0, 0, 1) }
#sk-container-id-2 div.sk-toggleable__content { max-height: 0; max-width: 0; overflow: hidden; text-align: left; background-color: rgba(240, 248, 255, 1) }
#sk-container-id-2 div.sk-toggleable__content pre { margin: 0.2em; color: rgba(0, 0, 0, 1); border-radius: 0.25em; background-color: rgba(240, 248, 255, 1) }
#sk-container-id-2 input.sk-toggleable__control:checked~div.sk-toggleable__content { max-height: 200px; max-width: 100%; overflow: auto }
#sk-container-id-2 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before { content: "▾" }
#sk-container-id-2 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label { background-color: rgba(212, 235, 255, 1) }
#sk-container-id-2 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label { background-color: rgba(212, 235, 255, 1) }
#sk-container-id-2 input.sk-hidden--visually { border: 0; clip: rect(1px, 1px, 1px, 1px); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute; width: 1px }
#sk-container-id-2 div.sk-estimator { font-family: monospace; background-color: rgba(240, 248, 255, 1); border: 1px dotted rgba(0, 0, 0, 1); border-radius: 0.25em; box-sizing: border-box; margin-bottom: 0.5em }
#sk-container-id-2 div.sk-estimator:hover { background-color: rgba(212, 235, 255, 1) }
#sk-container-id-2 div.sk-parallel-item::after { content: ""; width: 100%; border-bottom: 1px solid rgba(128, 128, 128, 1); flex-grow: 1 }
#sk-container-id-2 div.sk-label:hover label.sk-toggleable__label { background-color: rgba(212, 235, 255, 1) }
#sk-container-id-2 div.sk-serial::before { content: ""; position: absolute; border-left: 1px solid rgba(128, 128, 128, 1); box-sizing: border-box; top: 0; bottom: 0; left: 50%; z-index: 0 }
#sk-container-id-2 div.sk-serial { display: flex; flex-direction: column; align-items: center; background-color: rgba(255, 255, 255, 1); padding-right: 0.2em; padding-left: 0.2em; position: relative }
#sk-container-id-2 div.sk-item { position: relative; z-index: 1 }
#sk-container-id-2 div.sk-parallel { display: flex; align-items: stretch; justify-content: center; background-color: rgba(255, 255, 255, 1); position: relative }
#sk-container-id-2 div.sk-item::before, #sk-container-id-2 div.sk-parallel-item::before { content: ""; position: absolute; border-left: 1px solid rgba(128, 128, 128, 1); box-sizing: border-box; top: 0; bottom: 0; left: 50%; z-index: -1 }
#sk-container-id-2 div.sk-parallel-item { display: flex; flex-direction: column; z-index: 1; position: relative; background-color: rgba(255, 255, 255, 1) }
#sk-container-id-2 div.sk-parallel-item:first-child::after { align-self: flex-end; width: 50% }
#sk-container-id-2 div.sk-parallel-item:last-child::after { align-self: flex-start; width: 50% }
#sk-container-id-2 div.sk-parallel-item:only-child::after { width: 0 }
#sk-container-id-2 div.sk-dashed-wrapped { border: 1px dashed rgba(128, 128, 128, 1); margin: 0 0.4em 0.5em; box-sizing: border-box; padding-bottom: 0.4em; background-color: rgba(255, 255, 255, 1) }
#sk-container-id-2 div.sk-label label { font-family: monospace; font-weight: bold; display: inline-block; line-height: 1.2em }
#sk-container-id-2 div.sk-label-container { text-align: center }
#sk-container-id-2 div.sk-container { display: inline-block !important; position: relative }
#sk-container-id-2 div.sk-text-repr-fallback { display: none }

SGDRegressor()

In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

SGDRegressor
SGDRegressor()
# 查看梯度下降回归的回归系数
sgd.coef_
array([-0.09761234,  0.08895746, -0.02421963,  0.02879482, -0.17976106,
0.30861884, -0.00250273, -0.27224473, 0.12435245, -0.0780263 ,
-0.24480836, 0.12012805, -0.38888841])
# 梯度下降回归预测测试集的目标值,std_y.inverse_transform:返回标准化之前的值(反标准化)
y_sgd_predict = std_y.inverse_transform(sgd.predict(x_test).reshape(-1,1))
y_sgd_predict
array([[15.21420286],
[24.63693863],
[24.39828101],
[24.13982716],
[32.78620978],
[17.93179618],
[26.15279053],
[14.48966421],
[23.47566531],
[33.17239509],
[31.84452891],
[12.45562282],
[27.95300787],
[33.80241039],
[28.49956651],
[24.66480492],
[22.36941513],
[12.77314567],
[16.19679874],
[19.55497851],
[16.56475828],
[37.33119072],
[28.7775393 ],
[20.96986273],
[30.61621249],
[21.02209026],
[21.7295418 ],
[12.81210827],
[24.5110437 ],
[26.43938704],
[18.35264658],
[32.65009183],
[18.43526582],
[23.00618081],
[31.7400822 ],
[29.04743561],
[33.05208407],
[25.74448792],
[24.50083552],
[25.60223044],
[39.54513459],
[17.1185942 ],
[31.03740088],
[31.08938082],
[43.05539907],
[25.73953331],
[24.94663261],
[22.54125585],
[18.28413619],
[26.10355346],
[ 6.00742562],
[17.91294014],
[18.30811745],
[12.44053594],
[12.80928627],
[35.3744289 ],
[ 9.09787342],
[22.93659674],
[ 5.43064498],
[21.74836536],
[14.35146387],
[29.01003788],
[29.08635743],
[22.73088123],
[14.63525207],
[21.85792442],
[27.65781677],
[23.792957 ],
[24.6814747 ],
[10.92976509],
[19.83990001],
[15.96966791],
[18.14900105],
[25.20832651],
[13.27422495],
[14.30232772],
[23.11242467],
[25.77201334],
[19.68444307],
[28.57611678],
[ 7.63364889],
[20.4696819 ],
[ 2.27690801],
[16.55235057],
[25.58622675],
[22.77961526],
[32.47346299],
[17.77241159],
[22.97811939],
[36.08937688],
[26.73491284],
[18.29474336],
[29.46454709],
[21.71750293],
[26.04970043],
[-5.49919448],
[22.22155065],
[22.98441588],
[15.12536374],
[35.73982924],
[40.87874356],
[23.690842 ],
[20.5993433 ],
[35.69123855],
[20.68804356],
[20.94190843],
[26.02227126],
[31.17410177],
[40.95630421],
[29.90544672],
[23.50763821],
[22.27432439],
[29.64014839],
[16.78407484],
[38.12893576],
[17.69781499],
[25.22891716],
[14.21875615],
[12.55974345],
[26.99891265],
[17.65595579],
[ 8.4159419 ],
[19.90142312],
[22.80759632],
[19.16843753],
[19.42995139],
[14.04081021]])
# 梯度下降回归预测的均方差(损失值)
loss_sgd = mean_squared_error(std_y.inverse_transform(y_test), y_sgd_predict)
loss_sgd
28.05592202385498
# 实例化岭回归 param:alpha(正则化力度)
rd = Ridge(alpha=1.0)
# 传入训练集 训练模型
rd.fit(x_train,y_train)

#sk-container-id-3 { color: rgba(0, 0, 0, 1); background-color: rgba(255, 255, 255, 1) }
#sk-container-id-3 pre { padding: 0 }
#sk-container-id-3 div.sk-toggleable { background-color: rgba(255, 255, 255, 1) }
#sk-container-id-3 label.sk-toggleable__label { cursor: pointer; display: block; width: 100%; margin-bottom: 0; padding: 0.3em; box-sizing: border-box; text-align: center }
#sk-container-id-3 label.sk-toggleable__label-arrow:before { content: "▸"; float: left; margin-right: 0.25em; color: rgba(105, 105, 105, 1) }
#sk-container-id-3 label.sk-toggleable__label-arrow:hover:before { color: rgba(0, 0, 0, 1) }
#sk-container-id-3 div.sk-estimator:hover label.sk-toggleable__label-arrow:before { color: rgba(0, 0, 0, 1) }
#sk-container-id-3 div.sk-toggleable__content { max-height: 0; max-width: 0; overflow: hidden; text-align: left; background-color: rgba(240, 248, 255, 1) }
#sk-container-id-3 div.sk-toggleable__content pre { margin: 0.2em; color: rgba(0, 0, 0, 1); border-radius: 0.25em; background-color: rgba(240, 248, 255, 1) }
#sk-container-id-3 input.sk-toggleable__control:checked~div.sk-toggleable__content { max-height: 200px; max-width: 100%; overflow: auto }
#sk-container-id-3 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before { content: "▾" }
#sk-container-id-3 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label { background-color: rgba(212, 235, 255, 1) }
#sk-container-id-3 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label { background-color: rgba(212, 235, 255, 1) }
#sk-container-id-3 input.sk-hidden--visually { border: 0; clip: rect(1px, 1px, 1px, 1px); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute; width: 1px }
#sk-container-id-3 div.sk-estimator { font-family: monospace; background-color: rgba(240, 248, 255, 1); border: 1px dotted rgba(0, 0, 0, 1); border-radius: 0.25em; box-sizing: border-box; margin-bottom: 0.5em }
#sk-container-id-3 div.sk-estimator:hover { background-color: rgba(212, 235, 255, 1) }
#sk-container-id-3 div.sk-parallel-item::after { content: ""; width: 100%; border-bottom: 1px solid rgba(128, 128, 128, 1); flex-grow: 1 }
#sk-container-id-3 div.sk-label:hover label.sk-toggleable__label { background-color: rgba(212, 235, 255, 1) }
#sk-container-id-3 div.sk-serial::before { content: ""; position: absolute; border-left: 1px solid rgba(128, 128, 128, 1); box-sizing: border-box; top: 0; bottom: 0; left: 50%; z-index: 0 }
#sk-container-id-3 div.sk-serial { display: flex; flex-direction: column; align-items: center; background-color: rgba(255, 255, 255, 1); padding-right: 0.2em; padding-left: 0.2em; position: relative }
#sk-container-id-3 div.sk-item { position: relative; z-index: 1 }
#sk-container-id-3 div.sk-parallel { display: flex; align-items: stretch; justify-content: center; background-color: rgba(255, 255, 255, 1); position: relative }
#sk-container-id-3 div.sk-item::before, #sk-container-id-3 div.sk-parallel-item::before { content: ""; position: absolute; border-left: 1px solid rgba(128, 128, 128, 1); box-sizing: border-box; top: 0; bottom: 0; left: 50%; z-index: -1 }
#sk-container-id-3 div.sk-parallel-item { display: flex; flex-direction: column; z-index: 1; position: relative; background-color: rgba(255, 255, 255, 1) }
#sk-container-id-3 div.sk-parallel-item:first-child::after { align-self: flex-end; width: 50% }
#sk-container-id-3 div.sk-parallel-item:last-child::after { align-self: flex-start; width: 50% }
#sk-container-id-3 div.sk-parallel-item:only-child::after { width: 0 }
#sk-container-id-3 div.sk-dashed-wrapped { border: 1px dashed rgba(128, 128, 128, 1); margin: 0 0.4em 0.5em; box-sizing: border-box; padding-bottom: 0.4em; background-color: rgba(255, 255, 255, 1) }
#sk-container-id-3 div.sk-label label { font-family: monospace; font-weight: bold; display: inline-block; line-height: 1.2em }
#sk-container-id-3 div.sk-label-container { text-align: center }
#sk-container-id-3 div.sk-container { display: inline-block !important; position: relative }
#sk-container-id-3 div.sk-text-repr-fallback { display: none }

Ridge()

In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

Ridge
Ridge()
# 查看岭回归的回归系数
rd.coef_
array([[-0.11307323,  0.12670886,  0.0472335 ,  0.03097279, -0.27277927,
0.26649452, 0.02738887, -0.34543899, 0.30352311, -0.23553989,
-0.26624461, 0.11041044, -0.4112231 ]])
# 岭回归预测测试集的目标值,std_y.inverse_transform:返回标准化之前的值(反标准化)
y_rd_predict = std_y.inverse_transform(rd.predict(x_test))
y_rd_predict
array([[16.81586993],
[25.62225283],
[24.13239652],
[23.60178301],
[33.17482664],
[17.47603707],
[25.12448624],
[14.3927178 ],
[23.82242142],
[33.83569284],
[30.25910195],
[13.28992719],
[28.54601232],
[34.54914571],
[27.36491618],
[24.87707782],
[22.00096365],
[14.31750595],
[15.26655896],
[18.95164011],
[14.52104908],
[37.38819398],
[28.82792081],
[23.3211182 ],
[30.6343198 ],
[20.80233876],
[21.31839148],
[13.79005679],
[24.3590396 ],
[26.87702832],
[19.35529157],
[32.16020072],
[19.52355909],
[24.26581358],
[31.63175652],
[30.17323569],
[32.66670796],
[25.47912641],
[24.36217689],
[24.91701584],
[39.47302165],
[18.22458912],
[30.75058024],
[32.14915944],
[43.35075081],
[25.58142763],
[24.22487493],
[22.23864659],
[16.45656221],
[27.14231857],
[ 4.52270441],
[18.23427535],
[17.87417222],
[14.1986027 ],
[13.62643288],
[34.69768313],
[ 8.34275415],
[23.6132958 ],
[ 6.38923846],
[21.27558839],
[15.66185343],
[29.25676316],
[29.39607496],
[20.06328838],
[14.96702673],
[20.93444425],
[28.53639958],
[23.76724172],
[23.49637722],
[11.0745397 ],
[19.48381901],
[15.51875938],
[18.65960692],
[24.24100427],
[15.64918598],
[14.14894164],
[22.94337728],
[24.09499988],
[21.05268108],
[28.55429725],
[ 7.51316118],
[22.62833775],
[ 3.43124359],
[15.98036192],
[25.70480807],
[22.57033657],
[32.66624286],
[17.87124766],
[24.43818932],
[35.27111772],
[26.94613641],
[17.56269425],
[28.14078364],
[21.18918514],
[24.78403264],
[-4.78164143],
[21.36553975],
[21.94334785],
[16.31804996],
[35.31337498],
[40.90768652],
[23.60641046],
[19.94431495],
[34.4813584 ],
[21.35327276],
[20.51324011],
[23.90175952],
[28.77241981],
[40.73752328],
[29.39270623],
[21.38182702],
[22.15806225],
[31.07297608],
[17.17452852],
[38.05954909],
[18.16913598],
[25.97549364],
[13.78567603],
[12.51045123],
[26.99932827],
[18.59193795],
[11.15468796],
[19.52228306],
[23.60713735],
[18.8861402 ],
[19.4947593 ],
[13.61341828]])
# 岭回归预测的均方差(损失值)
loss_rd = mean_squared_error(std_y.inverse_transform(y_test), y_rd_predict)
loss_rd
27.836735080339313

机器学习08DAY的更多相关文章

  1. .NET平台开源项目速览(13)机器学习组件Accord.NET框架功能介绍

    Accord.NET Framework是在AForge.NET项目的基础上封装和进一步开发而来.因为AForge.NET更注重与一些底层和广度,而Accord.NET Framework更注重与机器 ...

  2. 【Machine Learning】机器学习及其基础概念简介

    机器学习及其基础概念简介 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...

  3. 借助亚马逊S3和RapidMiner将机器学习应用到文本挖掘

    本挖掘典型地运用了机器学习技术,例如聚类,分类,关联规则,和预测建模.这些技术揭示潜在内容中的意义和关系.文本发掘应用于诸如竞争情报,生命科学,客户呼声,媒体和出版,法律和税收,法律实施,情感分析和趋 ...

  4. Android开发学习之路-机器学习库(图像识别)、百度翻译

    对于机器学习也不是了解的很深入,今天无意中在GitHub看到一个star的比较多的库,就用着试一试,效果也还行.比是可能比不上TensorFlow的,但是在Android上用起来比较简单,毕竟Tens ...

  5. 【NLP】基于机器学习角度谈谈CRF(三)

    基于机器学习角度谈谈CRF 作者:白宁超 2016年8月3日08:39:14 [摘要]:条件随机场用于序列标注,数据分割等自然语言处理中,表现出很好的效果.在中文分词.中文人名识别和歧义消解等任务中都 ...

  6. 机器学习实战笔记(Python实现)-08-线性回归

    --------------------------------------------------------------------------------------- 本系列文章为<机器 ...

  7. 机器学习实战笔记(Python实现)-06-AdaBoost

    --------------------------------------------------------------------------------------- 本系列文章为<机器 ...

  8. 机器学习实战笔记(Python实现)-05-支持向量机(SVM)

    --------------------------------------------------------------------------------------- 本系列文章为<机器 ...

  9. 机器学习实战笔记(Python实现)-04-Logistic回归

    --------------------------------------------------------------------------------------- 本系列文章为<机器 ...

  10. 机器学习实战笔记(Python实现)-03-朴素贝叶斯

    --------------------------------------------------------------------------------------- 本系列文章为<机器 ...

随机推荐

  1. 【jmeter】请求域名解析失败,添加本地代理

    jmeter HTTP请求URL中使用域名 http://xxx.xxx.xxx,异常:java.net.UnkownHostException 原因:请求域名没有被解析成功,该http请求没有通过本 ...

  2. 不可错过的JS代码优化技巧(持续更新)

    1. 带有多个条件的 if 语句 把多个值放在一个数组中,然后调用数组的 includes 方法. //longhand if (x === 'abc' || x === 'def' || x === ...

  3. HashMap长度为什么是2的幂

    虽然hash值很多,范围很大,但是内存存不了那么大的数组,所以取hash的散列值的时候,需要用hash值,除以数组长度取余数.又由于取余数(%)的性能不如与运算(&),所以想用与运算来代替取余 ...

  4. 井字棋判断输赢C

    #include <stdio.h> int main(){ char a[3][3]; for (int i = 0; i < 3; ++i) { for (int j = 0; ...

  5. Oracle学习-----基本SQL select语句

    一.基本select语句 SELECT 标识  选择那些列 FROM     标识从哪个表中选择 select * 标识 全部选择 select department_id, location_id ...

  6. 12.20linux学习第十九天

    今天老刘开始讲第17章 使用iSCSI服务部署网络存储.第18章 使用MariaDB数据库管理系统和第19章 使用PXE+Kickstart无人值守安装服务,内容有点多. 7.1 iSCSI技术介绍 ...

  7. 《JavaScript高级程序设计》Chapter03 JavaScript语言基础

    目录 Syntax Variable var let const Data Type Undefined Null Boolean Number String Symbol Object Operat ...

  8. django_模板层的过滤器和继承

    **************************************************************************************************** ...

  9. MacOS 使用UnblockNeteaseMusic解锁网易云灰色歌曲(主要是想听杰伦)

    最近想听杰伦的音乐 但是网易云木有版权 于是在github上找到了UnblockNeteaseMusic这个项目 不多废话 直接上教程! 第一步 找到该项目的地址 并使用git克隆到本地: https ...

  10. shell命令查找文件

    1.find命令的参数下面是find命令一些常用参数的例子,有用到的时候查查就行了,像上面前几个贴子,都用到了其中的的一些参数,也可以用man或查看论坛里其它贴子有find命令手册使用name选项文件 ...