Python打卡训练营day32

import pandas as pd
import  pdpbox
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from pdpbox.info_plots import PredictPlot  # 导入PredictPlot类
# 加载鸢尾花数据集
iris = load_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)
# 选择待分析的特征(如:petal length (cm))
feature = 'petal length (cm)'
feature_name = feature  # 特征显示名称
predict_plot = PredictPlot(
    df=df,
    feature=feature,
    feature_name=feature_name,
    model=model,
    grid_type='percentile',
    model_features=features,
)
fig, axes, summary_df = predict_plot.plot(
    which_classes=None,  # 绘制所有类别(0,1,2)
    show_percentile=True,  # 显示百分位线
    engine='plotly',
    template='plotly_white'
)
fig.update_layout(
    width=1200,  # 宽度800像素
    height=1600,  # 高度500像素
    title=dict(text=f'Target Plot: {feature_name}', x=0.5)  # 居中标题
)
 
fig.show()

@浙大疏锦行 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值