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' # 目标列名 df # 划分训练集与测试集 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 feature = ['petal length (cm)', 'petal width (cm)'] feature_names = ['petal length (cm)', 'petal width (cm)'] plot = InteractTargetPlot( df = df, features = feature, feature_names = feature_names, target = target ) plot.plot()[0] # plot.plot()[2]@浙大疏锦行