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

Implemented Zhang's Metric #105

Merged
merged 1 commit into from
Jan 14, 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
1 change: 1 addition & 0 deletions docs/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ The framework currently implements the following interest measures (metrics):
- Comprehensibility
- Netconf [#fn]_
- Yule's Q [#fn]_
- Zhang's Metric [#fn]_

More information about these interest measures can be found in the API reference
of the :class:`~niaarm.rule.Rule` class.
Expand Down
16 changes: 16 additions & 0 deletions interest_measures.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,19 @@ and 0 reflects independence)
**Reference:** E. V. Altay and B. Alatas, "Sensitivity Analysis of MODENAR Method for Mining of Numeric Association
Rules," 2019 1st International Informatics and Software Engineering Conference (UBMYK), 2019, pp. 1-6,
doi: 10.1109/UBMYK48245.2019.8965539.

# Zhang's Metric

Zheng's metric measures the strength of association (positive or negative) between the antecedent and consequent,
taking into account both their co-occurrence and non-co-occurrence.

```math
zhang(X \implies Y) =
\frac{conf(X \implies Y) - conf(\neg X \implies Y)}{max\{conf(X \implies Y), conf(\neg X \implies Y)\}}
```

**Range:** $`[-1, 1]`$ (-1 reflects total negative association, 1 reflects perfect positive association
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.
22 changes: 22 additions & 0 deletions niaarm/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ class Rule:
**Reference:** E. V. Altay and B. Alatas, "Sensitivity Analysis of MODENAR Method for Mining of Numeric Association
Rules," 2019 1st International Informatics and Software Engineering Conference (UBMYK), 2019, pp. 1-6,
doi: 10.1109/UBMYK48245.2019.8965539.
zhang: Zheng's metric measures the strength of association (positive or negative) between the antecedent and consequent, taking into account both their co-occurrence and non-co-occurrence.

:math:`zhang(X \implies Y) =
\frac{conf(X \implies Y) - conf(\neg X \implies Y)}{max\{conf(X \implies Y), conf(\neg X \implies Y)\}}`

**Range:** :math:`[-1, 1]` (-1 reflects total negative association, 1 reflects perfect positive association
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.

"""

Expand Down Expand Up @@ -176,6 +186,7 @@ class Rule:
"comprehensibility",
"netconf",
"yulesq",
"zhang",
)

def __init__(self, antecedent, consequent, fitness=0.0, transactions=None):
Expand Down Expand Up @@ -304,6 +315,17 @@ def comprehensibility(self):
1 + len(self.antecedent) + len(self.consequent)
)

@property
def zhang(self):
support_x = self.coverage
support_y = self.rhs_support
support = self.support

numerator = support - support_x * support_y
denominator = max(support * (1 - support_x), support_x * (support_y - support))

return numerator / denominator

def __eq__(self, other):
return (
self.antecedent == other.antecedent and self.consequent == other.consequent
Expand Down
1 change: 1 addition & 0 deletions niaarm/rule_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def __str__(self):
f'Average comprehensibility: {self.mean("comprehensibility")}\n'
f'Average netconf: {self.mean("netconf")}\n'
f'Average Yule\'s Q: {self.mean("yulesq")}\n'
f'Average Zhang\'s Metric: {self.mean("zhang")}\n'
f"Average antecedent length: {sum(len(rule.antecedent) for rule in self) / len(self)}\n"
f"Average consequent length: {sum(len(rule.consequent) for rule in self) / len(self)}\n"
)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@ def test_netconf(self):
def test_yulesq(self):
self.assertAlmostEqual(self.rule_one.yulesq, (6 - 1) / (6 + 1))
self.assertAlmostEqual(self.rule_two.yulesq, (6 - 1) / (6 + 1))

def test_zhang(self):
self.assertAlmostEqual(self.rule_one.zhang, 5 / 9)
self.assertAlmostEqual(self.rule_two.zhang, 5 / 8)
Loading