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

长沙做暑假实践活动网站网站策划的内容包含了什么?

长沙做暑假实践活动网站,网站策划的内容包含了什么?,如何用群晖做自己的网站,外贸网站优化谷歌关键词排名外包知识回顾: 1. 转化器和估计器的概念 2. 管道工程 3. ColumnTransformer和Pipeline类 作业: 整理下全部逻辑的先后顺序,看看能不能制作出适合所有机器学习的通用pipeline 一、导入数据库 import pandas as pd import numpy as np import matplo…

知识回顾:

1.  转化器和估计器的概念

2.  管道工程

3.  ColumnTransformer和Pipeline类

作业:

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

一、导入数据库

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import time # 导入 time 库
import warnings
warnings.filterwarnings("ignore")
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

 二、导入pipeline

from sklearn.pipeline import Pipeline 
from sklearn.compose import ColumnTransformer 
from sklearn.preprocessing import OrdinalEncoder, OneHotEncoder, StandardScaler 
from sklearn.impute import SimpleImputer 
from sklearn.ensemble import RandomForestClassifier 
from sklearn.metrics import classification_report, confusion_matrix 
data = pd.read_csv('heart.csv')
print("原始数据加载完成,形状为:", data.shape)
y = data['target'] # 标签
X = data.drop(['target'], axis=1) 
print("\n特征和标签分离完成。")
print("特征 X 的形状:", X.shape)
print("标签 y 的形状:", y.shape)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) 
print("\n数据集划分完成 (预处理之前)。")
print("X_train 形状:", X_train.shape)
print("X_test 形状:", X_test.shape)
print("y_train 形状:", y_train.shape)
print("y_test 形状:", y_test.shape)
object_cols = X.select_dtypes(include=['object']).columns.tolist()
numeric_cols = X.select_dtypes(exclude=['object']).columns.tolist()
ordinal_features = ['cp',  'restecg',  'slope', 'ca', 'thal']ordinal_categories = [[0, 1, 2, 3], # Home Ownership 的顺序 (对应1, 2, 3, 4)[0, 1, 2], # Years in current job 的顺序 (对应1-11)[0, 1, 2], # Term 的顺序 (对应0, 1)[0, 1, 2, 3, 4],[0, 1, 2, 3],
]

三、构建有序特征pipeline

ordinal_transformer = Pipeline(steps=[('imputer', SimpleImputer(strategy='most_frequent')), ('encoder', OrdinalEncoder(categories=ordinal_categories, handle_unknown='use_encoded_value', unknown_value=-1)) 
])
print("有序特征处理 Pipeline 定义完成。")
nominal_features = ['sex','fbs','exang']

四、构建标称特征pipeline

nominal_transformer = Pipeline(steps=[('imputer', SimpleImputer(strategy='most_frequent')), ('onehot', OneHotEncoder(handle_unknown='ignore', sparse_output=False)) 
])
print("标称特征处理 Pipeline 定义完成。")
continuous_features = [f for f in X.columns if f not in ordinal_features + nominal_features]
continuous_transformer = Pipeline(steps=[('imputer', SimpleImputer(strategy='most_frequent')),('scaler', StandardScaler()) 
print("连续特征处理 Pipeline 定义完成。")

五、串联预处理器和模型

preprocessor = ColumnTransformer(transformers=[('ordinal', ordinal_transformer, ordinal_features), ('nominal', nominal_transformer, nominal_features), ('continuous', continuous_transformer, continuous_features) ],remainder='passthrough' # 如何处理没有在上面列表中指定的列。
)
print("\nColumnTransformer (预处理器) 定义完成。")
pipeline = Pipeline(steps=[('preprocessor', preprocessor), ('classifier', RandomForestClassifier(random_state=42)) 
])
print("\n完整的 Pipeline 定义完成。")
print("\n--- 1. 默认参数随机森林 (训练集 -> 测试集) ---") 
start_time = time.time() 
pipeline.fit(X_train, y_train)
pipeline_pred = pipeline.predict(X_test)
end_time = time.time() 
print(f"训练与预测耗时: {end_time - start_time:.4f} 秒") 
print("\n默认随机森林 在测试集上的分类报告:") 
print(classification_report(y_test, pipeline_pred))
print("默认随机森林 在测试集上的混淆矩阵:") 
print(confusion_matrix(y_test, pipeline_pred))

http://www.dtcms.com/wzjs/550666.html

相关文章:

  • 网站开发人员名片网站建设那个好
  • 网站续费一般多少钱网站建站上海
  • 武夷山网站建设wzjseoseo网络推广软件
  • 专注番禺网站优化电子商务网站建设所需要的经费
  • 网站设计基本流程第一步中山大学精品课程网站
  • 全国推广优化网站个人网页设计作业
  • jquery 网站后台模板 仿做网站有什么用
  • 福永三合一网站设计设计房屋立体图的软件
  • 哈尔滨建设部网站品牌规划
  • 成都网站seo服务网站开发工程师职业定位
  • 手机点了钓鱼网站怎么办网站开发人员工具
  • 网站如何做tag如何在网上卖自己的产品
  • 企业网站带新闻发布功能的建站阿里巴巴官网登录
  • 官方建网站有哪些步骤深圳网站seo
  • 做设计用哪个素材网站学生个人网页制作免费
  • iis网站伪静态郑州 网站报价
  • 深圳外贸网站建设制作我的世界怎么自己做皮肤并上传网站
  • 模块建站工具包就业的培训机构
  • 创新的广州做网站规划建立一个网站 项目
  • 企业门户网站建设教程织梦后台点击网站主页
  • 宝安品牌网站制作小程序定制开发话术
  • 腾讯云手动搭建wordpress个人站点石河子农八师建设兵团社保网站
  • 客户网站建设确认书织梦淘宝客网站
  • 网站没收录的几大原因广州网络广告推广公司
  • 大连做公司网站哪家好外贸营销员
  • 做网站有什么比较好看的动效开原网站开发
  • 唐山做网站哪家好网上比较好的装修设计平台
  • 织梦高端html5网站建设工作室网络公司网站模板景观设计公司排行榜
  • 为企业进行网站建设方案深圳市建设工程交易服
  • 兰州西固区网站建设平台网站文件大小