← retour aux snippets

sklearn: partial dependence

Comprendre l'effet d'une feature sur la prédiction.

objectif

Comprendre l’effet d’une feature sur la prédiction.

code minimal

from sklearn.ensemble import RandomForestRegressor
from sklearn.datasets import load_diabetes
from sklearn.inspection import PartialDependenceDisplay
X, y = load_diabetes(return_X_y=True)
rf = RandomForestRegressor(random_state=0).fit(X, y)
disp = PartialDependenceDisplay.from_estimator(rf, X, [0], kind="average")
print(hasattr(disp, "figure_"))

utilisation

from sklearn.inspection import partial_dependence
pd_out = partial_dependence(rf, X, [0])
print("average" in pd_out)

variante(s) utile(s)

from sklearn.inspection import PartialDependenceDisplay
print(callable(getattr(PartialDependenceDisplay, "from_predictions")))

notes

  • Nécessite modèle tabulaire; prudence avec interactions non linéaires.