Explainer
Explainer
class provides a set of methods for interpreting and understanding the behavior of fitted estimators such as RUGClassifier
, RUXClassifier
, RUXLGBMClassifier
, or RUXXGBClassifier
.
- class ruleopt.Explainer
Initializes the Explainer with a given estimator. The estimator must be fitted and of a type that inherits from _RUGBASE, such as RUGClassifier, RUXClassifier, RUXLGBMClassifier, or RUXXGBClassifier.
- __init__(estimator)
- Parameters:
estimator (ruleopt.estimator instance) – A fitted estimator of a type that inherits from _RUGBASE.
- retrieve_rule_details(feature_names=None, indices=None, info=True)
Retrieves and optionally prints detailed information about the specified rules. If indices are provided, information for those specific rules is returned. Otherwise, information for all rules is returned.
- Parameters:
feature_names (list or None, default=None) – List of feature names for more readable rule descriptions. If None, indices are used.
indices (list or None, default=None) – Indices of the rules to retrieve. If None, retrieves all rules.
info (bool, default=True) – If True, prints the rules’ details in a human-readable format.
- Returns:
A dictionary mapping each rule index to its details, including label, weight, rule description, and statistical distribution. If a rule has no conditions, it sets the majority class.
- Return type:
dict
- find_applicable_rules_for_samples(x, threshold=0.0, feature_names=None, info=True)
Identifies which rules apply to each instance in the provided input data based on a given threshold. Optionally, prints detailed information about each rule that applies to the instances.
- Parameters:
x (array-like of shape (n_samples, n_features)) – The training input samples. Internally, it will be converted to dtype=np.float32.
threshold (float, default=0.0) – Minimum rule weight threshold for considering a rule as covering an instance.
feature_names (list or None, default=None) – List of feature names for more readable rule descriptions. If None, indices are used.
info (bool, default=True) – If True, prints the details of the applicable rules for each instance.
- Returns:
A list of lists, where each inner list contains the indices of rules that cover the corresponding instance in x.
- Return type:
List[List[int]]
- summarize_rule_metrics(info=True)
Calculates and optionally prints the total number of rules and the average rule length within the model.
If instance is not covered by any rule, rule length counts as 0.
- Parameters:
info (bool, default=True) – If True, prints the summary information.
- Returns:
A dictionary containing ‘num_of_rules’ (the total number of rules) and ‘avg_rule_length’ (the average length of the rules).
- Return type:
dict
- evaluate_rule_coverage_metrics(x, info=True)
Calculates metrics including the number of instances not covered by any rule (‘num_of_missed’), the average number of rules per sample (‘avg_num_rules_per_sample’), and the average rule length per sample (‘avg_rule_length_per_sample’). Optionally, prints this information.
If instance is not covered by any rule, rule length counts as 0.
- Parameters:
x (array-like of shape (n_samples, n_features)) – The training input samples. Internally, it will be converted to dtype=np.float32.
info (bool, default=True) – If True, prints the calculated metrics.
- Returns:
A dictionary with calculated metrics: ‘num_of_missed’, ‘avg_num_rules_per_sample’, and ‘avg_rule_length_per_sample’.
- Return type:
dict