CET¶
Counterfactual Explanation Trees
CET uses tree structures to generate interpretable counterfactual explanations.
Overview¶
CET builds decision trees that guide the counterfactual generation process.
Usage¶
from counterfactuals.cf_methods.local_methods.cet import CounterfactualExplanationTree
method = CounterfactualExplanationTree(
gen_model=gen_model,
disc_model=classifier,
disc_model_criterion=criterion,
device="cuda"
)
result = method.explain(
X=instance,
y_origin=0,
y_target=1,
X_train=X_train,
y_train=y_train
)
API Reference¶
CounterfactualExplanationTree
¶
CounterfactualExplanationTree(mdl, X, Y=[], max_iteration=1000, max_depth=3, min_samples_leaf=1, remain_redundant_leaf=False, max_candidates=50, tol=1e-06, use_mined_rules=False, minsup=0.5, discretization_bins=5, lime_approximation=False, n_samples=10000, alpha=1.0, feature_names=[], feature_types=[], feature_categories=[], feature_constraints=[], target_name='Output', target_labels=['Good', 'Bad'], device=None, **kwargs)
Bases: BaseCounterfactualMethod, LocalCounterfactualMixin
Source code in counterfactuals/cf_methods/local_methods/cet/cet.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |
explain
¶
Wrapper to produce ExplanationResult for compatibility.
This uses the existing get_counterfactuals method and returns an ExplanationResult dataclass.
Source code in counterfactuals/cf_methods/local_methods/cet/cet.py
get_counterfactuals
¶
Get counterfactual examples for input instances.
Parameters¶
X : array-like of shape (n_samples, n_features) Input instances to generate counterfactuals for return_costs : bool, default=False Whether to return the costs associated with each counterfactual
Returns¶
counterfactuals : array-like of shape (n_samples, n_features) Counterfactual examples costs : array-like of shape (n_samples,), optional Costs associated with each counterfactual if return_costs=True