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

day 32

官方文档阅读

绘制pdpbox库中的InteractTargetPlot实例

import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier# 加载鸢尾花数据集
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['target'] = iris.target  # 添加目标列(0-2类:山鸢尾、杂色鸢尾、维吉尼亚鸢尾)# 特征与目标变量
features = iris.feature_names  # 4个特征:花萼长度、花萼宽度、花瓣长度、花瓣宽度
target = 'target'  # 目标列名# 划分训练集与测试集
X_train, X_test, y_train, y_test = train_test_split(df[features], df[target], test_size=0.2, random_state=42
)# 训练模型
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

 实例化要传入原始数据,包含多个特征的列表,这些特征的名称,标签,分桶方式等

import  pdpbox
from pdpbox.info_plots import InteractTargetPlot  # 导入TargetPlot类
# 选择待分析的特征(如:petal length (cm))
feature = ['petal length (cm)','sepal width (cm)']
feature_name = feature  # 特征显示名称# 初始化TargetPlot对象(移除plot_type参数)
interact_target_plot = InteractTargetPlot(df=df,  # 原始数据(需包含特征和目标列)features=feature,  # 目标特征列feature_names=feature_name,  # 特征名称(用于绘图标签)# target='target',  # 多分类目标索引(鸢尾花3个类别)target='target',  # 多分类目标索引(鸢尾花3个类别)grid_types='percentile',  # 分桶方式:百分位num_grid_points=10  # 划分为10个桶
)interact_target_plot.plot()[0]

@浙大疏锦行

相关文章:

  • 【笔记】排查并解决Error in LLM call after 3 attempts: (status code: 502)
  • el-radio-group 与 el-dropdown 一起使用时的注意事项
  • VPX3U规格主板的架构实现与性能评估:飞腾D3000+景嘉微JH930平台
  • 网络:如何通过已知的电脑的机器名(计算机名),获取ip地址
  • Serverless爬虫架构揭秘:动态IP、冷启动与成本优化
  • PHP:经典编程语言在当代Web开发中的新活力
  • 从零基础到最佳实践:Vue.js 系列(5/10):《状态管理》
  • Java面试实录:从JVM调优到Spring Cloud实践
  • 操作系统期末复习(一)
  • 在线视频下载利器,支持100多平台下载
  • Python 字典的用法和技巧
  • 古诗生成器
  • FastAPI 支持文件下载和上传
  • 数据结构篇--优先级队列排序--实验报告
  • 城市停车场光伏-储能-充电系统耦合机制与效益分析
  • Ubuntu20.04部署KVM
  • Ubuntu22.04 系统安装Docker教程
  • 如何使用两块硬盘作为 Ubuntu24 的系统盘,实现坏掉一块不影响系统运行。
  • 域名与DNS详解
  • ROS 配置环境中的变量讲解