一、回归算法之岭回归
具有L2正则化的线性最小二乘法。岭回归是一种专用于共线性数据分析的有偏估计回归方法,实质上是一种改良的最小二乘估计法,通过放弃最小二乘法的无偏性,以损失部分信息、降低精度为代价获得回归系数更为符合实际、更可靠的回归方法,对病态数据的拟合要强于最小二乘法。当数据集中存在共线性的时候,岭回归就会有用。
sklearn.linear_model.Ridge
class sklearn.linear_model.Ridge(alpha=1.0, fit_intercept=True, normalize=False, copy_X=True, max_iter=None, tol=0.001, solver='auto', random_state=None)**""":param alpha:float类型,正规化的程度"""
from sklearn.linear_model import Ridge
clf = Ridge(alpha=1.0)
clf.fit([[0, 0], [0, 0], [1, 1]], [0, .1, 1]))
方法
score(X, y, sample_weight=None)
clf.score()
属性
coef_
clf.coef_
array([ 0.34545455, 0.34545455])
intercept_
clf.intercept_
0.13636...
二、岭回归案例分析
def linearmodel():"""线性回归对波士顿数据集处理:return: None"""# 1、加载数据集ld = load_boston()x_train,x_test,y_train,y_test = train_test_split(ld.data,ld.target,test_size=0.25)# 2、标准化处理# 特征值处理std_x = StandardScaler()x_train = std_x.fit_transform(x_train)x_test = std_x.transform(x_test)# 目标值进行处理std_y = StandardScaler()y_train = std_y.fit_transform(y_train)y_test = std_y.transform(y_test)# 3、估计器流程# LinearRegressionlr = LinearRegression()lr.fit(x_train,y_train)# print(lr.coef_)y_lr_predict = lr.predict(x_test)y_lr_predict = std_y.inverse_transform(y_lr_predict)print("Lr预测值:",y_lr_predict)# SGDRegressorsgd = SGDRegressor()sgd.fit(x_train,y_train)# print(sgd.coef_)y_sgd_predict = sgd.predict(x_test)y_sgd_predict = std_y.inverse_transform(y_sgd_predict)print("SGD预测值:",y_sgd_predict)# 带有正则化的岭回归rd = Ridge(alpha=0.01)rd.fit(x_train,y_train)y_rd_predict = rd.predict(x_test)y_rd_predict = std_y.inverse_transform(y_rd_predict)print(rd.coef_)# 两种模型评估结果print("lr的均方误差为:",mean_squared_error(std_y.inverse_transform(y_test),y_lr_predict))print("SGD的均方误差为:",mean_squared_error(std_y.inverse_transform(y_test),y_sgd_predict))print("Ridge的均方误差为:",mean_squared_error(std_y.inverse_transform(y_test),y_rd_predict))return None