一 sklean中模型详解
1.1 Ride regression
1.2 Lasso regression
1.3 ElasticNet
二 算法实战
2.1 导入包
python
复制代码
import numpy as np
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.linear_model import Ridge, Lasso, ElasticNet
from sklearn.metrics import mean_squared_error, r2_score
2.2 加载数据集
python
复制代码
# # 加载糖尿病数据集
X, y = datasets.load_diabetes(return_X_y=True)
2.3 划分训练集和测试集
python
复制代码
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
2.4 定义模型
python
复制代码
# 定义岭回归模型
ridge = Ridge()
# 定义LASSO回归模型
lasso = Lasso()
# 定义弹性网络模型
elastic_net = ElasticNet()
2.5 设置网格参数
python
复制代码
# 设置参数网格
param_grid_ridge = {'alpha': np.logspace(-4, 4, 50)}
param_grid_lasso = {'alpha': np.logspace(-4, 4, 50)}
param_grid_elastic_net = {'alpha': np.logspace(-4, 4, 50), 'l1_ratio': np.linspace(0, 1, 10)}
2.6 创建GridSearchCV对象
python
复制代码
# 创建GridSearchCV对象
grid_ridge = GridSearchCV(ridge, param_grid_ridge, cv=5, scoring='neg_mean_squared_error')
grid_lasso = GridSearchCV(lasso, param_grid_lasso, cv=5, scoring='neg_mean_squared_error')
grid_elastic_net = GridSearchCV(elastic_net, param_grid_elastic_net, cv=5, scoring='neg_mean_squared_error')
2.7 模型训练
python
复制代码
# 训练模型
grid_ridge.fit(X_train, y_train)
grid_lasso.fit(X_train, y_train)
grid_elastic_net.fit(X_train, y_train)
2.8 获取最优参数
python
复制代码
# 获取最优参数
best_params_ridge = grid_ridge.best_params_
best_params_lasso = grid_lasso.best_params_
best_params_elastic_net = grid_elastic_net.best_params_
# 输出最优参数
print("Best parameters for Ridge:", best_params_ridge)
print("Best parameters for LASSO:", best_params_lasso)
print("Best parameters for Elastic Net:", best_params_elastic_net)
2.9 模型预测
python
复制代码
# 使用最佳模型进行预测
best_model_ridge = grid_ridge.best_estimator_
best_model_lasso = grid_lasso.best_estimator_
best_model_elastic_net = grid_elastic_net.best_estimator_
y_pred_ridge = best_model_ridge.predict(X_test)
y_pred_lasso = best_model_lasso.predict(X_test)
y_pred_elastic_net = best_model_elastic_net.predict(X_test)
2.10 模型评估
python
复制代码
# 评估模型
mse_ridge = mean_squared_error(y_test, y_pred_ridge)
r2_ridge = r2_score(y_test, y_pred_ridge)
mse_lasso = mean_squared_error(y_test, y_pred_lasso)
r2_lasso = r2_score(y_test, y_pred_lasso)
mse_elastic_net = mean_squared_error(y_test, y_pred_elastic_net)
r2_elastic_net = r2_score(y_test, y_pred_elastic_net)
# 输出结果
print("Ridge Regression:")
print(f"Mean Squared Error: {mse_ridge:.2f}")
print(f"R^2 Score: {r2_ridge:.2f}")
print("\nLASSO Regression:")
print(f"Mean Squared Error: {mse_lasso:.2f}")
print(f"R^2 Score: {r2_lasso:.2f}")
print("\nElastic Net Regression:")
print(f"Mean Squared Error: {mse_elastic_net:.2f}")
print(f"R^2 Score: {r2_elastic_net:.2f}")