Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Leverage Rule Quality Criterion #114

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions interest_measures.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,18 @@ and 0 reflects independence)

**Reference:** T. Zhang, “Association Rules,” in Knowledge Discovery and Data Mining. Current Issues and New
Applications, 2000, pp. 245–256. doi: 10.1007/3-540-45571-X_31.

## Leverage

Leverage metric is difference between the frequency of antecedent and the consequent appearing together and the expected
frequency of them appearing separately based on their individual support

```math
leverage(X \implies Y) = support(X \implies Y) - (support(X) \times support(Y))
```

**Range:** `[-1, 1]` (-1 reflects total negative association, 1 reflects perfect positive association
and 0 reflects independence)

**Reference:** Gregory Piatetsky-Shapiro. 1991. Discovery, Analysis, and Presentation of Strong Rules. In
Knowledge Discovery in Databases, Gregory Piatetsky-Shapiro and William J. Frawley (Eds.). AAAI/MIT Press, 229–248.
15 changes: 15 additions & 0 deletions niaarm/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ class Rule:
**Reference:** T. Zhang, “Association Rules,” in Knowledge Discovery and Data Mining. Current Issues and New
Applications, 2000, pp. 245–256. doi: 10.1007/3-540-45571-X_31.

leverage: difference between the frequency of antecedent and the consequent appearing together and the expected
frequency of them appearing separately based on their individual support

:math: `leverage(X \implies Y) = support(X \implies Y) - (support(X) \times support(Y))`

**Range:** :math: `[-1, 1]` (-1 reflects total negative association, 1 reflects perfect positive association
and 0 reflects independence)

**Reference:** Gregory Piatetsky-Shapiro. 1991. Discovery, Analysis, and Presentation of Strong Rules. In
Knowledge Discovery in Databases, Gregory Piatetsky-Shapiro and William J. Frawley (Eds.). AAAI/MIT Press, 229–248.
"""

__slots__ = (
Expand Down Expand Up @@ -327,6 +337,11 @@ def zhang(self):

return numerator / denominator

@property
def leverage(self):
return self.support - (
(self.antecedent_count / self.num_transactions) * (self.consequent_count / self.num_transactions))

def __eq__(self, other):
return (
self.antecedent == other.antecedent and self.consequent == other.consequent
Expand Down
4 changes: 4 additions & 0 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ def test_yulesq(self):
def test_zhang(self):
self.assertAlmostEqual(self.rule_one.zhang, 5 / 9)
self.assertAlmostEqual(self.rule_two.zhang, 5 / 8)

def test_leverage(self):
self.assertAlmostEqual(self.rule_one.leverage, 0.102040816326)
self.assertAlmostEqual(self.rule_two.leverage, 0.102040816326)
Loading