Implementation:Scikit learn Scikit learn PartialDependence
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Model Inspection |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for computing partial dependence of features in fitted models provided by scikit-learn.
Description
The partial dependence module computes partial dependence functions that show the marginal effect of one or two features on the predicted outcome of a machine learning model. It supports two computation methods: a fast recursion method for tree-based models (gradient boosting, random forests, decision trees) that traverses tree structures directly, and a slower but more general brute force method that works with any estimator by averaging predictions over a grid of feature values. The module also handles categorical features.
Usage
Use partial dependence when you need to understand how individual features influence model predictions, for model interpretation and feature importance analysis, or when creating partial dependence plots to explain model behavior to stakeholders.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/inspection/_partial_dependence.py
Signature
def partial_dependence(
estimator,
X,
features,
*,
sample_weight=None,
categorical_features=None,
feature_names=None,
response_method="auto",
percentiles=(0.05, 0.95),
grid_resolution=100,
method="auto",
kind="average",
custom_values=None,
)
Import
from sklearn.inspection import partial_dependence
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| estimator | BaseEstimator | Yes | A fitted estimator (must support predict, predict_proba, or decision_function) |
| X | array-like of shape (n_samples, n_features) | Yes | Input data from which feature grid values are derived |
| features | list of int or list of str | Yes | Feature indices or names for which to compute partial dependence |
| sample_weight | array-like of shape (n_samples,) | No | Sample weights for averaging |
| categorical_features | array-like of bool or int | No | Indicates which features are categorical |
| feature_names | array-like of str | No | Names of features; used when features is a list of strings |
| response_method | str | No | Prediction method: auto, predict_proba, decision_function, predict |
| percentiles | tuple of float | No | Lower and upper percentile for grid range (default (0.05, 0.95)) |
| grid_resolution | int | No | Number of equally spaced points on the grid (default 100) |
| method | str | No | Computation method: auto, recursion, or brute |
| kind | str | No | Kind of partial dependence: average, individual, or both |
Outputs
| Name | Type | Description |
|---|---|---|
| pd_results | Bunch | Contains average (averaged PD), individual (per-sample ICE values), grid_values (feature values used), and feature_names |
Usage Examples
Basic Usage
from sklearn.datasets import make_friedman1
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.inspection import partial_dependence
X, y = make_friedman1(random_state=0)
est = GradientBoostingRegressor(n_estimators=100, random_state=0).fit(X, y)
# Compute partial dependence for feature 0
pd_result = partial_dependence(est, X, features=[0], kind="average")
print("Grid values:", pd_result["grid_values"][0][:5])
print("PD values:", pd_result["average"][0][:5])
# Compute individual conditional expectations (ICE)
pd_result_ice = partial_dependence(est, X, features=[0], kind="individual")
print("ICE shape:", pd_result_ice["individual"][0].shape)