Action Rules
The package for action rules mining using Action-Apriori (Apriori Modified for Action Rules Mining).
- Documentation: https://lukassykora.github.io/action-rules
- GitHub: https://github.com/lukassykora/action-rules
- PyPI: https://pypi.org/project/action-rules/
- Free software: MIT
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 with bootstrap (percentile or BCa), analytic (Wald, random-design Wald, Newcombe-Wilson, Clopper-Pearson), Bayesian (uniform, Jeffreys or Haldane prior) or honest sample-splitting inference.
$ 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", # see "Methods and options" below
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 / UNSUPPORTED)
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())
Methods and options
method |
Interval |
|---|---|
bootstrap (default), bca |
Percentile or BCa bootstrap; bootstrap_type, n_bootstrap, resample_mode (rows or vectorised multinomial) |
analytic / wald |
Closed form selected by analytic_type: wald, wald_rd (random design, alias wald_random), newcombe_wilson (alias wilson), clopper_pearson, auto |
wald_rd, newcombe_wilson, clopper_pearson, auto |
Shortcuts for method="analytic" with that analytic_type |
bayesian, jeffreys |
Beta-Binomial posterior; prior = uniform (default), jeffreys or haldane; n_mc |
honest_split |
Rules re-mined on a discovery split, analytic intervals (analytic_type) on the disjoint estimation split |
simultaneous_m=mapplies a Bonferroni adjustment to every method: each interval is computed atalpha / m, somintervals hold jointly with probability at leastconfidence_level.- Options that a method would not use (for example
priorwithmethod="bootstrap"orresample_mode="multinomial"with an analytic method) raiseValueErrorinstead of being ignored.
Honest (selection-adjusted) intervals
Intervals computed on the same rows that selected the rules are optimistic (winner's curse). With
method="honest_split" the rows are split at random into disjoint discovery and estimation parts; the rules
are re-mined on the discovery part only (support thresholds scaled by its share of the rows) and the
intervals are computed on the estimation part. The fitted model, including get_rules(), is replaced by
the discovery-part model so that the intervals and the rules match.
action_rules.fit(data, stable_attributes, flexible_attributes, target, '0', '1')
results = action_rules.confidence_intervals(
data, method="honest_split", discovery_fraction=0.5, analytic_type="newcombe_wilson", random_state=42
)
discovery_rows, estimation_rows = action_rules.honest_split_indices
# Or with your own holdout: fit on the discovery data, estimate on the disjoint holdout.
action_rules.fit(discovery, stable_attributes, flexible_attributes, target, '0', '1')
results = action_rules.confidence_intervals(discovery, method="honest_split", estimation_data=holdout)
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_method— anymethodfrom the table above--analytic_type— analytic sub-type foranalytic,waldandhonest_split(default:wald)--bootstrap_type—percentile(default) orbca--resample_mode—rows(default) ormultinomial(bootstrap only)--prior—uniform,laplace,jeffreysorhaldane(Bayesian only)--simultaneous_m— Bonferroni family size, applies to every method--discovery_fraction— discovery share forhonest_split(default: 0.5)--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
- Confidence Intervals (Bootstrap, Analytic, Bayesian)
- Titanic Dataset (GPU accelerated)
- Customer Churn (easy workflow)
- High-Utility Action Rules Mining Example
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)
- End-to-end mining with utility tables, confidence intervals, and JSON export
- Visual diagnostics: churn-rate panel, bootstrap distribution, forest plot, bootstrap vs analytic CI scatter
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)
- Per-rule confidence intervals across five inference methods (bootstrap percentile/BCa, Wald, Wilson, Bayesian) on four benchmark datasets
- Empirical coverage simulation: do the nominal 95% intervals actually cover 95% of the time?
- Runtime benchmark of every CI method on every dataset
- Rule categorization (Accept / Reject / Uncertain) for uplift and realistic rule gain
- Stratified K-fold cross-validation with four targeting strategies and three reliability views
- Sensitivity of the categorization to support-threshold perturbations
- Sensitivity of realistic rule gain to a uniform cost-side rescaling of the utility tables
Performance
- Customer Churn (GPU vs. CPU, sparse matrix vs. dense matrix, Pandas vs. cuDF)
- Scalene Profiling
- GPU Memory Usage - Sparse vs. Dense Matrix
- CPU Usage
- Compare the Action-Rules package with ActionRulesDiscovery package - link to ActionRulesDiscovery package: https://github.com/lukassykora/actionrules
Credits
This package was created with Cookiecutter and the waynerv/cookiecutter-pypackage project template.
Metadata
Release files for action-rules 2.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| action_rules-2.1.1.tar.gz | 131.9 kB | Without provenance | Details for action_rules-2.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| action_rules-2.1.1-py3-none-any.whl | Python 3 | none | any | Details for action_rules-2.1.1-py3-none-any.whl |
Total release size: 223.5 kB
Release files / action_rules-2.1.1.tar.gz
| Download URL | action_rules-2.1.1.tar.gz |
|---|---|
| Size | 131.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7c12f8fb0fe5b81f829ba06e2acf3f05e69bff62030676df8028dad29e54b418
|
|
BLAKE2b-256 checksum How to use checksums |
6fc1305ad49cb0dd8ee087dc08aaad99441acafcd9e4eb8f0b97700897d5746a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / action_rules-2.1.1-py3-none-any.whl
| Download URL | action_rules-2.1.1-py3-none-any.whl |
|---|---|
| Size | 91.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
01badc0f4d073ca1616ac077d34a42243257c56462ac94f9bb819a29e79984d3
|
|
BLAKE2b-256 checksum How to use checksums |
c113f08c814bf0d85c270b4d71fec673e0425f3a3837feade81fd906f74c3705
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for action-rules 2.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.