- 🌈所属专栏:【机械学习】
- ✨作者主页: Mr.Zwq
- ✔️个人简介:一个正在努力学技术的Python领域创作者,擅长爬虫,逆向,全栈方向,专注基础和实战分享,欢迎咨询!
您的点赞、关注、收藏、评论,是对我最大的激励和支持!!!🤩🥰😍
目录
安装
数据
使用
线性回归
决策树回归
随机森林回归
岭回归
套索回归
支持向量机回归
总结
安装
pip install scikit-learn
数据
X,y即为所需要进行回归处理的数据。
操作:拆分为训练集和测试集
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3, random_state=12)
使用
线性回归
# 线性回归模型
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score# 创建线性回归模型并拟合
model = LinearRegression()
model.fit(X_train, y_train)# 进行预测
y_pred = model.predict(X_test)# 计算模型性能指标
# 利用均方误差(MSE)评价预测结果的合理性,MSE的数值越小越好,即越接近0表示模型的预测与真实值之间的差异较小。
mse = mean_squared_error(y_test, y_pred)
# 利用平均绝对误差(MAE)预测结果的合理性,MAE的数值越小越好,即越接近0表示模型的预测与真实值之间的差异较小。
mae = mean_absolute_error(y_test, y_pred)
# r2分数越接近1代表模型性能越好
r2 = r2_score(y_test, y_pred) print(f'Mean Squared Error: {mse:.4f}')
print(f'Mean Absolute Error: {mae:.4f}')
print(f'R^2 Score: {r2:.4f}')
决策树回归
# 决策树回归模型
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score# 创建决策树回归模型并拟合
model = DecisionTreeRegressor()
model.fit(X_train, y_train)# 进行预测
y_pred = model.predict(X_test)mse = mean_squared_error(y_test, y_pred)
mae = mean_absolute_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred) print(f'Mean Squared Error: {mse:.4f}')
print(f'Mean Absolute Error: {mae:.4f}')
print(f'R^2 Score: {r2:.4f}')
随机森林回归
# 随机森林回归模型
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score# 创建随机森林回归模型并拟合
model = RandomForestRegressor()
model.fit(X_train, y_train)# 进行预测
y_pred = model.predict(X_test)mse = mean_squared_error(y_test, y_pred)
mae = mean_absolute_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred) print(f'Mean Squared Error: {mse:.4f}')
print(f'Mean Absolute Error: {mae:.4f}')
print(f'R^2 Score: {r2:.4f}')
岭回归
# 岭回归模型
from sklearn.linear_model import Ridge
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score# 创建岭回归模型
ridge = Ridge()# 定义alpha值的候选范围
param_grid = {'alpha':[0.1,1.0,10.0]}# 使用交叉验证选择最优的alpha值
ridge_cv = GridSearchCV(ridge,param_grid,cv=5)
ridge_cv.fit(X_train,y_train)# 获取最优的alpha值
best_alpha = ridge_cv.best_params_['alpha']
print("最优alpha值:", best_alpha)# 使用最优的alpha值创建并训练岭回归模型
ridge = Ridge(alpha=best_alpha)
ridge.fit(X_train,y_train)# 进行预测
y_pred = model.predict(X_test)mse = mean_squared_error(y_test, y_pred)
mae = mean_absolute_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred) print(f'Mean Squared Error: {mse:.4f}')
print(f'Mean Absolute Error: {mae:.4f}')
print(f'R^2 Score: {r2:.4f}')
套索回归
# 套索回归模型
from sklearn.linear_model import Lasso
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score# 定义alpha值候选范围
param_grid = {'alpha':[0.1,1.0,10.0]}# 使用交叉验证选择最优的alpha值
ridge_cv = GridSearchCV(ridge,param_grid,cv=5)
ridge_cv.fit(X_train,y_train)# 获取最优的alpha值
best_alpha = ridge_cv.best_params_['alpha']
print("最优alpha值:", best_alpha)# 创建并训练套索回归模型
lasso = Lasso(alpha=best_alpha)
lasso.fit(X_train,y_train)# 在测试集上进行预测
y_pred = lasso.predict(X_test)mse = mean_squared_error(y_test, y_pred)
mae = mean_absolute_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred) print(f'Mean Squared Error: {mse:.4f}')
print(f'Mean Absolute Error: {mae:.4f}')
print(f'R^2 Score: {r2:.4f}')
支持向量机回归
# 支持向量机回归
from sklearn.svm import SVR
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score# 特征标准化
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)# 创建支持向量机回归模型并拟合
model = SVR()
model.fit(X_train_scaled, y_train)# 进行预测
y_pred = model.predict(X_test_scaled)mse = mean_squared_error(y_test, y_pred)
mae = mean_absolute_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred) print(f'Mean Squared Error: {mse:.4f}')
print(f'Mean Absolute Error: {mae:.4f}')
print(f'R^2 Score: {r2:.4f}')
总结
感谢观看,原创不易,如果觉得有帮助,请给文章点个赞吧,让更多的人看到。🌹🌹🌹
👍🏻也欢迎你,关注我。👍🏻
如有疑问,可在评论区留言哦~