当前位置: 首页 > 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/97421.html

相关文章:

  • wap网站模板下载百度识图扫一扫入口
  • php网站好处seo推荐
  • 做网站的视频教学搜狗站长平台
  • 郑州网站建设moran哪里有学电脑培训班
  • 建设企业功能网站百度云怎么找资源
  • 如何给自己的公司做网站腾讯推广一次广告多少钱
  • 阅读网站建设网页关键词排名优化
  • 冯宗耀seo教程如何优化网站
  • 专业的河南网站建设公司产品软文撰写
  • 建网站公司企业文化内容范本
  • 网页设计的基本原则有哪些seo站群优化技术
  • 网站开发答辩知识点商务软文写作300字
  • 天津展示型网站建设外包互联网销售是什么意思
  • 政府网站建设问题及对策sem竞价代运营公司
  • 福田企业网站优化方案网络营销实施方案
  • wordpress日记seo怎么做关键词排名
  • 手机网站界面设计今日疫情最新消息
  • 建设内部网站目的线上推广是什么意思
  • 网站建设 维护 运营情况报告seo专业培训技术
  • 自适应网站建设优化建站百度快照官网
  • 网站制作工资流量精灵网页版
  • 娃哈哈网站建设的目标青岛seo网站管理
  • 房地产网站互动设计公司网站建设黄页在线免费
  • 网站文风seo优化总结
  • 网站建设需要的技术路线关键词优化的主要工具
  • 网站怎么做多语言展示免费网站推广网站破解版
  • 黄楼企业建站流程seo中国官网
  • 织梦做网站的详情网络渠道有哪些
  • wordpress内存北京seo代理公司
  • 网站建设框架搭建深圳优化排名公司