lore_sa.rule.Expression

class lore_sa.rule.Expression(variable: str, operator: Callable, value)[source]

Logical expression representing a condition in a rule.

An Expression represents a single condition (premise) in a decision rule, consisting of a variable name, a comparison operator, and a threshold value. For example: “age > 30” or “income <= 50000”.

Expressions are used to build Rule objects that explain black box predictions.

variable

Name of the feature/variable in the condition

Type:

str

operator

Comparison operator from the operator module (e.g., operator.gt, operator.lt, operator.eq, operator.ge, operator.le)

Type:

Callable

value

Threshold value for the comparison

Type:

float or int

Example

>>> import operator
>>> from lore_sa.rule import Expression
>>>
>>> # Create an expression: age > 30
>>> expr = Expression('age', operator.gt, 30)
>>> print(expr)  # Output: age > 30
>>>
>>> # Create an expression: income <= 50000
>>> expr2 = Expression('income', operator.le, 50000)
>>> print(expr2)  # Output: income <= 50000
__init__(variable: str, operator: Callable, value)[source]

Initialize a logical expression.

Parameters:
  • variable (str) – Name of the variable/feature that the expression refers to

  • operator (Callable) – Logical comparison operator. Use one from the operator module: operator.gt (>), operator.lt (<), operator.eq (=), operator.ge (>=), operator.le (<=), operator.ne (!=)

  • value (float or int) – Numerical threshold value to compare against

Raises:

ValueError – If an unsupported operator is provided

Methods

__init__(variable, operator, value)

Initialize a logical expression.

operator2string()

Convert the logical operator to its string representation.

to_dict()

Convert the expression to a dictionary representation.

operator2string()[source]

Convert the logical operator to its string representation.

Returns:

String representation of the operator (e.g., ‘>’, ‘<’, ‘=’, ‘>=’, ‘<=’, ‘!=’)

Return type:

str

Raises:

ValueError – If the operator is not one of the recognized comparison operators

Example

>>> import operator
>>> expr = Expression('age', operator.gt, 30)
>>> expr.operator2string()  # Returns '>'
to_dict()[source]

Convert the expression to a dictionary representation.

Returns:

Dictionary with keys ‘att’ (attribute/variable name),

’op’ (operator as string), and ‘thr’ (threshold value)

Return type:

dict

Example

>>> expr = Expression('age', operator.gt, 30)
>>> expr.to_dict()
{'att': 'age', 'op': '>', 'thr': 30}