Counterfactual Data Structures¶
- class confetti.structs.counterfactual_structs.Counterfactual(counterfactual: ndarray | DataFrame, label: str | int | float)¶
Bases:
objectContainer for a single counterfactual instance and its predicted label.
Note
This class is a lightweight structure used internally by
CounterfactualSet.
- class confetti.structs.counterfactual_structs.CounterfactualResults(counterfactual_sets: List[CounterfactualSet] | None = None)¶
Bases:
objectContainer for counterfactual results across multiple instances.
- Parameters:
counterfactual_sets (list of CounterfactualSet or None, optional) – A list of
CounterfactualSetobjects, each corresponding to one explained instance. IfNone, the container is initialized empty.
Note
This class acts as a higher-level aggregator over multiple
CounterfactualSetobjects. It provides convenience methods for exporting structured results for all instances at once.- property counterfactual_sets: List[CounterfactualSet]¶
Return the list of stored counterfactual sets.
- Returns:
The stored counterfactual sets.
- Return type:
- class confetti.structs.counterfactual_structs.CounterfactualSet(original_instance: ndarray | DataFrame, original_label: str | int | float | int64 | float64, nearest_unlike_neighbour: ndarray | DataFrame, best_solution: None | Counterfactual, all_counterfactuals: List[Counterfactual], feature_importance: ndarray | None = None)¶
Bases:
objectContainer for all counterfactual explanations generated for one instance.
- Parameters:
original_instance (np.ndarray or pd.DataFrame) – The original instance for which counterfactuals were generated.
original_label (str | int | float | np.int64 | np.float64) – The predicted label of the original instance.
nearest_unlike_neighbour (np.ndarray or pd.DataFrame) – The nearest unlike neighbour (NUN) of the original instance.
best_solution (Counterfactual or None) – The best counterfactual solution identified by the method.
all_counterfactuals (list of Counterfactual) – All counterfactuals generated for this instance.
feature_importance (np.ndarray or None, optional) – Optional 1D array of feature-importance values (e.g., CAM weights) for the nearest unlike neighbour.
Note
This object stores all counterfactual candidates for a single instance, together with metadata such as the NUN and optional importance weights. It provides convenience methods for exporting structured results.
- property all_counterfactuals: List[Counterfactual]¶
Return all generated counterfactuals.
- Returns:
All counterfactual candidates generated for this instance.
- Return type:
- property best: Counterfactual¶
Return the best counterfactual solution.
- Returns:
The best counterfactual, selected according to the method’s optimization criteria.
- Return type:
- property feature_importance: ndarray | None¶
Return the optional feature-importance vector.
- Returns:
A 1D array of feature-importance weights, or
Noneif not provided.- Return type:
np.ndarray or None
- property nearest_unlike_neighbour: ndarray | DataFrame¶
Return the nearest unlike neighbour.
- Returns:
The nearest unlike neighbour instance.
- Return type:
np.ndarray or pd.DataFrame
- property original_instance: ndarray | DataFrame¶
Return the original instance.
- Returns:
The original instance for which counterfactuals were generated.
- Return type:
np.ndarray or pd.DataFrame
- property original_label: str | int | float | int64 | float64¶
Return the predicted label of the original instance.