# 独孤九剑第一式-岭回归和Lasso回归

?文章适合于所有的相关人士进行学习?
?各位看官看完了之后不要立刻转身呀?
?期待三连关注小小博主加收藏?
?小小博主回关快 会给你意想不到的惊喜呀?

## ?岭回归

#### ?岭回归的引出

1. 出现自变量个数多余样本量个数，（矩阵中列数多于行数）。举个例子，根据我们学习过的线性代数

此时就不存在逆矩阵。
2. 自变量之间存在严重的多重共线性，自变量之间存在强相关。

#### ?岭回归代码部分

``````import pandas as pd
import numpy as np
from sklearn import model_selection
from sklearn.linear_model import Ridge,RidgeCV#线性回归模型的岭回归
import matplotlib.pyplot as plt
``````

``````diabetes = pd.read_excel(r'diabetes.xlsx', sep = '')
# 构造自变量（剔除患者性别、年龄和因变量）只取第三列到最后一列但是取不到最后一列
predictors = diabetes.columns[2:-1]
# 将数据集拆分为训练集和测试集
X_train, X_test, y_train, y_test = model_selection.train_test_split(diabetes[predictors], diabetes['Y'],
test_size = 0.2, random_state = 1234 )#分为自变量和因变量
``````

``````Lambdas = np.logspace(-5, 2, 200)#-5到2呈现等比数列 生成200个数值
# 岭回归模型的交叉验证
# 设置交叉验证的参数，对于每一个Lambda值，都执行10重交叉验证
ridge_cv = RidgeCV(alphas = Lambdas, normalize=True, scoring='neg_mean_squared_error', cv = 10)#十重交叉验证，数值为Lambda
# 模型拟合   CV指的是 cross v
ridge_cv.fit(X_train, y_train)#对之前的进行拟合
# 返回最佳的lambda值
ridge_best_Lambda = ridge_cv.alpha_
ridge_best_Lambda
``````

``````from sklearn.metrics import mean_squared_error
# 基于最佳的Lambda值建模
ridge = Ridge(alpha = ridge_best_Lambda, normalize=True)
ridge.fit(X_train, y_train)
# 返回岭回归系数
pd.Series(index = ['Intercept'] + X_train.columns.tolist(),data = [ridge.intercept_] + ridge.coef_.tolist())
# 预测
ridge_predict = ridge.predict(X_test)
# 预测效果验证
RMSE = np.sqrt(mean_squared_error(y_test,ridge_predict))
RMSE
``````

## ?Lasso回归模型

max_iter表示迭代1000次则退出本次求解参数，tol表示误差，如果误差和实际值小于0.0001，则退出求解本次参数。两者哦都是对最后求解参数的一种结束条件。

#### ?Lasso代码部分

``````from sklearn.linear_model import Lasso,LassoCV
# LASSO回归模型的交叉验证
lasso_cv = LassoCV(alphas = Lambdas, normalize=True, cv = 10, max_iter=10000)
lasso_cv.fit(X_train, y_train)
# 输出最佳的lambda值
lasso_best_alpha = lasso_cv.alpha_
lasso_best_alpha
``````

``````lasso = Lasso(alpha = lasso_best_alpha, normalize=True, max_iter=10000)
lasso.fit(X_train, y_train)
# 返回LASSO回归的系数
pd.Series(index = ['Intercept'] + X_train.columns.tolist(),data = [lasso.intercept_] + lasso.coef_.tolist())

# 预测
lasso_predict = lasso.predict(X_test)
# 预测效果验证
RMSE = np.sqrt(mean_squared_error(y_test,lasso_predict))
RMSE
``````

?文章适合于所有的相关人士进行学习?
?各位看官看完了之后不要立刻转身呀?
?期待三连关注小小博主加收藏?
?小小博主回关快 会给你意想不到的惊喜呀?

THE END