Day.32
查看官方文档:
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
features = iris.feature_names
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)
选择特征:
feature = 'petal length (cm)'
feature_name = feature
feature = 'petal length (cm)'
feature_name = feature
target_plot = TargetPlot(
df=df,
feature=feature,
feature_name=feature_name,
# target='target',
target='target',
grid_type='percentile',
num_grid_points=10
)
绘图:
fig, axes, summary_df = target_plot.plot(
which_classes=None,
show_percentile=True,
engine='plotly',
template='plotly_white'
)
fig.update_layout(
width=800,
height=500,
title=dict(text=f'Target Plot: {feature_name}', x=0.5)
)@浙大疏锦行
fig.show()