Partitioners¶
Partitioners create groups/subsets of data for conditional permutation.
ManualPartitioner¶
Use when you have domain knowledge about how series should be grouped.
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¶
Automatically learns subgroups using a decision tree (cs-PFI algorithm).
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
Base Class¶
BasePartitioner
¶
Bases: ABC
Abstract base class for data partitioners.
Partitioners create groups/subsets of data for conditional permutation.
fit(X, feature)
abstractmethod
¶
Fit the partitioner to the data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Input features DataFrame. |
required |
feature
|
str
|
The feature to condition on. |
required |
Returns:
| Type | Description |
|---|---|
BasePartitioner
|
Self for method chaining. |
Source code in src/xeries/core/base.py
fit_get_groups(X, feature)
¶
Fit and return groups in one step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Input features DataFrame. |
required |
feature
|
str
|
The feature to condition on. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[intp]
|
Array of group labels. |
Source code in src/xeries/core/base.py
get_groups(X)
abstractmethod
¶
Get group labels for each sample in X.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Input features DataFrame. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[intp]
|
Array of group labels with same length as X. |