带有L2正则化的线性回归-岭回归
API:
from sklearn.linear_model import Ridge
- Ridge(alpha=1.0, fit_intercept=True,solver="auto", normalize=False)
- 具有l2正则化的线性回归
- alpha:正则化力度,也叫 λ 惩罚项系数
- λ取值:0~1 1~10
- fit_intercept:是否添加偏置
- solver:“auto"--会根据数据自动选择优化方法
- sag:如果数据集、特征都比较大,选择该随机梯度下降优化
- normalize:数据是否进行标准化
- normalize=True: 在fit之前调用preprocessing.StandardScaler标准化数据
- Ridge.coef_:回归权重
- Ridge.intercept_:回归偏置
预测波士顿房价
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import Ridge
def linear3():
"""
岭回归预测波士顿房价
:return:
"""
# 1.获取数据
boston = load_boston()
# 2.划分数据集
x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=22)
# 3.标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
# 4.预估器
estimator = Ridge()
estimator.fit(x_train, y_train)
# 5.得出模型
print("岭回归的权重系数为:\n", estimator.coef_)
print("岭回归的偏置为:\n", estimator.intercept_)
# 6.模型评估
y_predict = estimator.predict(x_test)
print("岭回归的预测房价为:\n", y_predict)
error = mean_squared_error(y_predict, y_test)
print("岭回归的均方误差为:\n", error)
return None
linear3()