【sklearn】回归模型常规建模流程

模型训练pipeline

基于数十种统计类型特征,构建LR回归模型。代码逻辑包含:样本切分、特征预处理、模型训练、模型评估、特征重要性的可视化。

步骤一:导入所需库

复制代码
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, PolynomialFeatures
from sklearn.metrics import mean_squared_error, r2_score

步骤二:读取数据

复制代码
data = pd.read_csv('data.csv')

步骤三:数据预处理

复制代码
# 去除缺失值
data.dropna(inplace=True)

# 划分自变量和因变量
X = data.iloc[:, :-1]
y = data.iloc[:, -1]

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

# 构建pipeline
pipeline = Pipeline([
    ('scaler', StandardScaler()),
    ('poly', PolynomialFeatures(degree=2, include_bias=False)),
    ('reg', LinearRegression())
])

# 训练模型
pipeline.fit(X_train, y_train)

# 预测结果
y_pred = pipeline.predict(X_test)

步骤四:模型评估

复制代码
# 均方误差
mse = mean_squared_error(y_test, y_pred)

# R2值
r2 = r2_score(y_test, y_pred)

print('MSE: %.3f' % mse)
print('R2 score: %.3f' % r2)

步骤五:特征重要性的可视化

复制代码
# 获取特征重要性
importance = pipeline.named_steps['reg'].coef_

# 将特征重要性与对应特征名对应
feature_names = pipeline.named_steps['poly'].get_feature_names(X.columns)
feature_importance = pd.DataFrame({'Feature': feature_names, 'Importance': importance})
feature_importance = feature_importance.sort_values('Importance', ascending=False)

# 绘制水平条形图
plt.figure(figsize=(10, 8))
plt.barh(feature_importance['Feature'], feature_importance['Importance'])
plt.title('Feature importance')
plt.xlabel('Importance')
plt.ylabel('Feature')
plt.show()
相关推荐
吴佳浩4 分钟前
为什么"骂"大模型,它反而更聪明了?
人工智能·llm
Font Tian7 分钟前
GPT-oss + vLLM + LobalChat
人工智能·gpt·llm
weixin_466817 分钟前
Python编程之面向对象
开发语言·人工智能·python
连线Insight24 分钟前
竞逐AI内容,爱奇艺先出手了
大数据·人工智能
杭州泽沃电子科技有限公司1 小时前
钢铁厂运输设备在线监测:构建智能工厂的安全与效率基石
运维·人工智能·智能监测
董厂长1 小时前
阅读:REACT: SYNERGIZING REASONING AND ACTING INLANGUAGE MODELS(在语言模型中协同推理与行动)
人工智能·语言模型·agent·react
技术闲聊DD1 小时前
深度学习(5)-PyTorch 张量详细介绍
人工智能·pytorch·深度学习
Lucas555555551 小时前
多模态RAG进阶:基于GPT-4V+LangGraph的下一代智能体系统完全指南
人工智能
小白狮ww1 小时前
LiveCC 首个视频解说大模型开源,比赛视频也能轻松拿捏!
人工智能·深度学习·机器学习
hhhdd_20251 小时前
5 款 PDF 翻译工具深度测评:从格式到免费权限全解析
人工智能·机器学习