当前位置: 首页 > wzjs >正文

深圳做微信网站公司哪家好南宁网站建设哪家公

深圳做微信网站公司哪家好,南宁网站建设哪家公,网站如何在百度刷排名,免费网站奖励自己游戏作业: 整理下全部逻辑的先后顺序,看看能不能制作出适合所有机器学习的通用pipeline 数据预处理 → 特征选择 → 降维 → 模型训练 import pandas as pd import numpy as np from sklearn.model_selection import train_test_split, GridSearchCV from sk…

作业:

整理下全部逻辑的先后顺序,看看能不能制作出适合所有机器学习的通用pipeline

数据预处理 → 特征选择 → 降维 → 模型训练

 

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler, OrdinalEncoder, OneHotEncoder
from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.decomposition import PCA
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report
from skopt import BayesSearchCV
from skopt.space import Integer, Categorical
import shap# -------------------- 1. 数据加载与划分 --------------------
# 加载原始数据(假设目标列名为'Credit Default')
data = pd.read_csv('data.csv')
y = data['Credit Default']
X = data.drop('Credit Default', axis=1)# 划分数据集(保持原始状态,避免数据泄露)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42
)# -------------------- 2. 定义特征处理流程 --------------------
# 定义特征类型(根据实际数据调整)
numeric_features = ['Annual Income', 'Current Loan Amount']
ordinal_features = ['Years in current job']
ordinal_categories = [['< 1 year', '1 year', '2 years',..., '10+ years']]
nominal_features = ['Purpose']# 数值特征处理:中位数填充 + 标准化
numeric_transformer = Pipeline(steps=[('imputer', SimpleImputer(strategy='median')),('scaler', StandardScaler())
])# 有序分类处理:众数填充 + 顺序编码
ordinal_transformer = Pipeline(steps=[('imputer', SimpleImputer(strategy='most_frequent')),('encoder', OrdinalEncoder(categories=ordinal_categories, handle_unknown='use_encoded_value', unknown_value=-1))
])# 标称分类处理:众数填充 + 独热编码
nominal_transformer = Pipeline(steps=[('imputer', SimpleImputer(strategy='most_frequent')),('onehot', OneHotEncoder(handle_unknown='ignore', sparse_output=False))
])# 合并预处理步骤
preprocessor = ColumnTransformer(transformers=[('num', numeric_transformer, numeric_features),('ord', ordinal_transformer, ordinal_features),('nom', nominal_transformer, nominal_features)],remainder='drop'  # 丢弃未指定列
)# -------------------- 3. 构建完整Pipeline --------------------
# 包含特征选择、降维和分类器
full_pipeline = Pipeline([('preprocessor', preprocessor),('feature_selector', SelectKBest(score_func=f_classif, k=10)),  # 选择Top10特征('dim_reducer', PCA(n_components=0.95)),  # 保留95%方差('classifier', RandomForestClassifier(random_state=42))
])# -------------------- 4. 网格搜索调参 --------------------
# 定义参数网格(注意格式:步骤名__参数名)
param_grid = {'feature_selector__k': [5, 10, 15],  # 特征选择数量'dim_reducer__n_components': [0.8, 0.9, 0.95],  # PCA保留方差'classifier__n_estimators': [100, 200],  # 随机森林树数量'classifier__max_depth': [None, 5, 10]
}# 创建网格搜索对象
grid_search = GridSearchCV(estimator=full_pipeline,param_grid=param_grid,cv=3,n_jobs=-1,scoring='f1_macro'
)# 执行网格搜索
grid_search.fit(X_train, y_train)# 输出最佳参数
print("最佳参数组合:", grid_search.best_params_)
print("最佳验证分数:", grid_search.best_score_)# -------------------- 5. 贝叶斯优化调参 --------------------
# 定义参数搜索空间(使用skopt的Space类)
bayes_space = {'feature_selector__k': Integer(5, 20),'dim_reducer__n_components': Categorical([0.8, 0.85, 0.9, 0.95]),'classifier__n_estimators': Integer(50, 300),'classifier__max_depth': Integer(3, 15)
}# 创建贝叶斯优化对象
bayes_search = BayesSearchCV(estimator=full_pipeline,search_spaces=bayes_space,n_iter=30,  # 迭代次数cv=3,n_jobs=-1,scoring='f1_macro'
)# 执行贝叶斯优化
bayes_search.fit(X_train, y_train)# 输出最佳参数
print("\n贝叶斯优化最佳参数:", bayes_search.best_params_)
print("贝叶斯优化最佳分数:", bayes_search.best_score_)# -------------------- 6. 模型评估与可视化 --------------------
# 使用最佳模型进行预测
best_model = bayes_search.best_estimator_
y_pred = best_model.predict(X_test)# 输出分类报告
print("\n测试集分类报告:")
print(classification_report(y_test, y_pred))# -------------------- 7. SHAP特征重要性分析 --------------------
# 提取预处理后的特征名称
onehot_columns = best_model.named_steps['preprocessor'].named_transformers_['nom'].named_steps['onehot'].get_feature_names_out(nominal_features)
all_features = numeric_features + ordinal_features + list(onehot_columns)# 创建SHAP解释器
explainer = shap.TreeExplainer(best_model.named_steps['classifier'])
X_processed = best_model.named_steps['preprocessor'].transform(X_train)  # 获取预处理后的数据# 计算SHAP值
shap_values = explainer.shap_values(X_processed)# 绘制特征重要性图
shap.summary_plot(shap_values, X_processed, feature_names=all_features)

@浙大疏锦行


文章转载自:

http://2eFoBnyl.jrqbr.cn
http://ND6PCB5I.jrqbr.cn
http://Q2TmHiMs.jrqbr.cn
http://xI028UP1.jrqbr.cn
http://4CoH0Snp.jrqbr.cn
http://jRRdcMW5.jrqbr.cn
http://No6FtWJf.jrqbr.cn
http://6mZDcgZr.jrqbr.cn
http://19l2H5FM.jrqbr.cn
http://HjOdyUxr.jrqbr.cn
http://kY3FycXA.jrqbr.cn
http://1QRpOGyh.jrqbr.cn
http://f7rwsuZo.jrqbr.cn
http://sWImdlwe.jrqbr.cn
http://1iJjauUH.jrqbr.cn
http://ufJI6r34.jrqbr.cn
http://reu08lr8.jrqbr.cn
http://O1JTWXfJ.jrqbr.cn
http://zyTqbREI.jrqbr.cn
http://30I8WeST.jrqbr.cn
http://uaBTcn56.jrqbr.cn
http://d9TqzS8U.jrqbr.cn
http://tCKfCc6Q.jrqbr.cn
http://WjPNKuNc.jrqbr.cn
http://EKbOarkA.jrqbr.cn
http://6SehGrK9.jrqbr.cn
http://VvRyqy8o.jrqbr.cn
http://UZhnjTTW.jrqbr.cn
http://t5SVWhrH.jrqbr.cn
http://WPAQ5qRC.jrqbr.cn
http://www.dtcms.com/wzjs/733583.html

相关文章:

  • 中国建设工程造价信息网站视频网站超链接怎么做
  • 浙江做网站公司电子商务有限公司是干什么的
  • 男人女人晚上做那事网站建设网站教程视频
  • 公司建设网站的意义百度智能云官网
  • 毕设做购物网站系统的原因策划公司网站建设
  • 厦门好的做网站公司注册城乡规划师考试大纲
  • 网站开发语言哪种好wordpress 文章侧边栏
  • 万户网络做网站如何推广的十种方式
  • 网站着陆页 推荐wordpress管理插件下载
  • 如何破解网站后台密码朋友说做网站什么的怎么赚钱
  • 建网站哪便宜网站抄袭
  • 设计网站实现PDF在线阅读需要怎么做苏州专业做网站比较好的公司
  • 企业网站备案代理商wordpress允许评论
  • 网站添加百度搜索在线培训系统平台
  • html5网站建设中模板高清海报素材网
  • 建站平台一键申请三方支付通道网上服务大厅官网
  • 毕业设计做网站选题手机酒店网站建设
  • 带后台的php网站模板关于做网站的搞笑段子
  • 可以做单的猎头网站专业网站开发哪家好
  • 聊城网站制作需要多少钱唐山公司网站建设 中企动力唐山
  • 关于网站开发的网站360建筑网证书估价
  • 最好的做网站的公司seo站长工具查询
  • 建旅游网站费用明细seminar怎么读
  • 惠州网站建设找惠州邦赤峰做网站的网络公司
  • 网站建设验收方式做网站的都是什么专业毕业的
  • 湖北省城乡与住房建设厅网站app开发流程表
  • 小企业建网站wordpress开启远程发布
  • 石家庄建站系统知名企业名字
  • 自助建手机网站免费wordpress手机显示侧边栏
  • 东莞网站建设网站推广价钱网站两边广告