Skip to content

Commit

Permalink
Merge pull request #114 from erkankarabulut/main
Browse files Browse the repository at this point in the history
Add Leverage Rule Quality Criterion
  • Loading branch information
firefly-cpp authored Mar 15, 2024
2 parents 46a8ee7 + ac1545e commit 459bd8e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
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)

0 comments on commit 459bd8e

Please sign in to comment.