Skip to main content

Action Rules

pypi python Build Status codecov

The package for action rules mining using Action-Apriori (Apriori Modified for Action Rules Mining).

Installation

$ pip install action-rules

For command-line interface (CLI) usage, the action-rules package must be installed using pipx:

$ pipx install action-rules

Features

Action Rules API

# Import Module
from action_rules import ActionRules
import pandas as pd

# Get Data
transactions = {'Sex': ['M', 'F', 'M', 'M', 'F', 'M', 'F'],
                'Age': ['Y', 'Y', 'O', 'Y', 'Y', 'O', 'Y'],
                'Class': [1, 1, 2, 2, 1, 1, 2],
                'Embarked': ['S', 'C', 'S', 'C', 'S', 'C', 'C'],
                'Survived': [1, 1, 0, 0, 1, 1, 0],
                }
data = pd.DataFrame.from_dict(transactions)
# Initialize ActionRules Miner with Parameters
stable_attributes = ['Age', 'Sex']
flexible_attributes = ['Embarked', 'Class']
target = 'Survived'
min_stable_attributes = 2
min_flexible_attributes = 1  # min 1
min_undesired_support = 1
min_undesired_confidence = 0.5
min_desired_support = 1
min_desired_confidence = 0.5
undesired_state = '0'
desired_state = '1'
# Action Rules Mining
action_rules = ActionRules(
    min_stable_attributes=min_stable_attributes,
    min_flexible_attributes=min_flexible_attributes,
    min_undesired_support=min_undesired_support,
    min_undesired_confidence=min_undesired_confidence,
    min_desired_support=min_desired_support,
    min_desired_confidence=min_desired_confidence,
    verbose=True
)
# Fit
action_rules.fit(
    data=data,  # cuDF or Pandas Dataframe
    stable_attributes=stable_attributes,
    flexible_attributes=flexible_attributes,
    target=target,
    target_undesired_state=undesired_state,
    target_desired_state=desired_state,
    use_sparse_matrix=True,  # needs SciPy or Cupyx (if use_gpu is True) installed
    use_gpu=False,  # needs Cupy installed
)
# Print rules
# Example: [(Age: O) ∧ (Sex: M) ∧ (Embarked: S → C)] ⇒ [Survived: 0 → 1], support of undesired part: 1, confidence of undesired part: 1.0, support of desired part: 1, confidence of desired part: 1.0, uplift: 1.0
for action_rule in action_rules.get_rules().get_ar_notation():
    print(action_rule)
# Print rules (pretty notation)
# Example: If attribute 'Age' is 'O', attribute 'Sex' is 'M', attribute 'Embarked' value 'S' is changed to 'C', then 'Survived' value '0' is changed to '1 with uplift: 1.0.
for action_rule in action_rules.get_rules().get_pretty_ar_notation():
    print(action_rule)
# JSON export
print(action_rules.get_rules().get_export_notation())

Action Rules CLI

$ action-rules --min_stable_attributes 2 --min_flexible_attributes 1 --min_undesired_support 1 --min_undesired_confidence 0.5 --min_desired_support 1 --min_desired_confidence 0.5 --csv_path 'data.csv' --stable_attributes 'Sex, Age' --flexible_attributes 'Class, Embarked' --target 'Survived' --undesired_state '0' --desired_state '1' --output_json_path 'output.json'

Confidence Intervals

Compute confidence intervals for uplift and realistic rule gain using one of three methods: bootstrap, analytic (Wald), or Bayesian.

$ pip install action-rules[inference]   # adds scipy
$ pip install action-rules[viz]         # adds matplotlib + scipy
from action_rules import ActionRules

# After fitting action rules...
action_rules = ActionRules(
    min_stable_attributes=2, min_flexible_attributes=1,
    min_undesired_support=1, min_undesired_confidence=0.5,
    min_desired_support=1, min_desired_confidence=0.5,
)
action_rules.fit(data, stable_attributes, flexible_attributes, target, '0', '1')

# Compute bootstrap confidence intervals
results = action_rules.confidence_intervals(
    data,
    method="bootstrap",      # "bootstrap", "analytic", "wald", or "bayesian"
    confidence_level=0.95,
    threshold=0.0,           # categorize rules as Accept/Reject/Uncertain
    n_bootstrap=1000,        # bootstrap resamples (bootstrap only)
    random_state=42,
)

# Each result contains: uplift_point, uplift_ci_lower, uplift_ci_upper, uplift_se,
# realistic_rule_gain_point/ci_lower/ci_upper/se (if utility tables provided),
# category (RuleCategory.ACCEPT / REJECT / UNCERTAIN)
for r in results:
    print(f"Rule {r.rule_index}: uplift = {r.uplift_point:.4f} "
          f"[{r.uplift_ci_lower:.4f}, {r.uplift_ci_upper:.4f}] → {r.category.value}")

# JSON export now includes CI data
print(action_rules.get_rules().get_export_notation())

Visualization

from action_rules.visualization import bootstrap_histogram, forest_plot, grouped_forest_plot

# Single-rule distribution (bootstrap or Bayesian)
fig = bootstrap_histogram(results[0], metric="uplift", threshold=0.0)
fig.savefig("distribution.png")

# Forest plot: all rules with CI bars
fig = forest_plot(results, metric="uplift", threshold=0.0)
fig.savefig("forest.png")

# Compare methods side-by-side
results_boot = action_rules.confidence_intervals(data, method="bootstrap", random_state=42)
results_anal = action_rules.confidence_intervals(data, method="analytic")
results_bayes = action_rules.confidence_intervals(data, method="bayesian", random_state=42)

fig = grouped_forest_plot(
    {"bootstrap": results_boot, "analytic": results_anal, "bayesian": results_bayes},
    metric="uplift",
    threshold=0.0,
)
fig.savefig("comparison.png")

CLI with Confidence Intervals

$ action-rules --min_stable_attributes 2 --min_flexible_attributes 1 \
    --min_undesired_support 1 --min_undesired_confidence 0.5 \
    --min_desired_support 1 --min_desired_confidence 0.5 \
    --csv_path data.csv --stable_attributes 'Sex, Age' \
    --flexible_attributes 'Class, Embarked' --target Survived \
    --undesired_state 0 --desired_state 1 \
    --ci_method bootstrap --confidence_level 0.95 --n_bootstrap 1000 \
    --ci_threshold 0.0 --random_state 42 \
    --output_json_path output.json

Available CI options:

  • --ci_methodbootstrap, analytic, wald, or bayesian
  • --confidence_level — confidence level (default: 0.95)
  • --ci_threshold — threshold for Accept/Reject/Uncertain categorization
  • --n_bootstrap — number of bootstrap resamples (default: 1000)
  • --n_mc — number of Monte Carlo draws for Bayesian (default: 10000)
  • --random_state — random seed for reproducibility

Jupyter Notebook Examples

Hands-On Telco Tour

A two-notebook end-to-end walkthrough on the Telco Customer Churn dataset. Run them in order; together they take under a minute. (folder README)

Inference and Validation Studies

A self-contained suite of replicability notebooks for the package's confidence-interval and cross-validation tooling. Each notebook persists a CSV under notebooks/inference_studies/results/ so the studies can be inspected without rerunning. (folder README)

Performance

Credits

This package was created with Cookiecutter and the waynerv/cookiecutter-pypackage project template.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

action_rules-2.0.1.tar.gz (119.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

action_rules-2.0.1-py3-none-any.whl (84.5 kB view details)

Uploaded Python 3

File details

Details for the file action_rules-2.0.1.tar.gz.

File metadata

  • Download URL: action_rules-2.0.1.tar.gz
  • Upload date:
  • Size: 119.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for action_rules-2.0.1.tar.gz
Algorithm Hash digest
SHA256 7964056f5656693d05b072f75bcdfae3ed90be53f88965876d4f55dfc1b3136a
MD5 7a83d30359ba8e157c6fd0ac6a7e2437
BLAKE2b-256 eb95cb2dab11f624a21cae5682ac70bafc22ec68f3f29d26bff9b0096896338b

See more details on using hashes here.

File details

Details for the file action_rules-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: action_rules-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 84.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for action_rules-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 60b804fd989500c49b220be5cfc64fe26853f40b13678422704755ab944c1a91
MD5 325c70490f61ade7b3185ab62f491610
BLAKE2b-256 4bc2f00ab7f023b58a721807be76c54f8f6b68854ba0d6a8773173221970adab

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 files

2.0.0

2 files

1.1.0

2 files

1.0.11

2 files

1.0.10

2 files

1.0.9

2 files

1.0.8

2 files

1.0.7

2 files

1.0.6

2 files

1.0.5

2 files

1.0.4

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

0.0.29

2 files

0.0.28

2 files

0.0.27

2 files

0.0.26

2 files

0.0.25

2 files

0.0.24

2 files

0.0.23

2 files

0.0.22

2 files

0.0.21

2 files

0.0.20

2 files

0.0.18

2 files

0.0.17

2 files

0.0.16

2 files

0.0.15

2 files

0.0.14

2 files

0.0.13

2 files

0.0.11

2 files

0.0.10

2 files

0.0.9

2 files

0.0.8

2 files

0.0.7

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page