API Reference¶
This page provides an overview of the xeries API.
Main Classes¶
ConditionalPermutationImportance¶
ConditionalPermutationImportance(model, metric='mse', strategy='auto', partitioner=None, n_repeats=5, n_jobs=-1, random_state=None)
¶
Bases: MetricBasedExplainer
Conditional Permutation Feature Importance calculator.
This implements conditional permutation importance where feature values are only shuffled within defined subgroups, preserving the correlation structure between features.
Supports two strategies: - 'auto': Uses tree-based cs-PFI to automatically learn subgroups - 'manual': Uses pre-defined groups provided by the user
Example
explainer = ConditionalPermutationImportance(model, metric='mse') result = explainer.explain(X, y, features=['lag_1', 'lag_2']) print(result.to_dataframe())
Initialize the conditional permutation importance calculator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
ModelProtocol
|
A model with a predict method. |
required |
metric
|
MetricFunction | str
|
Scoring metric ('mse', 'mae', 'rmse', 'r2') or callable. |
'mse'
|
strategy
|
str
|
Grouping strategy ('auto' for tree-based, 'manual' for user-defined). |
'auto'
|
partitioner
|
BasePartitioner | None
|
Custom partitioner instance. If None, uses TreePartitioner for 'auto'. |
None
|
n_repeats
|
int
|
Number of times to repeat permutation for each feature. |
5
|
n_jobs
|
int
|
Number of parallel jobs (-1 for all cores). |
-1
|
random_state
|
int | None
|
Random seed for reproducibility. |
None
|
Source code in src/xeries/importance/permutation.py
explain(X, y, features=None, groups=None, *args, **kwargs)
¶
Compute conditional permutation importance for features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Input features DataFrame. |
required |
y
|
ArrayLike
|
Target values. |
required |
features
|
list[str] | None
|
List of features to compute importance for. If None, uses all columns in X. |
None
|
groups
|
GroupLabels | None
|
Pre-defined group labels for 'manual' strategy. Required when strategy='manual' and no partitioner is provided. |
None
|
Returns:
| Type | Description |
|---|---|
FeatureImportanceResult
|
FeatureImportanceResult containing importance scores. |
Source code in src/xeries/importance/permutation.py
explain_per_series(X, y, series_col, features=None, min_samples=10)
¶
Compute conditional permutation importance separately for each series.
This method filters the data by each unique series ID and computes feature importance independently for each series. Permutation is performed only within each individual series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Input features DataFrame. |
required |
y
|
ArrayLike
|
Target values. |
required |
series_col
|
str
|
Name of the column or MultiIndex level containing series IDs. |
required |
features
|
list[str] | None
|
List of features to compute importance for. If None, uses all columns except series_col. |
None
|
min_samples
|
int
|
Minimum number of samples required per series. Series with fewer samples are skipped. |
10
|
Returns:
| Type | Description |
|---|---|
dict[Any, FeatureImportanceResult]
|
Dictionary mapping series IDs to FeatureImportanceResult objects. |
Example
explainer = ConditionalPermutationImportance(model, metric='mse') results = explainer.explain_per_series(X, y, series_col='level') for series_id, result in results.items(): ... print(f"{series_id}: {result.to_dataframe()}")
Source code in src/xeries/importance/permutation.py
ManualPartitioner¶
ManualPartitioner(mapping, series_col='level')
¶
Bases: BasePartitioner
Partitioner using a user-defined mapping dictionary.
This partitioner assigns samples to groups based on a predefined mapping from series identifiers (or other categorical values) to group labels. Useful when domain knowledge suggests natural groupings.
Example
mapping = {'MT_001': 'group_A', 'MT_002': 'group_B', 'MT_003': 'group_A'} partitioner = ManualPartitioner(mapping, series_col='level') groups = partitioner.fit_get_groups(X, feature='lag_1')
Initialize the manual partitioner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapping
|
dict[Any, Any]
|
Dictionary mapping series identifiers to group labels. |
required |
series_col
|
str
|
Name of the column or index level containing series IDs. |
'level'
|
Source code in src/xeries/partitioners/manual.py
n_groups
property
¶
Return the number of unique groups.
fit(X, feature)
¶
Fit the partitioner (encodes group labels to integers).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Input features DataFrame. |
required |
feature
|
str
|
The feature to condition on (not used for manual partitioner). |
required |
Returns:
| Type | Description |
|---|---|
ManualPartitioner
|
Self for method chaining. |
Source code in src/xeries/partitioners/manual.py
get_groups(X)
¶
Get group labels for each sample based on the mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Input features DataFrame with series identifiers. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[intp]
|
Array of integer group labels. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If partitioner has not been fitted. |
KeyError
|
If series_col is not found in X. |
Source code in src/xeries/partitioners/manual.py
TreePartitioner¶
TreePartitioner(max_depth=4, min_samples_leaf=0.05, series_col=None, random_state=None)
¶
Bases: BasePartitioner
Partitioner using decision tree leaf nodes for subgroup discovery.
This implements the Conditional Subgroup Permutation Feature Importance (cs-PFI) algorithm. A decision tree is trained to predict the feature of interest using all other features. The leaf nodes of this tree define homogeneous subgroups for conditional permutation.
Example
partitioner = TreePartitioner(max_depth=4, min_samples_leaf=0.05) groups = partitioner.fit_get_groups(X, feature='lag_1')
Initialize the tree partitioner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_depth
|
int | None
|
Maximum depth of the decision tree. |
4
|
min_samples_leaf
|
int | float
|
Minimum samples required in a leaf node. Can be int (absolute) or float (fraction of total samples). |
0.05
|
series_col
|
str | None
|
Column with series identifiers to one-hot encode.
|
None
|
random_state
|
int | None
|
Random seed for reproducibility. |
None
|
Source code in src/xeries/partitioners/tree.py
n_groups
property
¶
Return the number of leaf nodes (groups).
tree
property
¶
Return the fitted decision tree.
fit(X, feature)
¶
Fit the decision tree to predict the feature of interest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Input features DataFrame. |
required |
feature
|
str
|
The feature to condition on (will be predicted by tree). |
required |
Returns:
| Type | Description |
|---|---|
TreePartitioner
|
Self for method chaining. |
Source code in src/xeries/partitioners/tree.py
get_groups(X)
¶
Get leaf node indices as group labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Input features DataFrame. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[intp]
|
Array of leaf node indices (group labels). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If partitioner has not been fitted. |
Source code in src/xeries/partitioners/tree.py
Result Types¶
FeatureImportanceResult¶
FeatureImportanceResult(feature_names, importances, std=None, baseline_score=0.0, permuted_scores=dict(), method='permutation', n_repeats=1)
dataclass
¶
Bases: BaseResult
Container for feature importance results.
Attributes:
| Name | Type | Description |
|---|---|---|
feature_names |
list[str]
|
List of feature names. |
importances |
NDArray[floating[Any]]
|
Array of importance scores for each feature. |
std |
NDArray[floating[Any]] | None
|
Standard deviations of importance scores (from multiple permutations). |
baseline_score |
float
|
The baseline model score before permutation. |
permuted_scores |
dict[str, list[float]]
|
Dictionary mapping feature names to their permuted scores. |
method |
str
|
The method used to compute importance ('permutation', 'shap', etc.). |
n_repeats |
int
|
Number of permutation repeats used. |
to_dataframe()
¶
Convert results to a pandas DataFrame.
Source code in src/xeries/core/types.py
Visualization¶
plot_importance_bar¶
plot_importance_bar(result, max_features=20, ax=None, figsize=(10, 6), title=None, color='#1f77b4', show_std=True)
¶
Plot feature importance as a horizontal bar chart.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
FeatureImportanceResult
|
FeatureImportanceResult from an explainer. |
required |
max_features
|
int | None
|
Maximum number of features to display (top N). |
20
|
ax
|
Axes | None
|
Matplotlib axes to plot on. If None, creates new figure. |
None
|
figsize
|
tuple[int, int]
|
Figure size (width, height) in inches. |
(10, 6)
|
title
|
str | None
|
Plot title. If None, uses default. |
None
|
color
|
str
|
Bar color. |
'#1f77b4'
|
show_std
|
bool
|
Whether to show error bars for standard deviation. |
True
|
Returns:
| Type | Description |
|---|---|
tuple[Figure, Axes]
|
Tuple of (Figure, Axes). |
Source code in src/xeries/visualization/plots.py
plot_importance_heatmap¶
plot_importance_heatmap(results, features=None, ax=None, figsize=(12, 8), cmap='YlOrRd', annot=True, title=None)
¶
Plot importance comparison across multiple conditions as a heatmap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
dict[str, FeatureImportanceResult]
|
Dictionary mapping condition names to FeatureImportanceResult. |
required |
features
|
list[str] | None
|
List of features to include. If None, uses union of all. |
None
|
ax
|
Axes | None
|
Matplotlib axes to plot on. If None, creates new figure. |
None
|
figsize
|
tuple[int, int]
|
Figure size (width, height) in inches. |
(12, 8)
|
cmap
|
str
|
Colormap name. |
'YlOrRd'
|
annot
|
bool
|
Whether to annotate cells with values. |
True
|
title
|
str | None
|
Plot title. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Figure, Axes]
|
Tuple of (Figure, Axes). |
Source code in src/xeries/visualization/plots.py
Planned APIs¶
The following APIs are planned for future releases and are not part of the current release:
- Conditional SHAP
- SHAP-IQ
- Feature Dropping
- Causal Feature Importance